-
-
Notifications
You must be signed in to change notification settings - Fork 278
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
deaaf72
commit 2446b26
Showing
9 changed files
with
165 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useContacts } from '@/api/hooks/useContacts'; | ||
import { EditContactForm } from '@/components/Contacts/EditContactForm'; | ||
import { Drawer, DrawerContent, DrawerHeader, DrawerTitle } from '@/components/ui/drawer'; | ||
import { useEffect } from 'react'; | ||
import { useNavigate, useParams } from 'react-router-dom'; | ||
|
||
export const EditContactView = () => { | ||
const navigate = useNavigate(); | ||
|
||
const handleBack = () => { | ||
navigate(-1); | ||
}; | ||
|
||
const { contactId } = useParams<{ contactId: string }>(); | ||
const [contacts, , isLoading] = useContacts(); | ||
console.log({ contacts, isLoading }); | ||
const contact = contacts.find((contact) => contact.id === parseInt(contactId ?? '', 10)); | ||
|
||
useEffect(() => { | ||
if (!contact && !isLoading) { | ||
handleBack(); | ||
} | ||
}, [contact, handleBack, isLoading]); | ||
|
||
return ( | ||
<Drawer modal open onClose={handleBack}> | ||
<DrawerContent> | ||
<DrawerHeader> | ||
<DrawerTitle className="text-primary">Update contact</DrawerTitle> | ||
</DrawerHeader> | ||
|
||
<EditContactForm onContactUpdated={handleBack} contact={contact} /> | ||
</DrawerContent> | ||
</Drawer> | ||
); | ||
}; |
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,79 @@ | ||
import { updateContact } from '@/api/contacts'; | ||
import { queryClient } from '@/Providers'; | ||
import { Contact } from '../../../../shared/Types'; | ||
import { useState } from 'react'; | ||
import { handleClientError } from '@/utils/errors'; | ||
import { Button } from '../ui/button'; | ||
import { Input } from '../Input'; | ||
|
||
interface NewContactFormProps { | ||
contact?: Contact; | ||
onContactUpdated: () => void; | ||
} | ||
|
||
export const EditContactForm = ({ onContactUpdated, contact }: NewContactFormProps) => { | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
const form = event.currentTarget; | ||
const formData = new FormData(form); | ||
const name = formData.get('name') as string; | ||
const phoneNumber = formData.get('phone_number') as string; | ||
|
||
if (!name || !phoneNumber || !contact) { | ||
return; | ||
} | ||
|
||
try { | ||
const { payload } = await updateContact({ | ||
...contact, | ||
name, | ||
phone_number: phoneNumber, | ||
}); | ||
|
||
onContactUpdated(); | ||
|
||
/** | ||
* Update the cache with the new contact. | ||
*/ | ||
queryClient.setQueryData(['contacts'], (contacts: { payload: Contact[] }) => { | ||
return { | ||
...contacts, | ||
payload: contacts.payload.map((contact) => { | ||
if (contact.phone_number === phoneNumber) { | ||
return payload; | ||
} | ||
|
||
return contact; | ||
}), | ||
}; | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
setError(handleClientError(error)); | ||
} | ||
}; | ||
|
||
if (!contact) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<form | ||
className="flex flex-col gap-2 p-4" | ||
onSubmit={handleSubmit} | ||
onFocus={() => setError(null)} | ||
> | ||
<Input name="name" placeholder="Name" defaultValue={contact.name} /> | ||
<Input name="phone_number" placeholder="Phone number" defaultValue={contact.phone_number} /> | ||
|
||
{error && <div className="text-red-500">{error}</div>} | ||
|
||
<Button type="submit" variant="outline" className="text-primary"> | ||
Update contact | ||
</Button> | ||
</form> | ||
); | ||
}; |
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,8 @@ | ||
import { InputHTMLAttributes } from 'react'; | ||
|
||
interface InputProps extends InputHTMLAttributes<HTMLInputElement> { | ||
isDisabled?: boolean; | ||
} | ||
export const Input = (props: InputProps) => { | ||
return <input className="bg-secondary px-4 py-2 rounded-sm text-primary" {...props} />; | ||
}; |
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