-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcopilot-requests.ts
More file actions
136 lines (117 loc) · 4.92 KB
/
copilot-requests.ts
File metadata and controls
136 lines (117 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import useSWR, { SWRResponse } from 'swr'
import useSWRInfinite, { SWRInfiniteResponse } from 'swr/infinite'
import { EnvironmentConfig } from '~/config'
import { xhrGetAsync, xhrPatchAsync, xhrPostAsync } from '~/libs/core'
import { buildUrl } from '~/libs/shared/lib/utils/url'
import { Sort } from '~/apps/admin/src/platform/gamification-admin/src/game-lib'
import { getPaginatedAsync, PaginatedResponse } from '~/libs/core/lib/xhr/xhr-functions/xhr.functions'
import { CopilotRequest } from '../models/CopilotRequest'
const baseUrl = `${EnvironmentConfig.API.V5}/projects`
const PAGE_SIZE = 20
/**
* Creates a CopilotRequest object by merging the provided data and its nested data,
* setting specific properties, and formatting the createdAt date.
*
* @param data - The input data object containing the properties to be merged and transformed.
* @returns A new CopilotRequest object with the transformed properties.
*/
function copilotRequestFactory(data: any): CopilotRequest {
return {
...data,
...data.data,
copilotOpportunity: undefined,
createdAt: new Date(data.createdAt),
data: undefined,
opportunity: data.copilotOpportunity?.[0],
startDate: new Date(data.data?.startDate),
}
}
export type CopilotRequestsResponse = {
data: CopilotRequest[];
hasMoreCopilotRequests: boolean;
isValidating: boolean;
size: number;
setSize: (size: number) => void;
page: number;
}
/**
* Custom hook to fetch copilot requests for a given project.
*
* @param {string} [projectId] - Optional project ID to fetch copilot requests for a specific project.
* @returns {CopilotRequestsResponse} - The response containing copilot requests.
*/
export const useCopilotRequests = (sort: Sort, projectId?: string): CopilotRequestsResponse => {
const getKey = (pageIndex: number, previousPageData: CopilotRequest[]): string | undefined => {
if (previousPageData && previousPageData.length < PAGE_SIZE) return undefined
const url = buildUrl(`${baseUrl}${projectId ? `/${projectId}` : ''}/copilots/requests`)
return `
${url}?page=${pageIndex + 1}&pageSize=${PAGE_SIZE}&sort=${sort.fieldName} ${sort.direction}
`
}
const fetcher = (
url: string,
): Promise<PaginatedResponse<CopilotRequest[]>> => getPaginatedAsync<CopilotRequest[]>(url)
.then((data: any) => (
{
...data,
data: data.data.map(copilotRequestFactory),
}
))
const {
isValidating,
data = [],
size,
setSize,
}: SWRInfiniteResponse<PaginatedResponse<CopilotRequest[]>> = useSWRInfinite(getKey, fetcher, {
revalidateOnFocus: false,
})
const latestPage = data[data.length - 1] || {}
const copilotRequests = data.flatMap(page => page.data)
const hasMoreCopilotRequests = latestPage.page + 1 < latestPage.totalPages
return { data: copilotRequests, hasMoreCopilotRequests, isValidating, setSize: (s: number) => { setSize(s) }, size, page: latestPage.page }
}
export type CopilotRequestResponse = SWRResponse<CopilotRequest, CopilotRequest>
/**
* Custom hook to fetch and manage the state of a copilot request.
*
* @param {string} requestId - The unique identifier of the copilot request.
* @returns {CopilotRequestResponse} - The response containing the copilot request data.
*/
export const useCopilotRequest = (requestId?: string): CopilotRequestResponse => {
const url = requestId && buildUrl(`${baseUrl}/copilots/requests/${requestId}`)
const fetcher = (urlp: string): Promise<CopilotRequest> => xhrGetAsync<CopilotRequest>(urlp)
.then(copilotRequestFactory)
return useSWR(url, fetcher, {
refreshInterval: 0,
revalidateOnFocus: false,
})
}
/**
* Saves a copilot request by sending a POST request to the server.
*
* @param {CopilotRequest} request - The copilot request to be saved.
* @returns {Promise<CopilotRequest>} A promise that resolves to the saved copilot request.
*/
export const saveCopilotRequest = (request: CopilotRequest)
: Promise<CopilotRequest> => {
const url = request.id
? `${baseUrl}/copilots/requests/${request.id}` : `${baseUrl}/${request.projectId}/copilots/requests`
const requestData = {
data: { ...request, id: undefined },
}
return request.id ? xhrPatchAsync(url, requestData) : xhrPostAsync(url, requestData, {})
}
/**
* Approves a copilot request by sending a POST request to the server.
*
* @param {CopilotRequest} request - The copilot request to be approved.
* @returns {Promise<CopilotRequest>} A promise that resolves to the approved copilot request.
*/
export const approveCopilotRequest = (request: CopilotRequest)
: Promise<CopilotRequest> => {
const url = `${baseUrl}/${request.projectId}/copilots/requests/${request.id}/approve`
const requestData = {
type: request.projectType,
}
return xhrPostAsync(url, requestData, {})
}