Skip to content

Commit

Permalink
feat: close contact modal and reset form on success (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
secondl1ght authored Jul 4, 2024
1 parent b351f73 commit 0993db1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/views/contacts/AddContact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { zodResolver } from '@hookform/resolvers/zod';
import { Loader2 } from 'lucide-react';
import { FC } from 'react';
import { Dispatch, FC, SetStateAction } from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';

Expand Down Expand Up @@ -35,7 +35,10 @@ const formSchema = z.object({
// .email('This is not a valid email.'),
});

export const AddContact: FC<{ walletId: string }> = ({ walletId }) => {
export const AddContact: FC<{
walletId: string;
setOpenDialog: Dispatch<SetStateAction<boolean>>;
}> = ({ walletId, setOpenDialog }) => {
const { toast } = useToast();

const form = useForm<z.infer<typeof formSchema>>({
Expand All @@ -47,11 +50,13 @@ export const AddContact: FC<{ walletId: string }> = ({ walletId }) => {

const [create, { loading }] = useCreateContactMutation({
onCompleted: () => {
setOpenDialog(false);
toast({
variant: 'default',
title: 'Contact Added',
description: 'New contact has been added successfully',
});
form.reset();
},
onError: err => {
const messages = handleApolloError(err);
Expand Down
11 changes: 6 additions & 5 deletions src/views/contacts/Contacts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ const ContactCard: FC<{ contact: ContactType; cbk?: () => void }> = ({
};

export const Contacts = () => {
const [open, setOpen] = useState(false);
const [openDrawer, setOpenDrawer] = useState(false);
const [openDialog, setOpenDialog] = useState(false);

const currentContact = useContactStore(s => s.currentContact);
const setContact = useContactStore(s => s.setCurrentContact);
Expand Down Expand Up @@ -95,7 +96,7 @@ export const Contacts = () => {
<div className="flex w-full items-center justify-between">
<h1 className="text-sm font-semibold md:text-lg">Contacts</h1>
<div className="flex gap-2">
<Drawer open={open} onOpenChange={setOpen}>
<Drawer open={openDrawer} onOpenChange={setOpenDrawer}>
<DrawerTrigger asChild className="flex md:hidden">
<Button size={'icon'} variant={'outline'}>
<User className="size-4" />
Expand All @@ -112,7 +113,7 @@ export const Contacts = () => {
<ContactCard
key={c.id}
contact={c}
cbk={() => setOpen(p => !p)}
cbk={() => setOpenDrawer(p => !p)}
/>
))
) : (
Expand All @@ -131,13 +132,13 @@ export const Contacts = () => {
</DrawerContent>
</Drawer>

<Dialog>
<Dialog open={openDialog} onOpenChange={setOpenDialog}>
<DialogTrigger asChild>
<Button size={'icon'} variant={'outline'}>
<Plus className="size-4" />
</Button>
</DialogTrigger>
<AddContact walletId={value} />
<AddContact walletId={value} setOpenDialog={setOpenDialog} />
</Dialog>
</div>
</div>
Expand Down

0 comments on commit 0993db1

Please sign in to comment.