mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
Refactored Contacts to use Routes and not a Modal (#80)
The new route allows for other components to directly route to contacts/:id, where id is the contact id for a users given contacts. It also adds a getContact function accessible by the useConfig hook in case a contact needs to be retrieved using a contact id.
This commit is contained in:
14 files changed
+324
-453
No files matched your search
@@ -1,19 +1,31 @@
|
||||
import { useRecoilValue } from 'recoil';
|
||||
import { contactsState } from './state';
|
||||
|
||||
import { ContactProps } from '../../../common/typings/contact';
|
||||
import { Contact } from '../../../common/typings/contact';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
interface IUseContacts {
|
||||
contacts: ContactProps[]
|
||||
contacts: Contact[];
|
||||
getDisplayByNumber: (number: string) => string;
|
||||
getContact: (id: number) => Contact;
|
||||
}
|
||||
|
||||
export const useContacts = (): IUseContacts => {
|
||||
const contacts = useRecoilValue(contactsState.contacts);
|
||||
const getDisplayByNumber = useCallback((number: string) => {
|
||||
const found = contacts.find((contact) => contact.number === number)
|
||||
return found ? found.display : number
|
||||
}, [contacts])
|
||||
return { contacts, getDisplayByNumber };
|
||||
const getDisplayByNumber = useCallback(
|
||||
(number: string) => {
|
||||
const found = contacts.find((contact) => contact.number === number);
|
||||
return found ? found.display : number;
|
||||
},
|
||||
[contacts]
|
||||
);
|
||||
const getContact = useCallback(
|
||||
(id: number): Contact | null => {
|
||||
for (const contact of contacts) {
|
||||
if (contact.id === id) return contact;
|
||||
}
|
||||
},
|
||||
[contacts]
|
||||
);
|
||||
return { contacts, getDisplayByNumber, getContact };
|
||||
};
|
||||
Reference in new issue
Block a user