Skip to content

Commit

Permalink
feat: added new props to AutoCompleteAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandernsilva committed Oct 13, 2023
1 parent 649fd14 commit 8217123
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
27 changes: 11 additions & 16 deletions apps/meteor/client/components/AutoCompleteAgent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,38 @@ type AutoCompleteAgentProps = {
value: string;
error?: string;
placeholder?: string;
onChange: (value: string) => void;
haveAll?: boolean;
haveNoAgentsSelectedOption?: boolean;
excludeId?: string;
showIdleAgents?: boolean;
onlyAvailable?: boolean;
onChange: (value: string) => void;
};

const AutoCompleteAgent = ({
value,
error,
placeholder,
onChange,
haveAll = false,
haveNoAgentsSelectedOption = false,
excludeId,
showIdleAgents = false,
onlyAvailable = false,
onChange,
}: AutoCompleteAgentProps): ReactElement => {
const [agentsFilter, setAgentsFilter] = useState<string>('');

const debouncedAgentsFilter = useDebouncedValue(agentsFilter, 500);

const { itemsList: AgentsList, loadMoreItems: loadMoreAgents } = useAgentsList(
useMemo(
() => ({ text: debouncedAgentsFilter, haveAll, haveNoAgentsSelectedOption }),
[debouncedAgentsFilter, haveAll, haveNoAgentsSelectedOption],
() => ({ text: debouncedAgentsFilter, onlyAvailable, haveAll, haveNoAgentsSelectedOption, excludeId, showIdleAgents }),
[debouncedAgentsFilter, excludeId, haveAll, haveNoAgentsSelectedOption, onlyAvailable, showIdleAgents],
),
);

const { phase: agentsPhase, itemCount: agentsTotal, items: agentsItems } = useRecordList(AgentsList);

const sortedByName = agentsItems.sort((a, b) => {
if (a.label > b.label) {
return 1;
}
if (a.label < b.label) {
return -1;
}

return 0;
});

return (
<PaginatedSelectFiltered
value={value}
Expand All @@ -57,7 +52,7 @@ const AutoCompleteAgent = ({
flexShrink={0}
filter={agentsFilter}
setFilter={setAgentsFilter as (value: string | number | undefined) => void}
options={sortedByName}
options={agentsItems}
data-qa='autocomplete-agent'
endReached={
agentsPhase === AsyncStatePhase.LOADING ? (): void => undefined : (start): void => loadMoreAgents(start, Math.min(50, agentsTotal))
Expand Down
15 changes: 11 additions & 4 deletions apps/meteor/client/components/Omnichannel/hooks/useAgentsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ type AgentsListOptions = {
text: string;
haveAll: boolean;
haveNoAgentsSelectedOption: boolean;
excludeId?: string;
showIdleAgents?: boolean;
onlyAvailable?: boolean;
};

type AgentOption = { value: string; label: string; _updatedAt: Date; _id: string };
Expand All @@ -26,6 +29,7 @@ export const useAgentsList = (
const reload = useCallback(() => setItemsList(new RecordList<AgentOption>()), []);

const getAgents = useEndpoint('GET', '/v1/livechat/users/agent');
const { text, onlyAvailable = false, showIdleAgents = false, excludeId, haveAll, haveNoAgentsSelectedOption } = options;

useComponentDidUpdate(() => {
options && reload();
Expand All @@ -34,7 +38,10 @@ export const useAgentsList = (
const fetchData = useCallback(
async (start, end) => {
const { users: agents, total } = await getAgents({
...(options.text && { text: options.text }),
...(text && { text }),
...(excludeId && { excludeId }),
showIdleAgents,
onlyAvailable,
offset: start,
count: end + start,
sort: `{ "name": 1 }`,
Expand All @@ -50,15 +57,15 @@ export const useAgentsList = (
return agentOption;
});

options.haveAll &&
haveAll &&
items.unshift({
label: t('All'),
value: 'all',
_updatedAt: new Date(),
_id: 'all',
});

options.haveNoAgentsSelectedOption &&
haveNoAgentsSelectedOption &&
items.unshift({
label: t('Empty_no_agent_selected'),
value: 'no-agent-selected',
Expand All @@ -71,7 +78,7 @@ export const useAgentsList = (
itemCount: total + 1,
};
},
[getAgents, options.haveAll, options.haveNoAgentsSelectedOption, options.text, t],
[excludeId, getAgents, haveAll, haveNoAgentsSelectedOption, onlyAvailable, showIdleAgents, t, text],
);

const { loadMoreItems, initialItemCount } = useScrollableRecordList(itemsList, fetchData, 25);
Expand Down
4 changes: 3 additions & 1 deletion packages/rest-typings/src/v1/omnichannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3404,7 +3404,9 @@ export type OmnichannelEndpoints = {
};

'/v1/livechat/users/agent': {
GET: (params: PaginatedRequest<{ text?: string; onlyAvailable?: boolean }>) => PaginatedResult<{
GET: (
params: PaginatedRequest<{ text?: string; onlyAvailable?: boolean; excludeId?: string; showIdleAgents?: boolean }>,
) => PaginatedResult<{
users: (ILivechatAgent & { departments: string[] })[];
}>;
POST: (params: LivechatUsersManagerPOSTProps) => { success: boolean };
Expand Down

0 comments on commit 8217123

Please sign in to comment.