-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/download-api-manifest' into task/show-upload-co…
…llections-button
- Loading branch information
Showing
18 changed files
with
527 additions
and
20 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/app/services/context/collection-permissions/CollectionPermissionsContext.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,14 @@ | ||
import { createContext } from 'react'; | ||
|
||
import { ResourcePath } from '../../../../types/resources'; | ||
|
||
interface CollectionPermissionsContext { | ||
getPermissions: (paths: ResourcePath[]) => Promise<void>; | ||
permissions: any[]; | ||
isFetching?: boolean; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-redeclare | ||
export const CollectionPermissionsContext = createContext<CollectionPermissionsContext>( | ||
{} as CollectionPermissionsContext | ||
); |
62 changes: 62 additions & 0 deletions
62
src/app/services/context/collection-permissions/CollectionPermissionsProvider.tsx
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,62 @@ | ||
import { useState } from 'react'; | ||
|
||
import { CollectionPermission, Method, ResourcePath } from '../../../../types/resources'; | ||
import { CollectionPermissionsContext } from './CollectionPermissionsContext'; | ||
|
||
const DEVX_API_URL = 'https://graphexplorerapi-staging.azurewebsites.net/api/permissions'; | ||
|
||
function getRequestsFromPaths(paths: ResourcePath[]) { | ||
const requests: any[] = []; | ||
paths.forEach(path => { | ||
const { method, url } = path; | ||
requests.push({ | ||
method: method as Method, | ||
requestUrl: url | ||
}); | ||
}); | ||
return requests; | ||
} | ||
|
||
async function getCollectionPermissions(paths: ResourcePath[]) { | ||
const response = await fetch(DEVX_API_URL, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(getRequestsFromPaths(paths)) | ||
}); | ||
const perms = await response.json(); | ||
if (!perms.results) { | ||
throw new Error('No permissions found'); | ||
} | ||
return perms; | ||
} | ||
|
||
const CollectionPermissionsProvider = ({ children }: any) => { | ||
const [permissions, setPermissions] = useState<CollectionPermission[]>([]); | ||
const [isFetching, setIsFetching] = useState(false); | ||
const [code, setCode] = useState(''); | ||
|
||
async function getPermissions(items: ResourcePath[]): Promise<void> { | ||
const hashCode = window.btoa(JSON.stringify([...items])); | ||
if (hashCode !== code) { | ||
try { | ||
setIsFetching(true); | ||
const perms = await getCollectionPermissions(items); | ||
setPermissions(perms.results); | ||
setIsFetching(false); | ||
setCode(hashCode); | ||
} catch (error) { | ||
setIsFetching(false); | ||
setPermissions([]); | ||
} | ||
} | ||
} | ||
|
||
return ( | ||
<CollectionPermissionsContext.Provider | ||
value={{ getPermissions, permissions, isFetching }}>{children}</CollectionPermissionsContext.Provider> | ||
); | ||
} | ||
|
||
export default CollectionPermissionsProvider |
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,7 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { CollectionPermissionsContext } from '../context/collection-permissions/CollectionPermissionsContext'; | ||
|
||
export const useCollectionPermissions = () => { | ||
return useContext(CollectionPermissionsContext); | ||
}; |
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
86 changes: 86 additions & 0 deletions
86
src/app/views/sidebar/resource-explorer/collection/CollectionPermissions.tsx
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,86 @@ | ||
import { DefaultButton, DetailsList, DialogFooter, Label, PrimaryButton, SelectionMode } from '@fluentui/react'; | ||
import React, { useEffect } from 'react'; | ||
import { FormattedMessage } from 'react-intl'; | ||
|
||
import { useAppSelector } from '../../../../../store'; | ||
import { componentNames } from '../../../../../telemetry'; | ||
import { PopupsComponent } from '../../../../services/context/popups-context'; | ||
import { useCollectionPermissions } from '../../../../services/hooks/useCollectionPermissions'; | ||
import { translateMessage } from '../../../../utils/translate-messages'; | ||
import { downloadToLocal, trackDownload } from '../../../common/download'; | ||
|
||
const CollectionPermissions: React.FC<PopupsComponent<null>> = (props) => { | ||
const { getPermissions, permissions, isFetching } = useCollectionPermissions(); | ||
|
||
const { collections } = useAppSelector( | ||
(state) => state | ||
); | ||
const paths = collections ? collections.find(k => k.isDefault)!.paths : []; | ||
|
||
const columns = [ | ||
{ | ||
key: 'value', name: translateMessage('Value'), fieldName: 'value', | ||
minWidth: 300, | ||
ariaLabel: translateMessage('Value') | ||
}, | ||
{ | ||
key: 'scopeType', name: translateMessage('Scope Type'), fieldName: 'scopeType', minWidth: 200, | ||
ariaLabel: translateMessage('Scope Type') | ||
} | ||
]; | ||
|
||
function downloadPermissions(): void { | ||
const filename = 'collection-permissions.json'; | ||
downloadToLocal(permissions, filename); | ||
trackDownload(filename, componentNames.DOWNLOAD_COLLECTION_PERMISSIONS_BUTTON); | ||
} | ||
|
||
useEffect(() => { | ||
if (paths.length > 0) { | ||
getPermissions(paths) | ||
} | ||
}, [paths]); | ||
|
||
if (!isFetching && permissions.length === 0) { | ||
return ( | ||
<Label style={{ | ||
display: 'flex', | ||
width: '100%', | ||
minHeight: '200px', | ||
justifyContent: 'center', | ||
alignItems: 'center' | ||
}}> | ||
<FormattedMessage id='permissions not found' /> | ||
</Label> | ||
) | ||
} | ||
|
||
if (isFetching) { | ||
return ( | ||
<Label> | ||
<FormattedMessage id={'Fetching permissions'} />... | ||
</Label> | ||
) | ||
} | ||
|
||
return ( | ||
<> | ||
<DetailsList | ||
items={permissions} | ||
columns={columns} | ||
selectionMode={SelectionMode.none} | ||
/> | ||
{permissions.length > 0 && | ||
<DialogFooter styles={{ actionsRight: { justifyContent: 'start' } }}> | ||
<PrimaryButton onClick={downloadPermissions}> | ||
<FormattedMessage id='Download permissions' /> | ||
</PrimaryButton> | ||
<DefaultButton onClick={() => props.dismissPopup()}> | ||
<FormattedMessage id='Close' /> | ||
</DefaultButton> | ||
</DialogFooter>} | ||
</> | ||
) | ||
} | ||
|
||
export default CollectionPermissions |
Oops, something went wrong.