|
| 1 | +import { |
| 2 | + useCallback, |
| 3 | + useEffect, |
| 4 | +} from 'react'; |
| 5 | +import { |
| 6 | + useNavigate, |
| 7 | + useParams, |
| 8 | +} from 'react-router'; |
| 9 | +import { |
| 10 | + Button, |
| 11 | + Container, |
| 12 | + Heading, |
| 13 | + NumberInput, |
| 14 | + RawFileInput, |
| 15 | + SelectInput, |
| 16 | + TextArea, |
| 17 | + TextInput, |
| 18 | +} from '@ifrc-go/ui'; |
| 19 | +import { |
| 20 | + createSubmitHandler, |
| 21 | + getErrorObject, |
| 22 | + integerCondition, |
| 23 | + ObjectSchema, |
| 24 | + PartialForm, |
| 25 | + requiredStringCondition, |
| 26 | + useForm, |
| 27 | +} from '@togglecorp/toggle-form'; |
| 28 | + |
| 29 | +import FormSection from '#components/FormSection'; |
| 30 | +import Page from '#components/Page'; |
| 31 | +import { |
| 32 | + PartnerCreateInput, |
| 33 | + PartnerScopeEnum, |
| 34 | + useCreatePartnerMutation, |
| 35 | + usePartnerDetailQuery, |
| 36 | + useUpdatePartnerMutation, |
| 37 | +} from '#generated/types/graphql'; |
| 38 | +import urlToFile from '#utils/urlToFile'; |
| 39 | + |
| 40 | +import styles from './styles.module.css'; |
| 41 | + |
| 42 | +type PartialFormType = PartialForm<PartnerCreateInput> & |
| 43 | +{ createdBy: string, modifiedBy: string } |
| 44 | + |
| 45 | +type FormSchema = ObjectSchema<PartialFormType>; |
| 46 | +type FormSchemaFields = ReturnType<FormSchema['fields']>; |
| 47 | + |
| 48 | +const EditBlogSchema: FormSchema = { |
| 49 | + fields: (): FormSchemaFields => ({ |
| 50 | + title: { |
| 51 | + required: true, |
| 52 | + requiredValidation: requiredStringCondition, |
| 53 | + }, |
| 54 | + scope: { |
| 55 | + required: true, |
| 56 | + requiredValidation: requiredStringCondition, |
| 57 | + }, |
| 58 | + image: { |
| 59 | + required: true, |
| 60 | + }, |
| 61 | + createdBy: {}, |
| 62 | + modifiedBy: {}, |
| 63 | + |
| 64 | + }), |
| 65 | +}; |
| 66 | + |
| 67 | +const defaultEditFormValue: PartialFormType = { |
| 68 | + createdBy: '', |
| 69 | + modifiedBy: '', |
| 70 | +}; |
| 71 | +function PartnerForm() { |
| 72 | + const { id } = useParams(); |
| 73 | + const navigate = useNavigate(); |
| 74 | + const [{ data }] = usePartnerDetailQuery({ |
| 75 | + variables: { id: id || '' }, pause: !id, |
| 76 | + }); |
| 77 | + const [{ fetching: createPending }, createPartnerMutate] = useCreatePartnerMutation(); |
| 78 | + const [{ fetching: updatePending }, updatePartnerMutate] = useUpdatePartnerMutation(); |
| 79 | + const { |
| 80 | + setFieldValue, |
| 81 | + error: formError, |
| 82 | + value, |
| 83 | + validate, |
| 84 | + setError, |
| 85 | + } = useForm(EditBlogSchema, { value: defaultEditFormValue }); |
| 86 | + |
| 87 | + const error = getErrorObject(formError); |
| 88 | + |
| 89 | + const handleFormSubmit = useCallback(() => { |
| 90 | + const handler = createSubmitHandler( |
| 91 | + validate, |
| 92 | + setError, |
| 93 | + async (val) => { |
| 94 | + const mutateData = { |
| 95 | + image: val.image ?? '', |
| 96 | + title: val.title ?? '', |
| 97 | + scope: val.scope as PartnerScopeEnum, |
| 98 | + }; |
| 99 | + if (id) { |
| 100 | + const res = await updatePartnerMutate({ |
| 101 | + pk: id, |
| 102 | + data: mutateData, |
| 103 | + }); |
| 104 | + if (res.data?.updatePartner?.ok) { |
| 105 | + navigate('/partners'); |
| 106 | + } else if (res.data?.updatePartner.errors) { |
| 107 | + setError(res.data.updatePartner.errors); |
| 108 | + } |
| 109 | + } else { |
| 110 | + const res = await createPartnerMutate({ |
| 111 | + data: mutateData, |
| 112 | + }); |
| 113 | + |
| 114 | + if (res.data?.createPartner.ok) { |
| 115 | + navigate('/partners'); |
| 116 | + } else if (res.data?.createPartner?.errors) { |
| 117 | + setError(res.data.createPartner.errors); |
| 118 | + } |
| 119 | + } |
| 120 | + }, |
| 121 | + ); |
| 122 | + handler(); |
| 123 | + }, [setError, validate, id, createPartnerMutate, updatePartnerMutate, navigate]); |
| 124 | + |
| 125 | + useEffect(() => { |
| 126 | + if (data?.partner) { |
| 127 | + const { partner } = data; |
| 128 | + if (partner.image) { |
| 129 | + urlToFile(partner.image.url, partner.image.name) |
| 130 | + .then((file) => { |
| 131 | + setFieldValue(file, 'image'); |
| 132 | + }); |
| 133 | + } |
| 134 | + setFieldValue(partner.title, 'title'); |
| 135 | + setFieldValue(partner.scope, 'scope'); |
| 136 | + setFieldValue(partner.image, 'image'); |
| 137 | + setFieldValue(`${partner.modifiedBy.firstName} ${partner.modifiedBy.lastName}`, 'modifiedBy'); |
| 138 | + setFieldValue(`${partner.createdBy.firstName} ${partner.createdBy.lastName}`, 'createdBy'); |
| 139 | + } |
| 140 | + }, [data, setFieldValue]); |
| 141 | + |
| 142 | + const scopeOptions = Object.values(PartnerScopeEnum).map((scope) => ({ |
| 143 | + value: scope, |
| 144 | + label: scope, |
| 145 | + })); |
| 146 | + return ( |
| 147 | + <Page> |
| 148 | + <Container |
| 149 | + className={styles.container} |
| 150 | + childrenContainerClassName={styles.containerChild} |
| 151 | + > |
| 152 | + <FormSection headingLevel={3} label="Partner DETAIL" /> |
| 153 | + {(value.createdBy && value.modifiedBy) && ( |
| 154 | + <FormSection inputClassName={styles.inputClassName}> |
| 155 | + <div> |
| 156 | + <Heading level={6}>Created by:</Heading> |
| 157 | + <Heading level={6}>{value.createdBy}</Heading> |
| 158 | + </div> |
| 159 | + <div> |
| 160 | + <Heading level={6}>Modified by:</Heading> |
| 161 | + <Heading level={6}>{value.createdBy}</Heading> |
| 162 | + </div> |
| 163 | + </FormSection> |
| 164 | + )} |
| 165 | + <FormSection label="Title*" description="Enter the question"> |
| 166 | + <TextInput |
| 167 | + name="title" |
| 168 | + value={value.title} |
| 169 | + error={error?.title as string} |
| 170 | + onChange={setFieldValue} |
| 171 | + /> |
| 172 | + </FormSection> |
| 173 | + |
| 174 | + <FormSection label="Status*" description="Add status to either draft, publish or archived"> |
| 175 | + <SelectInput |
| 176 | + name="scope" |
| 177 | + options={scopeOptions} |
| 178 | + value={value.scope} |
| 179 | + keySelector={(o) => o.label} |
| 180 | + labelSelector={(o) => o.value} |
| 181 | + onChange={setFieldValue} |
| 182 | + placeholder="Select Status" |
| 183 | + error={error?.scope} |
| 184 | + /> |
| 185 | + </FormSection> |
| 186 | + |
| 187 | + <FormSection label="Partner Logo*" description="Add a cover photo, which will be displayed on top"> |
| 188 | + <RawFileInput |
| 189 | + name="image" |
| 190 | + onChange={(files) => setFieldValue(files, 'image')} |
| 191 | + variant="secondary" |
| 192 | + > |
| 193 | + Upload |
| 194 | + </RawFileInput> |
| 195 | + {value.image?.name && <p>{value.image.name}</p>} |
| 196 | + </FormSection> |
| 197 | + |
| 198 | + <div className={styles.submitBtn}> |
| 199 | + <Button name="save" onClick={handleFormSubmit} variant="primary"> |
| 200 | + {createPending || updatePending ? 'Saving' : 'Save'} |
| 201 | + </Button> |
| 202 | + </div> |
| 203 | + </Container> |
| 204 | + </Page> |
| 205 | + ); |
| 206 | +} |
| 207 | + |
| 208 | +export default PartnerForm; |
0 commit comments