-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(contacts): Refactor contacts to efficient data fetching
- Loading branch information
1 parent
1940833
commit ecb71cc
Showing
29 changed files
with
417 additions
and
352 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
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
18 changes: 13 additions & 5 deletions
18
phone/src/apps/contacts/components/List/SearchContacts.tsx
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 |
---|---|---|
@@ -1,17 +1,57 @@ | ||
import { atom } from 'recoil'; | ||
import { Contact } from '../../../../../typings/contact'; | ||
import { atom, selector, useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'; | ||
import { Contact, ContactEvents } from '../../../../../typings/contact'; | ||
import { fetchNui } from '../../../utils/fetchNui'; | ||
import { ServerPromiseResp } from '../../../../../typings/common'; | ||
import { isEnvBrowser } from '../../../utils/misc'; | ||
import LogDebugEvent from '../../../os/debug/LogDebugEvents'; | ||
import { BrowserContactsState } from '../utils/constants'; | ||
|
||
export const contactsState = { | ||
contacts: atom<Contact[]>({ | ||
key: 'contactsList', | ||
default: [], | ||
default: selector({ | ||
key: 'contactsListDefault', | ||
get: async () => { | ||
try { | ||
const resp = await fetchNui<ServerPromiseResp<Contact[]>>(ContactEvents.GET_CONTACTS); | ||
LogDebugEvent({ action: 'ContactsFetched', data: resp.data }); | ||
return resp.data; | ||
} catch (e) { | ||
if (isEnvBrowser()) { | ||
return BrowserContactsState; | ||
} | ||
console.error(e); | ||
return []; | ||
} | ||
}, | ||
}), | ||
}), | ||
showModal: atom<boolean>({ | ||
key: 'showModal', | ||
default: false, | ||
}), | ||
filterContacts: atom<string>({ | ||
key: 'filterContacts', | ||
filterInput: atom<string>({ | ||
key: 'filterInput', | ||
default: '', | ||
}), | ||
filteredContacts: selector({ | ||
key: 'filteredContacts', | ||
get: ({ get }) => { | ||
const filterInputVal: string = get(contactsState.filterInput); | ||
const contacts: Contact[] = get(contactsState.contacts); | ||
|
||
if (!filterInputVal) return contacts; | ||
|
||
const regExp = new RegExp(filterInputVal, 'gi'); | ||
|
||
return contacts.filter( | ||
(contact) => contact.display.match(regExp) || contact.number.match(regExp), | ||
); | ||
}, | ||
}), | ||
}; | ||
|
||
export const useSetContacts = () => useSetRecoilState(contactsState.contacts); | ||
export const useContacts = () => useRecoilState(contactsState.contacts); | ||
export const useContactsValue = () => useRecoilValue(contactsState.contacts); | ||
|
||
export const useFilteredContacts = () => useRecoilValue(contactsState.filteredContacts); | ||
|
||
export const useContactFilterInput = () => useRecoilState(contactsState.filterInput); | ||
export const useSetContactFilterInput = () => useSetRecoilState(contactsState.filterInput); |
Oops, something went wrong.