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:
Taso authored and GitHub committed 2021-02-09 09:59:54 -05:00
1 parent eaf27f37ef
commit c0509be2d1
14 files changed
+324 -453

No files matched your search

@@ -1,15 +0,0 @@
import { useRecoilState } from 'recoil';
import { ContactProps } from '../../../common/typings/contact';
import { contactsState } from './state';
interface ContactDetailProps {
contactDetail: ContactProps;
setContactDetail: (contact: object) => void;
}
export const useContactDetail = (): ContactDetailProps => {
const [ contactDetail, setContactDetail ] = useRecoilState(contactsState.contactDetail);
return { contactDetail, setContactDetail };
};
@@ -1,12 +0,0 @@
import { useRecoilState } from 'recoil';
import { contactsState } from './state';
interface ModalProps {
showContactModal: boolean;
setShowContactModal: (show: boolean) => void;
}
export const useContactModal = (): ModalProps => {
const [ showContactModal, setShowContactModal ] = useRecoilState(contactsState.showContactsModal);
return { showContactModal, setShowContactModal };
};
+19 -7
View File
@@ -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 };
};