diff --git a/package-lock.json b/package-lock.json index 82c172c..364ee9d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.2.0", "license": "MIT", "dependencies": { + "@chakra-ui/icons": "^2.1.1", "@chakra-ui/react": "^2.8.2", "@emotion/react": "^11.11.4", "@emotion/styled": "^11.11.0", @@ -24,6 +25,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-helmet": "^6.1.0", + "react-hook-form": "^7.51.2", "react-redux": "^9.1.0", "react-router-dom": "^6.22.1", "redux": "^5.0.1", @@ -2213,6 +2215,18 @@ "react": ">=18" } }, + "node_modules/@chakra-ui/icons": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/@chakra-ui/icons/-/icons-2.1.1.tgz", + "integrity": "sha512-3p30hdo4LlRZTT5CwoAJq3G9fHI0wDc0pBaMHj4SUn0yomO+RcDRlzhdXqdr5cVnzax44sqXJVnf3oQG0eI+4g==", + "dependencies": { + "@chakra-ui/icon": "3.2.0" + }, + "peerDependencies": { + "@chakra-ui/system": ">=2.0.0", + "react": ">=18" + } + }, "node_modules/@chakra-ui/image": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/@chakra-ui/image/-/image-2.1.0.tgz", @@ -8546,6 +8560,21 @@ "react": ">=16.3.0" } }, + "node_modules/react-hook-form": { + "version": "7.51.2", + "resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.51.2.tgz", + "integrity": "sha512-y++lwaWjtzDt/XNnyGDQy6goHskFualmDlf+jzEZvjvz6KWDf7EboL7pUvRCzPTJd0EOPpdekYaQLEvvG6m6HA==", + "engines": { + "node": ">=12.22.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/react-hook-form" + }, + "peerDependencies": { + "react": "^16.8.0 || ^17 || ^18" + } + }, "node_modules/react-i18next": { "version": "11.8.5", "resolved": "https://registry.npmjs.org/react-i18next/-/react-i18next-11.8.5.tgz", diff --git a/package.json b/package.json index fc3daf7..4d87616 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@ijl/cli": "^5.0.3" }, "dependencies": { + "@chakra-ui/icons": "^2.1.1", "@chakra-ui/react": "^2.8.2", "@emotion/react": "^11.11.4", "@emotion/styled": "^11.11.0", @@ -35,6 +36,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-helmet": "^6.1.0", + "react-hook-form": "^7.51.2", "react-redux": "^9.1.0", "react-router-dom": "^6.22.1", "redux": "^5.0.1", diff --git a/src/pages/course-list.tsx b/src/pages/course-list.tsx index 408d901..6b0b1fa 100644 --- a/src/pages/course-list.tsx +++ b/src/pages/course-list.tsx @@ -25,6 +25,7 @@ import { FormHelperText, Center, FormErrorMessage, + useToast, } from '@chakra-ui/react' import { useForm, Controller } from 'react-hook-form' @@ -41,25 +42,50 @@ interface NewCourseForm { } const CoursesList = () => { + const toast = useToast() const user = useAppSelector((s) => s.user) - const { data, isLoading, error } = api.useCoursesListQuery() + const { data, isLoading } = api.useCoursesListQuery() const [createUpdateCourse, crucQuery] = api.useCreateUpdateCourseMutation() - const [value, setValue] = useState('') const [showForm, setShowForm] = useState(false) const [courseDetailsOpenedId, setCourseDetailsOpenedId] = useState< string | null >(null) + const toastRef = useRef(null) - const { control, handleSubmit, reset } = useForm({ - mode: 'all', + const { + control, + handleSubmit, + reset, + formState: { errors }, + getValues, + } = useForm({ + defaultValues: { + startDt: '', + name: '', + }, }) const onSubmit = ({ startDt, name }) => { + toastRef.current = toast({ + title: 'Отправляем', + status: 'loading', + duration: 9000, + }) createUpdateCourse({ name, startDt }) } useEffect(() => { if (crucQuery.isSuccess) { + const values = getValues() + if (toastRef.current) { + toast.update(toastRef.current, { + title: 'Курс создан.', + description: `Курс ${values.name} успешно создан`, + status: 'success', + duration: 9000, + isClosable: true, + }) + } reset() } }, [crucQuery.isSuccess]) @@ -84,7 +110,7 @@ const CoursesList = () => { {isTeacher(user) && ( - {!showForm ? ( + {showForm ? ( @@ -99,18 +125,22 @@ const CoursesList = () => { control={control} name="startDt" rules={{ required: 'Обязательное поле' }} - render={({ field, fieldState }) => ( - + render={({ field }) => ( + Дата начала - {fieldState.error ? ( + {errors.startDt ? ( - {fieldState.error.message} + {errors.startDt?.message} ) : ( @@ -120,20 +150,37 @@ const CoursesList = () => { )} /> - - Название новой лекции: - - - + ( + + Название новой лекции: + + {errors.name && ( + + {errors.name.message} + + )} + + )} + />