forked from fastapi/full-stack-fastapi-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50ec7ad
commit 4cef3c3
Showing
10 changed files
with
386 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { | ||
Button, | ||
FormControl, | ||
FormErrorMessage, | ||
FormLabel, | ||
Input, | ||
Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalOverlay, | ||
} from "@chakra-ui/react" | ||
import { useMutation, useQueryClient } from "@tanstack/react-query" | ||
import { type SubmitHandler, useForm } from "react-hook-form" | ||
|
||
import { type ApiError, type ReferralCreate, ReferralsService } from "../../client" | ||
import useCustomToast from "../../hooks/useCustomToast" | ||
|
||
interface AddReferralProps { | ||
isOpen: boolean | ||
onClose: () => void | ||
} | ||
|
||
const AddReferral = ({ isOpen, onClose }: AddReferralProps) => { | ||
const queryClient = useQueryClient() | ||
const showToast = useCustomToast() | ||
const { | ||
register, | ||
handleSubmit, | ||
reset, | ||
formState: { errors, isSubmitting }, | ||
} = useForm<ReferralCreate>({ | ||
mode: "onBlur", | ||
criteriaMode: "all", | ||
defaultValues: { | ||
//claim_id: , | ||
//description: "", | ||
}, | ||
}) | ||
|
||
const mutation = useMutation({ | ||
mutationFn: (data: ReferralCreate) => | ||
ReferralsService.createReferral({ requestBody: data }), | ||
onSuccess: () => { | ||
showToast("Success!", "Referral created successfully.", "success") | ||
reset() | ||
onClose() | ||
}, | ||
onError: (err: ApiError) => { | ||
const errDetail = (err.body as any)?.detail | ||
showToast("Something went wrong.", `${errDetail}`, "error") | ||
}, | ||
onSettled: () => { | ||
queryClient.invalidateQueries({ queryKey: ["referrals"] }) | ||
}, | ||
}) | ||
|
||
const onSubmit: SubmitHandler<ReferralCreate> = (data) => { | ||
mutation.mutate(data) | ||
} | ||
|
||
return ( | ||
<> | ||
<Modal | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
size={{ base: "sm", md: "md" }} | ||
isCentered | ||
> | ||
<ModalOverlay /> | ||
<ModalContent as="form" onSubmit={handleSubmit(onSubmit)}> | ||
<ModalHeader>Add Referral</ModalHeader> | ||
<ModalCloseButton /> | ||
<ModalBody pb={6}> | ||
<FormControl isRequired isInvalid={!!errors.source_id}> | ||
<FormLabel htmlFor="source_id">Source ID</FormLabel> | ||
<Input | ||
id="source_id" | ||
{...register("source_id", { | ||
required: "Source ID is required.", | ||
})} | ||
placeholder="Source ID" | ||
type="text" | ||
/> | ||
{errors.source_id && ( | ||
<FormErrorMessage>{errors.source_id.message}</FormErrorMessage> | ||
)} | ||
</FormControl> | ||
{/* <FormControl mt={4}> | ||
<FormLabel htmlFor="description">Description</FormLabel> | ||
<Input | ||
id="description" | ||
{...register("description")} | ||
placeholder="Description" | ||
type="text" | ||
/> | ||
</FormControl> */} | ||
</ModalBody> | ||
|
||
<ModalFooter gap={3}> | ||
<Button variant="primary" type="submit" isLoading={isSubmitting}> | ||
Save | ||
</Button> | ||
<Button onClick={onClose}>Cancel</Button> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
</> | ||
) | ||
} | ||
|
||
export default AddReferral |
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,124 @@ | ||
import { | ||
Button, | ||
FormControl, | ||
FormErrorMessage, | ||
FormLabel, | ||
Input, | ||
Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalOverlay, | ||
} from "@chakra-ui/react" | ||
import { useMutation, useQueryClient } from "@tanstack/react-query" | ||
import { type SubmitHandler, useForm } from "react-hook-form" | ||
|
||
import { | ||
type ApiError, | ||
type ReferralPublic, | ||
type ReferralUpdate, | ||
ReferralsService, | ||
} from "../../client" | ||
import useCustomToast from "../../hooks/useCustomToast" | ||
|
||
interface EditReferralProps { | ||
referral: ReferralPublic | ||
isOpen: boolean | ||
onClose: () => void | ||
} | ||
|
||
const EditReferral = ({ referral, isOpen, onClose }: EditReferralProps) => { | ||
const queryClient = useQueryClient() | ||
const showToast = useCustomToast() | ||
const { | ||
register, | ||
handleSubmit, | ||
reset, | ||
formState: { isSubmitting, errors, isDirty }, | ||
} = useForm<ReferralUpdate>({ | ||
mode: "onBlur", | ||
criteriaMode: "all", | ||
defaultValues: referral, | ||
}) | ||
|
||
const mutation = useMutation({ | ||
mutationFn: (data: ReferralUpdate) => | ||
ReferralsService.updateReferral({ id: referral.id, requestBody: data }), | ||
onSuccess: () => { | ||
showToast("Success!", "Referral updated successfully.", "success") | ||
onClose() | ||
}, | ||
onError: (err: ApiError) => { | ||
const errDetail = (err.body as any)?.detail | ||
showToast("Something went wrong.", `${errDetail}`, "error") | ||
}, | ||
onSettled: () => { | ||
queryClient.invalidateQueries({ queryKey: ["referrals"] }) | ||
}, | ||
}) | ||
|
||
const onSubmit: SubmitHandler<ReferralUpdate> = async (data) => { | ||
mutation.mutate(data) | ||
} | ||
|
||
const onCancel = () => { | ||
reset() | ||
onClose() | ||
} | ||
|
||
return ( | ||
<> | ||
<Modal | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
size={{ base: "sm", md: "md" }} | ||
isCentered | ||
> | ||
<ModalOverlay /> | ||
<ModalContent as="form" onSubmit={handleSubmit(onSubmit)}> | ||
<ModalHeader>Edit Referral</ModalHeader> | ||
<ModalCloseButton /> | ||
<ModalBody pb={6}> | ||
<FormControl isInvalid={!!errors.source_id}> | ||
<FormLabel htmlFor="source_id">Source ID</FormLabel> | ||
<Input | ||
id="source_id" | ||
{...register("source_id", { | ||
required: "Source ID is required", | ||
})} | ||
type="text" | ||
/> | ||
{errors.source_id && ( | ||
<FormErrorMessage>{errors.source_id.message}</FormErrorMessage> | ||
)} | ||
</FormControl> | ||
{/* <FormControl mt={4}> | ||
<FormLabel htmlFor="description">Description</FormLabel> | ||
<Input | ||
id="description" | ||
{...register("description")} | ||
placeholder="Description" | ||
type="text" | ||
/> | ||
</FormControl> */} | ||
</ModalBody> | ||
<ModalFooter gap={3}> | ||
<Button | ||
variant="primary" | ||
type="submit" | ||
isLoading={isSubmitting} | ||
isDisabled={!isDirty} | ||
> | ||
Save | ||
</Button> | ||
<Button onClick={onCancel}>Cancel</Button> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
</> | ||
) | ||
} | ||
|
||
export default EditReferral |
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
Oops, something went wrong.