-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: LaunchDarkly importer UI (#2837)
Co-authored-by: Kim Gustyr <kim.gustyr@flagsmith.com>
- Loading branch information
1 parent
7964e49
commit a78eeaf
Showing
9 changed files
with
321 additions
and
4 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
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,94 @@ | ||
import { Res } from 'common/types/responses' | ||
import { Req } from 'common/types/requests' | ||
import { service } from 'common/service' | ||
|
||
export const launchDarklyService = service | ||
.enhanceEndpoints({ addTagTypes: ['launchDarklyProjectImport'] }) | ||
.injectEndpoints({ | ||
endpoints: (builder) => ({ | ||
createLaunchDarklyProjectImport: builder.mutation< | ||
Res['launchDarklyProjectImport'], | ||
Req['createLaunchDarklyProjectImport'] | ||
>({ | ||
invalidatesTags: [{ id: 'LIST', type: 'launchDarklyProjectImport' }], | ||
query: (query: Req['createLaunchDarklyProjectImport']) => ({ | ||
body: query.body, | ||
method: 'POST', | ||
url: `projects/${query.project_id}/imports/launch-darkly/`, | ||
}), | ||
}), | ||
getLaunchDarklyProjectImport: builder.query< | ||
Res['launchDarklyProjectImport'], | ||
Req['getLaunchDarklyProjectImport'] | ||
>({ | ||
providesTags: [{ id: 'LIST', type: 'launchDarklyProjectImport' }], | ||
query: (query) => ({ | ||
url: `projects/${query.project_id}/imports/launch-darkly/${query.import_id}/`, | ||
}), | ||
}), | ||
getLaunchDarklyProjectsImport: builder.query< | ||
Res['launchDarklyProjectsImport'], | ||
Req['getLaunchDarklyProjectsImport'] | ||
>({ | ||
providesTags: [{ id: 'LIST', type: 'launchDarklyProjectImport' }], | ||
query: (query) => ({ | ||
url: `projects/${query.project_id}/imports/launch-darkly/`, | ||
}), | ||
}), | ||
// END OF ENDPOINTS | ||
}), | ||
}) | ||
|
||
export async function createLaunchDarklyProjectImport( | ||
store: any, | ||
data: Req['createLaunchDarklyProjectImport'], | ||
options?: Parameters< | ||
typeof launchDarklyService.endpoints.createLaunchDarklyProjectImport.initiate | ||
>[1], | ||
) { | ||
return store.dispatch( | ||
launchDarklyService.endpoints.createLaunchDarklyProjectImport.initiate(data, options), | ||
) | ||
} | ||
export async function getLaunchDarklyProjectImport( | ||
store: any, | ||
data: Req['getLaunchDarklyProjectImport'], | ||
options?: Parameters< | ||
typeof launchDarklyService.endpoints.getLaunchDarklyProjectImport.initiate | ||
>[1], | ||
) { | ||
return store.dispatch( | ||
launchDarklyService.endpoints.getLaunchDarklyProjectImport.initiate( | ||
data, | ||
options, | ||
), | ||
) | ||
} | ||
export async function getLaunchDarklyProjectsImport( | ||
store: any, | ||
data: Req['getLaunchDarklyProjectsImport'], | ||
options?: Parameters< | ||
typeof launchDarklyService.endpoints.getLaunchDarklyProjectsImport.initiate | ||
>[1], | ||
) { | ||
return store.dispatch( | ||
launchDarklyService.endpoints.getLaunchDarklyProjectsImport.initiate( | ||
data, | ||
options, | ||
), | ||
) | ||
} | ||
// END OF FUNCTION_EXPORTS | ||
|
||
export const { | ||
useCreateLaunchDarklyProjectImportMutation, | ||
useGetLaunchDarklyProjectImportQuery, | ||
useGetLaunchDarklyProjectsImportQuery, | ||
// END OF EXPORTS | ||
} = launchDarklyService | ||
|
||
/* Usage examples: | ||
const { data, isLoading } = useGetLaunchDarklyProjectQuery({ id: 2 }, {}) //get hook | ||
const [createLaunchDarklyProjectImport, { isLoading, data, isSuccess }] = useCreateLaunchDarklyProjectImportMutation() //create hook | ||
launchDarklyService.endpoints.getLaunchDarklyProjectImport.select({id: 2})(store.getState()) //access data from any function | ||
*/ |
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
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,176 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import _data from 'common/data/base/_data' | ||
import { | ||
useCreateLaunchDarklyProjectImportMutation, | ||
useGetLaunchDarklyProjectImportQuery, | ||
} from 'common/services/useLaunchDarklyProjectImport' | ||
import AppLoader from 'components/AppLoader' | ||
|
||
type ImportPageType = { | ||
projectId: string | ||
projectName: string | ||
} | ||
|
||
const ImportPage: FC<ImportPageType> = ({ projectId, projectName }) => { | ||
const [LDKey, setLDKey] = useState<string>('') | ||
const [importId, setImportId] = useState<string>('') | ||
const [isLoading, setIsLoading] = useState<boolean>(false) | ||
const [isAppLoading, setAppIsLoading] = useState<boolean>(false) | ||
const [projects, setProjects] = useState<string>([]) | ||
const [createLaunchDarklyProjectImport, { data, isSuccess }] = | ||
useCreateLaunchDarklyProjectImportMutation() | ||
|
||
const { | ||
data: status, | ||
isSuccess: statusLoaded, | ||
refetch, | ||
} = useGetLaunchDarklyProjectImportQuery({ | ||
import_id: importId, | ||
project_id: projectId, | ||
}) | ||
|
||
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) { | ||
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) => { | ||
setIsLoading(false) | ||
setProjects(res.items) | ||
}) | ||
} | ||
|
||
const createImportLDProjects = (LDKey: string, projectId: string) => { | ||
createLaunchDarklyProjectImport({ | ||
body: { project_key: 'default', token: LDKey }, | ||
project_id: projectId, | ||
}) | ||
} | ||
|
||
return ( | ||
<> | ||
{isAppLoading && ( | ||
<div className='overlay'> | ||
<div className='title'>Importing Project</div> | ||
<AppLoader /> | ||
</div> | ||
)} | ||
<div className='mt-4'> | ||
<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) => 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='Launch Darkly Projects' | ||
items={projects} | ||
renderRow={({ name }, i) => { | ||
return ( | ||
<> | ||
<Button | ||
className='btn-project' | ||
onClick={() => | ||
openConfirm( | ||
'Import LaunchDarkly project', | ||
<div> | ||
{`Are you sure you want import ${name} to ${projectName}`} | ||
</div>, | ||
() => { | ||
createImportLDProjects(LDKey, 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 |
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 |
---|---|---|
|
@@ -18,3 +18,4 @@ | |
@import "spacing-utils"; | ||
@import "tooltips"; | ||
@import "base"; | ||
@import "overlay"; |
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,15 @@ | ||
.overlay { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
background-color: rgba(0, 0, 0, 0.544); | ||
z-index: 9999; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
.title{ | ||
color: #fff | ||
} | ||
} |
a78eeaf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-preview – ./frontend
flagsmith-frontend-preview-flagsmith.vercel.app
flagsmith-frontend-preview-git-main-flagsmith.vercel.app
flagsmith-frontend-production-native.vercel.app
a78eeaf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-staging – ./frontend
flagsmith-staging-frontend.vercel.app
flagsmith-frontend-staging-git-main-flagsmith.vercel.app
flagsmith-frontend-staging-flagsmith.vercel.app
staging.flagsmith.com
a78eeaf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./docs
docs-flagsmith.vercel.app
docs-git-main-flagsmith.vercel.app
docs.bullet-train.io
docs.flagsmith.com