Skip to content

Commit 94db2d0

Browse files
committed
chore: moved response list logic in service
1 parent 27b0f58 commit 94db2d0

File tree

2 files changed

+58
-50
lines changed

2 files changed

+58
-50
lines changed

src/Pages/App/CreateAppModal/AppClone/useDevtronCloneList.tsx

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
*/
44
import { useEffect, useMemo, useRef, useState } from 'react'
55

6-
import {
7-
abortPreviousRequests,
8-
GenericInfoCardListingProps,
9-
getIsRequestAborted,
10-
useAsync,
11-
} from '@devtron-labs/devtron-fe-common-lib'
6+
import { abortPreviousRequests, getIsRequestAborted, useAsync } from '@devtron-labs/devtron-fe-common-lib'
127

138
import { getJobs } from '@Components/Jobs/Service'
149
import { APP_TYPE } from '@Config/constants'
15-
import { getAppIconWithBackground } from '@Config/utils'
1610
import { getAppListMin } from '@Services/service'
1711

12+
import { getDevtronAppList } from '../service'
1813
import { DevtronAppCloneListProps, DevtronListResponse } from './types'
1914

2015
export const useDevtronCloneList = ({ handleCloneAppClick, isJobView }: DevtronAppCloneListProps) => {
@@ -53,11 +48,11 @@ export const useDevtronCloneList = ({ handleCloneAppClick, isJobView }: DevtronA
5348
},
5449
[],
5550
)
56-
let totalCount = 0
5751

58-
if (listResponse?.type === APP_TYPE.JOB) {
59-
totalCount = listResponse.data.result.jobCount
60-
}
52+
const { list, totalCount } = useMemo(() => {
53+
if (!listResponse) return { list: [], totalCount: 0 }
54+
return getDevtronAppList({ listResponse, handleCloneAppClick })
55+
}, [isListLoading, listResponse, handleCloneAppClick])
6156

6257
const loadMoreData = async () => {
6358
if (isLoadingMore || !listResponse) return
@@ -101,43 +96,6 @@ export const useDevtronCloneList = ({ handleCloneAppClick, isJobView }: DevtronA
10196
}
10297
}
10398

104-
const list = useMemo(() => {
105-
if (isListLoading || !listResponse) return []
106-
107-
if (listResponse.type === APP_TYPE.JOB) {
108-
const jobContainers = listResponse.data.result?.jobContainers ?? []
109-
110-
totalCount = listResponse.data.result.jobCount
111-
112-
return jobContainers.map<GenericInfoCardListingProps['list'][number]>((job) => {
113-
const { jobId, jobName, description } = job
114-
115-
return {
116-
id: String(jobId),
117-
title: jobName,
118-
description: description.description,
119-
author: description.createdBy,
120-
Icon: getAppIconWithBackground(APP_TYPE.JOB, 40),
121-
onClick: () => handleCloneAppClick({ appId: jobId, appName: jobName }),
122-
}
123-
})
124-
}
125-
const apps = listResponse.data.result ?? []
126-
127-
return apps.map<GenericInfoCardListingProps['list'][number]>((app) => {
128-
const { id, name, createdBy, description } = app
129-
130-
return {
131-
id: String(id),
132-
title: name,
133-
Icon: getAppIconWithBackground(APP_TYPE.DEVTRON_APPS, 40),
134-
onClick: () => handleCloneAppClick({ appId: id, appName: name }),
135-
author: createdBy,
136-
description,
137-
}
138-
})
139-
}, [isListLoading, listResponse])
140-
14199
return {
142100
isListLoading: isListLoading ?? getIsRequestAborted(listError),
143101
list,

src/Pages/App/CreateAppModal/service.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,58 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { post } from '@devtron-labs/devtron-fe-common-lib'
17+
import { BaseAppMetaData, GenericInfoCardListingProps, post } from '@devtron-labs/devtron-fe-common-lib'
1818

19-
import { Routes } from '@Config/constants'
19+
import { APP_TYPE, Routes } from '@Config/constants'
20+
import { getAppIconWithBackground } from '@Config/utils'
21+
22+
import { DevtronListResponse } from './AppClone/types'
2023

2124
export const createApp = (request) => post(Routes.APP, request)
25+
26+
export const getDevtronAppList = ({
27+
listResponse,
28+
handleCloneAppClick,
29+
}: {
30+
listResponse: DevtronListResponse
31+
handleCloneAppClick: (app: BaseAppMetaData) => void
32+
}) => {
33+
if (listResponse.type === APP_TYPE.JOB) {
34+
const jobContainers = listResponse.data.result?.jobContainers ?? []
35+
36+
const totalCount = listResponse.data.result.jobCount
37+
38+
return {
39+
list: jobContainers.map<GenericInfoCardListingProps['list'][number]>((job) => {
40+
const { jobId, jobName, description } = job
41+
42+
return {
43+
id: String(jobId),
44+
title: jobName,
45+
description: description.description,
46+
author: description.createdBy,
47+
Icon: getAppIconWithBackground(APP_TYPE.JOB, 40),
48+
onClick: () => handleCloneAppClick({ appId: jobId, appName: jobName }),
49+
}
50+
}),
51+
totalCount,
52+
}
53+
}
54+
const apps = listResponse.data.result ?? []
55+
56+
return {
57+
list: apps.map<GenericInfoCardListingProps['list'][number]>((app) => {
58+
const { id, name, createdBy, description } = app
59+
60+
return {
61+
id: String(id),
62+
title: name,
63+
Icon: getAppIconWithBackground(APP_TYPE.DEVTRON_APPS, 40),
64+
onClick: () => handleCloneAppClick({ appId: id, appName: name }),
65+
author: createdBy,
66+
description,
67+
}
68+
}),
69+
totalCount: apps.length,
70+
}
71+
}

0 commit comments

Comments
 (0)