I have two different but similar functions that make an api call. I'm wondering if there is a way to curry them?
const getNameMatches = async partialName=> {
const { data: documents} = await axios.get(
`api/Documents/SearchDocument?searchString=${partialName}`,
);
const { data: clients } = await axios.get('api/Clients/GetAll');
return documents.map(document=> ({
label: document.Name,
subLabel: clients.find(c => c.id === document.clientID)?.firstName,
value: document,
}));
const getAddressMatches = async partialAddress=> {
const { data: documents} = await axios.get(
`api/Documents/SearchDocumentByAddress?searchString=${partialAddress}`,
);
const { data: clients } = await axios.get('api/Clients/GetAll');
return documents.map(document=> ({
label: document.propertyAddress,
subLabel: clients.find(c => c.id === document.clientID)?.firstName,
value: document,
}));
I combined the two functions into one function but I would want to curry them and wondering if this is possible.
The combined function:
const getMatches = async (state, partialString) => {
const tabValue = state.tabValue;
const { data } = await axios.get(
`api/Documents/SearchDocument${
tabValue === 'Address' ? 'ByAddress' : ''
}?searchString=${partialString}`,
);
const { data: clients } = await axios.get('api/Clients/GetAll');
return data.map(loan => ({
label: tabValue === 'Name' ? document.name: document.propertyAddress,
subLabel: clients.find(c => c.id === loan.clientID)?.firstName,
value: document,
}));
};
the only difference is the API URL and the label.
So you can collect them and make it generic
const getMatches = async (partialValue,type)=> {
let urlString="",label="";
if(type==='name'){
urlString="SearchDocument";
label="Name";
}else{
urlString="SearchDocumentByAddress";
label="propertyAddress";
}
const { data: documents} = await axios.get(
`api/Documents/${urlString}?searchString=${partialValue}`,
);
const { data: clients } = await axios.get('api/Clients/GetAll');
return documents.map(document=> ({
label: document[label],
subLabel: clients.find(c => c.id === document.clientID)?.firstName,
value: document,
}));
If you really want currying , you can do something like this:
const searchFor = type => {
const path = type === 'name'
? 'SearchDocument'
: 'SearchDocumentByAddress';
const prop = type === 'name'
? 'Name'
: 'propertyAddress';
return function(string){
const apiDocs = `api/Documents/${path}?searchString=${string}`;
const apiClients = `api/Clients/GetAll`;
const documents = await axios.get(apiDocs);
const clients = await axios.get(apiClients);
return documents.map(document => ({
label: document[prop],
subLabel: clients.find(c => c.id === document.clientID).firstName,
value: document
}));
}
}
Then you can call it like:
const getNameMatches = searchFor('name');
getNameMatches('foobar');
/* OR */
const getAddrMatches = searchFor('address');
getAddrMatches('foobar');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.