-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't allow empty strings when creating items on the glossary (#1335)
* Don't allow empty strings on the glossary * Update system tests
- Loading branch information
Showing
16 changed files
with
253 additions
and
124 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
services/headless-lms/migrations/20241114074254_add_constraints_to_glossary.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALTER TABLE glossary DROP CONSTRAINT term_not_empty, | ||
DROP CONSTRAINT definition_not_empty; |
3 changes: 3 additions & 0 deletions
3
services/headless-lms/migrations/20241114074254_add_constraints_to_glossary.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ALTER TABLE glossary | ||
ADD CONSTRAINT term_not_empty CHECK (trim(term) <> ''), | ||
ADD CONSTRAINT definition_not_empty CHECK (trim(definition) <> ''); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
.../main-frontend/src/components/page-specific/manage/courses/id/glossary/CreateTermForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from "react" | ||
import { useForm } from "react-hook-form" | ||
import { useTranslation } from "react-i18next" | ||
|
||
import { postNewTerm } from "@/services/backend/courses" | ||
import Button from "@/shared-module/common/components/Button" | ||
import TextAreaField from "@/shared-module/common/components/InputFields/TextAreaField" | ||
import TextField from "@/shared-module/common/components/InputFields/TextField" | ||
import useToastMutation from "@/shared-module/common/hooks/useToastMutation" | ||
|
||
interface NewTermForm { | ||
newTerm: string | ||
newDefinition: string | ||
} | ||
|
||
interface CreateTermFormProps { | ||
refetch: () => void | ||
courseId: string | ||
} | ||
|
||
const CreateTermForm: React.FC<CreateTermFormProps> = ({ refetch, courseId }) => { | ||
const { t } = useTranslation() | ||
const { | ||
register, | ||
handleSubmit, | ||
formState: { errors, isValid }, | ||
reset, | ||
// eslint-disable-next-line i18next/no-literal-string | ||
} = useForm<NewTermForm>({ mode: "onChange" }) | ||
|
||
const createMutation = useToastMutation( | ||
(data: NewTermForm) => postNewTerm(courseId, data.newTerm, data.newDefinition), | ||
{ | ||
notify: true, | ||
method: "POST", | ||
}, | ||
{ | ||
onSuccess: () => { | ||
reset() | ||
refetch() | ||
}, | ||
}, | ||
) | ||
|
||
const onCreate = (data: NewTermForm) => { | ||
createMutation.mutate(data) | ||
} | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onCreate)}> | ||
<TextField | ||
label={t("new-term")} | ||
placeholder={t("new-term")} | ||
{...register("newTerm", { | ||
required: true, | ||
pattern: { | ||
value: /\S+/, | ||
message: t("required"), | ||
}, | ||
})} | ||
error={errors.newTerm && t("required")} | ||
/> | ||
<TextAreaField | ||
placeholder={t("new-definition")} | ||
label={t("new-definition")} | ||
{...register("newDefinition", { | ||
required: true, | ||
pattern: { | ||
value: /\S+/, | ||
message: t("required"), | ||
}, | ||
})} | ||
error={errors.newDefinition && t("required")} | ||
/> | ||
<Button variant="primary" size="medium" type="submit" disabled={!isValid}> | ||
{t("button-text-save")} | ||
</Button> | ||
</form> | ||
) | ||
} | ||
|
||
export default CreateTermForm |
Oops, something went wrong.