import React from 'react'; import { Button, Textarea, InputGroup, InputRightElement, Input } from '@chakra-ui/react'; import { FormControl, FormLabel, FormHelperText } from '@chakra-ui/form-control'; import { Flex, Box, Text, Divider, Stack } from '@chakra-ui/layout'; import { Controller, UseFormReturn } from 'react-hook-form'; /** * Types */ import type { OptionType } from '@educt/types'; import { UserRoleEnum } from '@educt/enums'; /** * Components */ import AsyncSelect from '@educt/components/AsyncSelect'; import FileSelect from '@educt/components/FileSelect'; /** * Hooks */ import { useErrorHandler } from 'react-error-boundary'; /** * Services */ import { CategoryServiceInstance, UserServiceInstance } from '@educt/services'; export type InputFields = { title: string; image?: File | undefined; category_id: string; description: string; education_description: string | null; teacher_id: string; }; export type CourseFormPropsType = { onSubmit: () => Promise; buttonLabel: string; isLoading: boolean; reactHookForm: UseFormReturn; }; const CourseForm: React.FC = ({ onSubmit, buttonLabel, isLoading, reactHookForm }) => { const { control, register, watch, formState: { errors, isDirty }, } = reactHookForm; const handleError = useErrorHandler(); /** * Load users into select field */ const loadUsersOptions = async (): Promise => { try { const users = await UserServiceInstance.fetchAll({ limit: 12, role: UserRoleEnum.TEACHER }); return users.data.map(user => ({ label: user.fullname, value: user.id, })); } catch (error) { handleError(error); } }; /** * Load categories into select filed */ const loadCategoriesOptions = async (): Promise => { try { const categories = await CategoryServiceInstance.fetchAll(); return categories.data.map(category => ({ label: category.title, value: category.id, })); } catch (error) { handleError(error); } }; const watchTitle = watch('title'); const watchDescription = watch('description'); const watchEducationDescription = watch('education_description'); return (
Name 90 ? 'red.500' : 'gray.500'}> {watchTitle?.length || 0}/90 e.g Web Development in Javascript {errors.title?.message} Description