-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Contacts): add basic search functionality
- Loading branch information
Showing
7 changed files
with
135 additions
and
15 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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
const GET_BILLS = "/bills"; | ||
const SEARCH_BILLS = "/bill/search"; | ||
const GET_CONTACTS = "/contacts/list"; | ||
const SEARCH_CONTACTS = "/contacts/search"; | ||
|
||
export { GET_BILLS, SEARCH_BILLS, GET_CONTACTS }; | ||
export { | ||
GET_BILLS, | ||
SEARCH_BILLS, | ||
GET_CONTACTS, | ||
SEARCH_CONTACTS, | ||
}; |
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,62 @@ | ||
import { http, delay, HttpResponse } from "msw"; | ||
import { SEARCH_CONTACTS } from "@/constants/endpoints"; | ||
import type { Contact } from "@/types/contact"; | ||
import * as contacts from "@/mocks/handlers/contacts/list"; | ||
|
||
type SearchContactsResponse = { | ||
contacts: Contact[]; | ||
} | ||
|
||
export const data: SearchContactsResponse = { | ||
contacts: contacts.data | ||
}; | ||
|
||
type SearchContactsFilter = { | ||
search_term?: string; | ||
types?: Contact['type'][]; | ||
}; | ||
|
||
const filterContacts = ( | ||
data: SearchContactsResponse, | ||
{ search_term, types }: SearchContactsFilter | ||
): SearchContactsResponse => { | ||
return { | ||
contacts: data.contacts.filter((it) => { | ||
const matchesSearchTerm = !search_term ? true : Object.entries(it).some(([, value]) => { | ||
if (typeof value === "object") { | ||
return Object.values(it).some( | ||
(innerValue) => | ||
typeof innerValue === "string" && | ||
innerValue.toLowerCase().includes(search_term.toLowerCase()) | ||
); | ||
} | ||
return ( | ||
typeof value === "string" && | ||
value.toLowerCase().includes(search_term.toLowerCase()) | ||
); | ||
}); | ||
|
||
const matchesTypes = types === undefined || types.includes(it.type); | ||
|
||
return ( | ||
matchesSearchTerm && matchesTypes | ||
); | ||
}) | ||
}; | ||
}; | ||
|
||
export const searchContacts = http.post< | ||
never, | ||
never, | ||
SearchContactsResponse, | ||
typeof SEARCH_CONTACTS | ||
>(SEARCH_CONTACTS, async ({ request }) => { | ||
|
||
await delay(500); | ||
|
||
const { filter } = await request.json(); | ||
|
||
const filteredData = filterContacts(data, filter); | ||
|
||
return HttpResponse.json(filteredData); | ||
}); |
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