|
| 1 | +import { |
| 2 | + useCallback, |
| 3 | + useEffect, |
| 4 | +} from 'react'; |
| 5 | +import { |
| 6 | + useNavigate, |
| 7 | + useParams, |
| 8 | +} from 'react-router'; |
| 9 | +import { |
| 10 | + Button, |
| 11 | + Heading, |
| 12 | + SelectInput, |
| 13 | + TextArea, |
| 14 | + TextInput, |
| 15 | +} from '@ifrc-go/ui'; |
| 16 | +import { |
| 17 | + createSubmitHandler, |
| 18 | + getErrorObject, |
| 19 | + ObjectSchema, |
| 20 | + PartialForm, |
| 21 | + requiredStringCondition, |
| 22 | + useForm, |
| 23 | +} from '@togglecorp/toggle-form'; |
| 24 | + |
| 25 | +import ContainerWrapper from '#components/ContainerWrapper'; |
| 26 | +import FileUpload from '#components/FileUpload'; |
| 27 | +import FormSection from '#components/FormSection'; |
| 28 | +import Page from '#components/Page'; |
| 29 | +import { |
| 30 | + ProjectCreateInput, |
| 31 | + useCreateProjectMutation, |
| 32 | + useDepartmentsQuery, |
| 33 | + useProjectDetailQuery, |
| 34 | + useUpdateProjectMutation, |
| 35 | +} from '#generated/types/graphql'; |
| 36 | +import urlToFile from '#utils/urlToFile'; |
| 37 | + |
| 38 | +type PartialFormType = PartialForm<ProjectCreateInput> & |
| 39 | +{ createdBy: string, modifiedBy: string } |
| 40 | + |
| 41 | +type FormSchema = ObjectSchema<PartialFormType>; |
| 42 | +type FormSchemaFields = ReturnType<FormSchema['fields']>; |
| 43 | + |
| 44 | +const EditBlogSchema: FormSchema = { |
| 45 | + fields: (): FormSchemaFields => ({ |
| 46 | + title: { |
| 47 | + required: true, |
| 48 | + requiredValidation: requiredStringCondition, |
| 49 | + }, |
| 50 | + description: { |
| 51 | + required: true, |
| 52 | + requiredValidation: requiredStringCondition, |
| 53 | + }, |
| 54 | + department: { |
| 55 | + required: true, |
| 56 | + }, |
| 57 | + coverImage: { |
| 58 | + required: true, |
| 59 | + }, |
| 60 | + createdBy: {}, |
| 61 | + modifiedBy: {}, |
| 62 | + |
| 63 | + }), |
| 64 | +}; |
| 65 | + |
| 66 | +const defaultEditFormValue: PartialFormType = { |
| 67 | + createdBy: '', |
| 68 | + modifiedBy: '', |
| 69 | +}; |
| 70 | +function ProjectForm() { |
| 71 | + const { id } = useParams(); |
| 72 | + const navigate = useNavigate(); |
| 73 | + const [{ data }] = useProjectDetailQuery({ |
| 74 | + variables: { id: id || '' }, pause: !id, |
| 75 | + }); |
| 76 | + const [{ data: departments }] = useDepartmentsQuery(); |
| 77 | + |
| 78 | + const [{ fetching: createPending }, createProjectMutate] = useCreateProjectMutation(); |
| 79 | + const [{ fetching: updatePending }, updateProjectMutate] = useUpdateProjectMutation(); |
| 80 | + const { |
| 81 | + setFieldValue, |
| 82 | + error: formError, |
| 83 | + value, |
| 84 | + validate, |
| 85 | + setError, |
| 86 | + } = useForm(EditBlogSchema, { value: defaultEditFormValue }); |
| 87 | + |
| 88 | + const error = getErrorObject(formError); |
| 89 | + |
| 90 | + const handleFormSubmit = useCallback(() => { |
| 91 | + const handler = createSubmitHandler( |
| 92 | + validate, |
| 93 | + setError, |
| 94 | + async (val) => { |
| 95 | + const mutateData = { |
| 96 | + coverImage: val.coverImage ?? null, |
| 97 | + title: val.title ?? '', |
| 98 | + department: val.department ?? null, |
| 99 | + description: val.description ?? '', |
| 100 | + }; |
| 101 | + if (id) { |
| 102 | + const res = await updateProjectMutate({ |
| 103 | + pk: id, |
| 104 | + data: mutateData, |
| 105 | + }); |
| 106 | + if (res.data?.updateProject?.ok) { |
| 107 | + navigate('/projects'); |
| 108 | + } else if (res.data?.updateProject.errors) { |
| 109 | + setError(res.data.updateProject.errors); |
| 110 | + } |
| 111 | + } else { |
| 112 | + const res = await createProjectMutate({ |
| 113 | + data: mutateData, |
| 114 | + }); |
| 115 | + |
| 116 | + if (res.data?.createProject.ok) { |
| 117 | + navigate('/projects'); |
| 118 | + } else if (res.data?.createProject?.errors) { |
| 119 | + setError(res.data.createProject.errors); |
| 120 | + } |
| 121 | + } |
| 122 | + }, |
| 123 | + ); |
| 124 | + handler(); |
| 125 | + }, [setError, validate, id, createProjectMutate, updateProjectMutate, navigate]); |
| 126 | + |
| 127 | + useEffect(() => { |
| 128 | + if (data?.project) { |
| 129 | + const { project } = data; |
| 130 | + if (project.coverImage) { |
| 131 | + urlToFile(project?.coverImage?.url, project?.coverImage?.name) |
| 132 | + .then((file) => { |
| 133 | + setFieldValue(file, 'coverImage'); |
| 134 | + }); |
| 135 | + } |
| 136 | + setFieldValue(project?.title, 'title'); |
| 137 | + setFieldValue(project?.description, 'description'); |
| 138 | + setFieldValue(project?.department?.id, 'department'); |
| 139 | + setFieldValue(`${project.modifiedBy?.firstName} ${project.modifiedBy?.lastName}`, 'modifiedBy'); |
| 140 | + setFieldValue(`${project.createdBy?.firstName} ${project.createdBy?.lastName}`, 'createdBy'); |
| 141 | + } |
| 142 | + }, [data, setFieldValue]); |
| 143 | + |
| 144 | + const departmentOptions = departments?.departments.results.map( |
| 145 | + (dept) => ({ |
| 146 | + id: dept.id, |
| 147 | + name: dept.title, |
| 148 | + }), |
| 149 | + ) ?? []; |
| 150 | + |
| 151 | + return ( |
| 152 | + <Page> |
| 153 | + <ContainerWrapper> |
| 154 | + <FormSection headingLevel={3} label="VACANCY DETAILS" /> |
| 155 | + {(value.createdBy && value.modifiedBy) && ( |
| 156 | + <FormSection> |
| 157 | + <Heading level={6}> |
| 158 | + Created by: |
| 159 | + {' '} |
| 160 | + {value.createdBy} |
| 161 | + </Heading> |
| 162 | + <Heading level={6}> |
| 163 | + Modified by: |
| 164 | + {' '} |
| 165 | + {value.createdBy} |
| 166 | + </Heading> |
| 167 | + </FormSection> |
| 168 | + )} |
| 169 | + <FormSection label="Title" description="Enter the Title" withAsteriskOnTitle> |
| 170 | + <TextInput |
| 171 | + name="title" |
| 172 | + value={value.title} |
| 173 | + error={error?.title as string} |
| 174 | + onChange={setFieldValue} |
| 175 | + /> |
| 176 | + </FormSection> |
| 177 | + <FormSection label="Cover Image" description="Add a Cover Image, which will be attached and shown on Project" withAsteriskOnTitle> |
| 178 | + <FileUpload |
| 179 | + name="coverImage" |
| 180 | + onChange={(files) => setFieldValue(files, 'coverImage')} |
| 181 | + value={value.coverImage} |
| 182 | + /> |
| 183 | + </FormSection> |
| 184 | + <FormSection label="Description" description="Enter the Description" withAsteriskOnTitle> |
| 185 | + <TextArea |
| 186 | + name="description" |
| 187 | + value={value.description} |
| 188 | + error={error?.description as string} |
| 189 | + onChange={setFieldValue} |
| 190 | + /> |
| 191 | + </FormSection> |
| 192 | + <FormSection label="Type" description="Add type to either Tuesday Program or Radio Red Cross" withAsteriskOnTitle> |
| 193 | + <SelectInput |
| 194 | + name="department" |
| 195 | + options={departmentOptions} |
| 196 | + value={value.department} |
| 197 | + keySelector={(o) => o.id} |
| 198 | + labelSelector={(o) => o.name} |
| 199 | + onChange={setFieldValue} |
| 200 | + placeholder="Select Status" |
| 201 | + error={error?.department} |
| 202 | + /> |
| 203 | + </FormSection> |
| 204 | + <FormSection> |
| 205 | + <Button name="save" onClick={handleFormSubmit} variant="primary"> |
| 206 | + {createPending || updatePending ? 'Saving' : 'Save'} |
| 207 | + </Button> |
| 208 | + </FormSection> |
| 209 | + </ContainerWrapper> |
| 210 | + </Page> |
| 211 | + ); |
| 212 | +} |
| 213 | + |
| 214 | +export default ProjectForm; |
0 commit comments