-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: API Permissions and Collections redesign (#3391)
- Loading branch information
Showing
38 changed files
with
1,281 additions
and
469 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
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
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 { CollectionPermission, ResourcePath } from '../../../../types/resources'; | ||
|
||
interface CollectionPermissionsContext { | ||
getPermissions: (paths: ResourcePath[]) => Promise<void>; | ||
permissions?: { [key: string]: CollectionPermission[] }; | ||
isFetching?: boolean; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-redeclare | ||
export const CollectionPermissionsContext = createContext<CollectionPermissionsContext>( | ||
{} as CollectionPermissionsContext | ||
); |
91 changes: 91 additions & 0 deletions
91
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,91 @@ | ||
import { ReactNode, useMemo, useState } from 'react'; | ||
|
||
import { CollectionPermission, Method, ResourcePath } from '../../../../types/resources'; | ||
import { | ||
getScopesFromPaths, getVersionsFromPaths, scopeOptions | ||
} from '../../../views/sidebar/resource-explorer/collection/collection.util'; | ||
import { CollectionPermissionsContext } from './CollectionPermissionsContext'; | ||
import { useAppSelector } from '../../../../store'; | ||
|
||
interface CollectionRequest { | ||
method: Method; | ||
requestUrl: string; | ||
} | ||
|
||
function getRequestsFromPaths(paths: ResourcePath[], version: string, scope: string) { | ||
const requests: CollectionRequest[] = []; | ||
paths.forEach(path => { | ||
const { method, url } = path; | ||
const pathScope = path.scope ?? scopeOptions[0].key; | ||
if (version === path.version && scope === pathScope) { | ||
requests.push({ | ||
method: method as Method, | ||
requestUrl: url | ||
}); | ||
} | ||
}); | ||
return requests; | ||
} | ||
|
||
async function getCollectionPermissions(permissionsUrl: string, paths: ResourcePath[]): | ||
Promise<{ [key: string]: CollectionPermission[] }> { | ||
const versions = getVersionsFromPaths(paths); | ||
const scopes = getScopesFromPaths(paths); | ||
const collectionPermissions: { [key: string]: CollectionPermission[] } = {}; | ||
|
||
for (const version of versions) { | ||
for (const scope of scopes) { | ||
const requestPaths = getRequestsFromPaths(paths, version, scope); | ||
if (requestPaths.length === 0) { | ||
continue; | ||
} | ||
const url = `${permissionsUrl}?version=${version}&scopeType=${scope}`; | ||
const response = await fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(requestPaths) | ||
}); | ||
const perms = await response.json(); | ||
collectionPermissions[`${version}-${scope}`] = (perms.results) ? perms.results : []; | ||
} | ||
} | ||
return collectionPermissions; | ||
} | ||
|
||
const CollectionPermissionsProvider = ({ children }: { children: ReactNode }) => { | ||
const { baseUrl } = useAppSelector((state) => state.devxApi); | ||
const [permissions, setPermissions] = useState<{ [key: string]: CollectionPermission[] } | undefined>(undefined); | ||
const [isFetching, setIsFetching] = useState(false); | ||
const [code, setCode] = useState(''); | ||
|
||
const getPermissions = async (items: ResourcePath[]): Promise<void> => { | ||
const hashCode = window.btoa(JSON.stringify([...items])); | ||
if (hashCode !== code) { | ||
try { | ||
setIsFetching(true); | ||
const perms = await getCollectionPermissions(`${baseUrl}/permissions`, items); | ||
setPermissions(perms); | ||
setCode(hashCode); | ||
} catch (error) { | ||
setPermissions(undefined); | ||
} finally { | ||
setIsFetching(false); | ||
} | ||
} | ||
}; | ||
|
||
const contextValue = useMemo( | ||
() => ({ getPermissions, permissions, isFetching }), | ||
[getPermissions, permissions, isFetching] | ||
); | ||
|
||
return ( | ||
<CollectionPermissionsContext.Provider value={contextValue}> | ||
{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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,66 @@ | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { Collection, ResourcePath } from '../../../types/resources'; | ||
|
||
const initialState: Collection[] = []; | ||
interface CollectionsState { | ||
collections: Collection[]; | ||
saved: boolean; | ||
} | ||
|
||
const initialState: CollectionsState = { | ||
collections: [], | ||
saved: false | ||
}; | ||
|
||
const collections = createSlice({ | ||
name: 'collections', | ||
initialState, | ||
reducers: { | ||
createCollection: (state, action: PayloadAction<Collection>) => { | ||
state.push(action.payload); | ||
return state | ||
state.collections.push(action.payload); | ||
state.saved = false; | ||
}, | ||
addResourcePaths:(state, action: PayloadAction<ResourcePath[]>) => { | ||
const index = state.findIndex(collection => collection.isDefault); | ||
addResourcePaths: (state, action: PayloadAction<ResourcePath[]>) => { | ||
const index = state.collections.findIndex(collection => collection.isDefault); | ||
if (index > -1) { | ||
state[index].paths.push(...action.payload) | ||
state.collections[index].paths.push(...action.payload); | ||
state.saved = false; | ||
} | ||
}, | ||
updateResourcePaths: (state, action: PayloadAction<ResourcePath[]>) => { | ||
const collectionIndex = state.collections.findIndex(k => k.isDefault); | ||
if (collectionIndex > -1) { | ||
state.collections[collectionIndex] = { | ||
...state.collections[collectionIndex], | ||
paths: action.payload | ||
}; | ||
state.saved = true; | ||
} | ||
}, | ||
removeResourcePaths: (state, action: PayloadAction<ResourcePath[]>)=>{ | ||
const index = state.findIndex(collection => collection.isDefault); | ||
if(index > -1) { | ||
const defaultResourcePaths = [...state[index].paths]; | ||
action.payload.forEach((resourcePath: ResourcePath)=>{ | ||
const delIndex = defaultResourcePaths.findIndex(p=>p.key === resourcePath.key) | ||
removeResourcePaths: (state, action: PayloadAction<ResourcePath[]>) => { | ||
const index = state.collections.findIndex(collection => collection.isDefault); | ||
if (index > -1) { | ||
const defaultResourcePaths = [...state.collections[index].paths]; | ||
action.payload.forEach((resourcePath: ResourcePath) => { | ||
const delIndex = defaultResourcePaths.findIndex(p => p.key === resourcePath.key); | ||
if (delIndex > -1) { | ||
defaultResourcePaths.splice(delIndex, 1) | ||
defaultResourcePaths.splice(delIndex, 1); | ||
} | ||
}) | ||
state[index].paths = defaultResourcePaths; | ||
}); | ||
state.collections[index].paths = defaultResourcePaths; | ||
state.saved = false; | ||
} | ||
}, | ||
resetSaveState: (state) => { | ||
state.saved = false; | ||
} | ||
} | ||
}) | ||
}); | ||
|
||
export const {createCollection, addResourcePaths, removeResourcePaths} = collections.actions | ||
export const | ||
{ createCollection, | ||
addResourcePaths, | ||
updateResourcePaths, | ||
removeResourcePaths, | ||
resetSaveState } = collections.actions; | ||
|
||
export default collections.reducer | ||
export default collections.reducer; |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export const searchBoxStyles: any = () => ({ | ||
root: { | ||
width: '97%' | ||
width: '100%' | ||
}, | ||
field: [ | ||
{ | ||
|
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 |
---|---|---|
|
@@ -76,5 +76,4 @@ export const ResourceExplorer = (props?: any) => { | |
return ( | ||
<LazyResourceExplorer {...props} /> | ||
) | ||
} | ||
|
||
} |
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
Oops, something went wrong.