-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathImportPage.tsx
199 lines (189 loc) · 6.05 KB
/
ImportPage.tsx
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import React, { FC, useEffect, useState } from 'react'
import _data from 'common/data/base/_data'
import {
useCreateLaunchDarklyProjectImportMutation,
useGetLaunchDarklyProjectImportQuery,
} from 'common/services/useLaunchDarklyProjectImport'
import AppLoader from 'components/AppLoader'
import InfoMessage from 'components/InfoMessage'
import Input from 'components/base/forms/Input'
import Utils from 'common/utils/utils'
import Button from 'components/base/forms/Button'
import PanelSearch from 'components/PanelSearch'
type ImportPageType = {
projectId: string
projectName: string
}
const ImportPage: FC<ImportPageType> = ({ projectId, projectName }) => {
const [LDKey, setLDKey] = useState<string>('')
const [importId, setImportId] = useState<number>()
const [isLoading, setIsLoading] = useState<boolean>(false)
const [isAppLoading, setAppIsLoading] = useState<boolean>(false)
const [projects, setProjects] = useState<{ key: string; name: string }[]>([])
const [createLaunchDarklyProjectImport, { data, isSuccess }] =
useCreateLaunchDarklyProjectImportMutation()
const {
data: status,
isSuccess: statusLoaded,
refetch,
} = useGetLaunchDarklyProjectImportQuery(
{
import_id: `${importId}`,
project_id: projectId,
},
{ skip: !importId },
)
useEffect(() => {
const checkImportStatus = async () => {
setAppIsLoading(true)
const intervalId = setInterval(async () => {
await refetch()
if (statusLoaded && status && status.status.result === 'success') {
clearInterval(intervalId)
setAppIsLoading(false)
window.location.reload()
}
}, 1000)
}
if (statusLoaded) {
checkImportStatus()
}
}, [statusLoaded, status, refetch])
useEffect(() => {
if (isSuccess && data?.id) {
setImportId(data.id)
refetch()
}
}, [isSuccess, data, refetch])
const getProjectList = (LDKey: string) => {
setIsLoading(true)
_data
.get(`https://app.launchdarkly.com/api/v2/projects`, '', {
'Authorization': LDKey,
})
.then((res: { items: { key: string; name: string }[] }) => {
setIsLoading(false)
setProjects(res.items)
})
}
const createImportLDProjects = (
LDKey: string,
key: string,
projectId: string,
) => {
createLaunchDarklyProjectImport({
body: { project_key: key, token: LDKey },
project_id: projectId,
})
}
return (
<>
{isAppLoading && (
<div className='overlay'>
<div className='title'>Importing Project</div>
<AppLoader />
</div>
)}
<div className='mt-4'>
<InfoMessage>
Import operations will overwrite existing environments and flags in
your project.
</InfoMessage>
<h5>Import LaunchDarkly Projects</h5>
<label>Set LaunchDarkly key</label>
<FormGroup>
<Row className='align-items-start col-md-8'>
<Flex className='ml-0'>
<Input
value={LDKey}
name='ldkey'
onChange={(e: InputEvent) =>
setLDKey(Utils.safeParseEventValue(e))
}
type='text'
placeholder='My LaunchDarkly key'
/>
</Flex>
<Button
id='save-proj-btn'
disabled={!LDKey}
className='ml-3'
onClick={() => getProjectList(LDKey)}
>
{'Next'}
</Button>
</Row>
</FormGroup>
{isLoading ? (
<div className='text-center'>
<Loader />
</div>
) : (
projects.length > 0 && (
<div>
<FormGroup>
<PanelSearch
id='projects-list'
className='no-pad panel-projects'
listClassName='row mt-n2 gy-4'
title='LaunchDarkly Projects'
items={projects}
renderRow={(
{ key, name }: { key: string; name: string },
i: number,
) => {
return (
<>
<Button
className='btn-project'
onClick={() =>
openConfirm(
'Import LaunchDarkly project',
<span>
Flagsmith will import {<strong>{name}</strong>}{' '}
to {<strong>{projectName}</strong>}. Are you
sure?
</span>,
() => {
createImportLDProjects(LDKey, key, projectId)
},
() => {
return
},
)
}
>
<Row className='flex-nowrap'>
<h2
style={{
backgroundColor: Utils.getProjectColour(i),
}}
className='btn-project-letter mb-0'
>
{name[0]}
</h2>
<div className='font-weight-medium btn-project-title'>
{name}
</div>
</Row>
</Button>
</>
)
}}
renderNoResults={
<div>
<Row>
<div className='font-weight-medium'>No Projects</div>
</Row>
</div>
}
/>
</FormGroup>
</div>
)
)}
</div>
</>
)
}
export default ImportPage