-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW] Paginated and Filtered selects on new/edit unit (#22052)
* create endpoint to show departments not related with unit * department refactor endpoint by unit and available by unit * [NEW] Paginated and Filtered selects on new/edit unit * TS fixes Typescript pls don't hurt me * More TS fixes Co-authored-by: Rafael <rafaelblink@gmail.com>
- Loading branch information
1 parent
d1062ef
commit fdb6639
Showing
13 changed files
with
354 additions
and
78 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
7 changes: 7 additions & 0 deletions
7
client/contexts/ServerContext/endpoints/v1/livechat/department.ts
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,7 @@ | ||
export type LivechatDepartment = { | ||
GET: (params: { | ||
query: string; | ||
}) => { | ||
statuses: unknown[]; | ||
}; | ||
}; |
13 changes: 13 additions & 0 deletions
13
client/contexts/ServerContext/endpoints/v1/livechat/departmentsByUnit.ts
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,13 @@ | ||
import { ILivechatDepartment } from '../../../../../../definition/ILivechatDepartment'; | ||
import { ObjectFromApi } from '../../../../../../definition/ObjectFromApi'; | ||
|
||
export type LivechatDepartmentsByUnit = { | ||
GET: (params: { | ||
text: string; | ||
offset: number; | ||
count: number; | ||
}) => { | ||
departments: ObjectFromApi<ILivechatDepartment>[]; | ||
total: number; | ||
}; | ||
}; |
13 changes: 13 additions & 0 deletions
13
client/contexts/ServerContext/endpoints/v1/livechat/monitorsList.ts
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,13 @@ | ||
import { ILivechatMonitor } from '../../../../../../definition/ILivechatMonitor'; | ||
import { ObjectFromApi } from '../../../../../../definition/ObjectFromApi'; | ||
|
||
export type LivechatMonitorsList = { | ||
GET: (params: { | ||
text: string; | ||
offset: number; | ||
count: number; | ||
}) => { | ||
monitors: ObjectFromApi<ILivechatMonitor>[]; | ||
total: number; | ||
}; | ||
}; |
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,63 @@ | ||
import { useCallback, useState } from 'react'; | ||
|
||
import { ILivechatDepartmentRecord } from '../../../definition/ILivechatDepartmentRecord'; | ||
import { useEndpoint } from '../../contexts/ServerContext'; | ||
import { useScrollableRecordList } from '../../hooks/lists/useScrollableRecordList'; | ||
import { useComponentDidUpdate } from '../../hooks/useComponentDidUpdate'; | ||
import { RecordList } from '../../lib/lists/RecordList'; | ||
|
||
type DepartmentsListOptions = { | ||
unitId: string; | ||
filter: string; | ||
}; | ||
|
||
export const useDepartmentsList = ( | ||
options: DepartmentsListOptions, | ||
): { | ||
itemsList: RecordList<ILivechatDepartmentRecord>; | ||
initialItemCount: number; | ||
reload: () => void; | ||
loadMoreItems: (start: number, end: number) => void; | ||
} => { | ||
const [itemsList, setItemsList] = useState(() => new RecordList<ILivechatDepartmentRecord>()); | ||
const reload = useCallback(() => setItemsList(new RecordList<ILivechatDepartmentRecord>()), []); | ||
const endpoint = `livechat/departments.available-by-unit/${ | ||
options.unitId || 'none' | ||
}` as 'livechat/departments.by-unit/'; | ||
|
||
const getDepartments = useEndpoint('GET', endpoint); | ||
|
||
useComponentDidUpdate(() => { | ||
options && reload(); | ||
}, [options, reload]); | ||
|
||
const fetchData = useCallback( | ||
async (start, end) => { | ||
const { departments, total } = await getDepartments({ | ||
text: options.filter, | ||
offset: start, | ||
count: end + start, | ||
}); | ||
|
||
return { | ||
items: departments.map((department: any) => { | ||
department._updatedAt = new Date(department._updatedAt); | ||
department.label = department.name; | ||
department.value = { value: department._id, label: department.name }; | ||
return department; | ||
}), | ||
itemCount: total, | ||
}; | ||
}, | ||
[getDepartments, options.filter], | ||
); | ||
|
||
const { loadMoreItems, initialItemCount } = useScrollableRecordList(itemsList, fetchData, 25); | ||
|
||
return { | ||
reload, | ||
itemsList, | ||
loadMoreItems, | ||
initialItemCount, | ||
}; | ||
}; |
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,60 @@ | ||
import { useCallback, useState } from 'react'; | ||
|
||
import { ILivechatMonitorRecord } from '../../../definition/ILivechatMonitorRecord'; | ||
import { useEndpoint } from '../../contexts/ServerContext'; | ||
import { useScrollableRecordList } from '../../hooks/lists/useScrollableRecordList'; | ||
import { useComponentDidUpdate } from '../../hooks/useComponentDidUpdate'; | ||
import { RecordList } from '../../lib/lists/RecordList'; | ||
|
||
type MonitorsListOptions = { | ||
filter: string; | ||
}; | ||
|
||
export const useMonitorsList = ( | ||
options: MonitorsListOptions, | ||
): { | ||
itemsList: RecordList<ILivechatMonitorRecord>; | ||
initialItemCount: number; | ||
reload: () => void; | ||
loadMoreItems: (start: number, end: number) => void; | ||
} => { | ||
const [itemsList, setItemsList] = useState(() => new RecordList<ILivechatMonitorRecord>()); | ||
const reload = useCallback(() => setItemsList(new RecordList<ILivechatMonitorRecord>()), []); | ||
|
||
const endpoint = 'livechat/monitors.list'; | ||
|
||
const getMonitors = useEndpoint('GET', endpoint); | ||
|
||
useComponentDidUpdate(() => { | ||
options && reload(); | ||
}, [options, reload]); | ||
|
||
const fetchData = useCallback( | ||
async (start, end) => { | ||
const { monitors, total } = await getMonitors({ | ||
text: options.filter, | ||
offset: start, | ||
count: end + start, | ||
}); | ||
|
||
return { | ||
items: monitors.map((members: any) => { | ||
members._updatedAt = new Date(members._updatedAt); | ||
members.label = members.username; | ||
members.value = { value: members._id, label: members.username }; | ||
return members; | ||
}), | ||
itemCount: total, | ||
}; | ||
}, | ||
[getMonitors, options.filter], | ||
); | ||
|
||
const { loadMoreItems, initialItemCount } = useScrollableRecordList(itemsList, fetchData, 25); | ||
return { | ||
reload, | ||
itemsList, | ||
loadMoreItems, | ||
initialItemCount, | ||
}; | ||
}; |
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,17 @@ | ||
import { IRocketChatRecord } from './IRocketChatRecord'; | ||
|
||
|
||
export interface ILivechatDepartmentRecord extends IRocketChatRecord { | ||
_id: string; | ||
name: string; | ||
enabled: boolean; | ||
description: string; | ||
showOnRegistration: boolean; | ||
showOnOfflineForm: boolean; | ||
requestTagBeforeClosingChat: boolean; | ||
email: string; | ||
chatClosingTags: string[]; | ||
offlineMessageChannelName: string; | ||
numAgents: number; | ||
businessHourId?: string; | ||
} |
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,8 @@ | ||
export interface ILivechatMonitor { | ||
_id: string; | ||
name: string; | ||
enabled: boolean; | ||
numMonitors: number; | ||
type: string; | ||
visibility: string; | ||
} |
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,11 @@ | ||
import { IRocketChatRecord } from './IRocketChatRecord'; | ||
|
||
|
||
export interface ILivechatMonitorRecord extends IRocketChatRecord { | ||
_id: string; | ||
name: string; | ||
enabled: boolean; | ||
numMonitors: number; | ||
type: string; | ||
visibility: string; | ||
} |
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,32 @@ | ||
import { escapeRegExp } from '@rocket.chat/string-helpers'; | ||
|
||
import { | ||
LivechatDepartment, | ||
} from '../../../../../app/models/server/raw'; | ||
|
||
export const findAllDepartmentsAvailable = async (unitId, offset, count, text) => { | ||
const filterReg = new RegExp(escapeRegExp(text), 'i'); | ||
|
||
const cursor = LivechatDepartment.find({ | ||
$or: [{ ancestors: { $in: [unitId] } }, { ancestors: { $exists: false } }], | ||
...text && { name: filterReg }, | ||
|
||
}, { limit: count, offset }); | ||
|
||
const departments = await cursor.toArray(); | ||
const total = await cursor.count(); | ||
const departmentsFiltered = departments.filter((department) => !department.ancestors?.length); | ||
|
||
return { departments: departmentsFiltered, total }; | ||
}; | ||
|
||
export const findAllDepartmentsByUnit = async (unitId, offset, count) => { | ||
const cursor = LivechatDepartment.find({ | ||
ancestors: { $in: [unitId] }, | ||
}, { limit: count, offset }); | ||
|
||
const total = await cursor.count(); | ||
const departments = await cursor.toArray(); | ||
|
||
return { departments, total }; | ||
}; |
Oops, something went wrong.