Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AMB-19] feat: close contact modal and reset form on success #8

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading