-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use react hook form for document forms (#1580)
* refactor: use react hook form for document forms * refactor: fix tests * refactor: apply PR requested changes
- Loading branch information
Showing
14 changed files
with
379 additions
and
322 deletions.
There are no files selected for viewing
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
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
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
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
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
111 changes: 111 additions & 0 deletions
111
src/components/item/form/document/DocumentCreateForm.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,111 @@ | ||
import { FormProvider, useForm } from 'react-hook-form'; | ||
|
||
import { Box, DialogActions, DialogContent, DialogTitle } from '@mui/material'; | ||
|
||
import { | ||
DiscriminatedItem, | ||
DocumentItemExtraFlavor, | ||
ItemGeolocation, | ||
ItemType, | ||
buildDocumentExtra, | ||
} from '@graasp/sdk'; | ||
import { COMMON } from '@graasp/translations'; | ||
import { Button } from '@graasp/ui'; | ||
|
||
import CancelButton from '@/components/common/CancelButton'; | ||
import { useBuilderTranslation, useCommonTranslation } from '@/config/i18n'; | ||
import { mutations } from '@/config/queryClient'; | ||
import { | ||
ITEM_FORM_CONFIRM_BUTTON_ID, | ||
ITEM_FORM_DOCUMENT_TEXT_ID, | ||
} from '@/config/selectors'; | ||
import { BUILDER } from '@/langs/constants'; | ||
|
||
import { ItemNameField } from '../ItemNameField'; | ||
import { | ||
DocumentContentForm, | ||
DocumentExtraFormInputs, | ||
} from './DocumentContentForm'; | ||
import { DocumentFlavorSelect } from './DocumentFlavorSelect'; | ||
|
||
type Props = { | ||
onClose: () => void; | ||
parentId?: DiscriminatedItem['id']; | ||
geolocation?: Pick<ItemGeolocation, 'lat' | 'lng'>; | ||
previousItemId?: DiscriminatedItem['id']; | ||
}; | ||
|
||
type Inputs = { | ||
name: string; | ||
isRaw: boolean; | ||
flavor: `${DocumentItemExtraFlavor}`; | ||
} & DocumentExtraFormInputs; | ||
|
||
export function DocumentCreateForm({ | ||
parentId, | ||
geolocation, | ||
previousItemId, | ||
onClose, | ||
}: Props): JSX.Element { | ||
const { t: translateBuilder } = useBuilderTranslation(); | ||
const { t: translateCommon } = useCommonTranslation(); | ||
const { mutateAsync: createItem } = mutations.usePostItem(); | ||
|
||
const methods = useForm<Inputs>({ | ||
defaultValues: { flavor: DocumentItemExtraFlavor.None }, | ||
}); | ||
const { | ||
reset, | ||
handleSubmit, | ||
formState: { isValid, isSubmitted }, | ||
} = methods; | ||
|
||
async function onSubmit(data: Inputs) { | ||
try { | ||
await createItem({ | ||
type: ItemType.DOCUMENT, | ||
name: data.name, | ||
extra: buildDocumentExtra({ | ||
content: data.content, | ||
flavor: data.flavor, | ||
isRaw: data.isRaw, | ||
}), | ||
parentId, | ||
geolocation, | ||
previousItemId, | ||
}); | ||
onClose(); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
return ( | ||
<Box component="form" height="100%" onSubmit={handleSubmit(onSubmit)}> | ||
<DialogTitle> | ||
{translateBuilder(BUILDER.CREATE_NEW_ITEM_DOCUMENT_TITLE)} | ||
</DialogTitle> | ||
<FormProvider {...methods}> | ||
<DialogContent> | ||
<ItemNameField required /> | ||
<DocumentFlavorSelect /> | ||
<DocumentContentForm | ||
documentItemId={ITEM_FORM_DOCUMENT_TEXT_ID} | ||
onChange={(v) => reset({ content: v })} | ||
placeholder={translateBuilder(BUILDER.TEXT_EDITOR_PLACEHOLDER)} | ||
/> | ||
</DialogContent> | ||
<DialogActions> | ||
<CancelButton onClick={onClose} /> | ||
<Button | ||
id={ITEM_FORM_CONFIRM_BUTTON_ID} | ||
type="submit" | ||
disabled={isSubmitted && !isValid} | ||
> | ||
{translateCommon(COMMON.SAVE_BUTTON)} | ||
</Button> | ||
</DialogActions> | ||
</FormProvider> | ||
</Box> | ||
); | ||
} |
Oops, something went wrong.