-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add detailed view to Workspaces
- Loading branch information
1 parent
2a2d577
commit 1b56723
Showing
4 changed files
with
117 additions
and
0 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
98 changes: 98 additions & 0 deletions
98
src/smart-components/workspaces/workspacesDetailedView.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,98 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { useIntl } from 'react-intl'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { fetchWorkspaces } from '../../redux/actions/workspaces-actions'; | ||
import messages from '../../Messages'; | ||
import { ContentHeader } from '@patternfly/react-component-groups'; | ||
import { PageSection } from '@patternfly/react-core'; | ||
import { DataView, DataViewTable, DataViewTh, DataViewTrTree, useDataViewSelection } from '@patternfly/react-data-view'; | ||
import { Workspace } from '../../redux/reducers/workspaces-reducer'; | ||
import { RBACStore } from '../../redux/store'; | ||
|
||
const WorkspacesDetailedView = () => { | ||
const intl = useIntl(); | ||
const dispatch = useDispatch(); | ||
const selection = useDataViewSelection({ matchOption: (a, b) => a.id === b.id }); | ||
|
||
const { isLoading, workspaces, error } = useSelector((state: RBACStore) => state.workspacesReducer); | ||
|
||
const [hierarchicalWorkspaces, setHierarchicalWorkspaces] = useState<Workspace[]>([]); | ||
|
||
useEffect(() => { | ||
dispatch(fetchWorkspaces()); | ||
}, [dispatch]); | ||
|
||
useEffect(() => { | ||
dispatch(fetchWorkspaces()); | ||
}, [dispatch]); | ||
|
||
const mapWorkspacesToHierarchy = (workspaceData: any[]): Workspace[] => { | ||
const workspaceMap: { [key: string]: Workspace } = {}; | ||
|
||
workspaceData.forEach((ws) => { | ||
workspaceMap[ws.uuid] = { | ||
id: ws.uuid, | ||
name: ws.name, | ||
description: ws.description, | ||
children: [], | ||
}; | ||
}); | ||
|
||
const hierarchy: Workspace[] = []; | ||
workspaceData.forEach((ws) => { | ||
if (ws.parent_id) { | ||
workspaceMap[ws.parent_id]?.children?.push(workspaceMap[ws.uuid]); | ||
} else { | ||
hierarchy.push(workspaceMap[ws.uuid]); | ||
} | ||
}); | ||
|
||
return hierarchy; | ||
}; | ||
|
||
useEffect(() => { | ||
if (workspaces.length > 0) { | ||
setHierarchicalWorkspaces(mapWorkspacesToHierarchy(workspaces)); | ||
} | ||
}, [workspaces]); | ||
|
||
const buildRows = (workspaces: Workspace[]): DataViewTrTree[] => | ||
workspaces.map((workspace) => ({ | ||
row: [workspace.name, workspace.description], | ||
id: workspace.id, | ||
...(workspace.children && workspace.children.length > 0 | ||
? { | ||
children: buildRows(workspace.children), | ||
} | ||
: {}), | ||
})); | ||
|
||
const rows: DataViewTrTree[] = buildRows(hierarchicalWorkspaces); | ||
|
||
const columns: DataViewTh[] = ['HIIIRName', 'Description']; | ||
|
||
return ( | ||
<React.Fragment> | ||
<ContentHeader | ||
title={intl.formatMessage(messages.workspaces)} | ||
subtitle={intl.formatMessage(messages.workspacesSubtitle)} | ||
linkProps={{ | ||
label: intl.formatMessage(messages.workspacesLearnMore), | ||
isExternal: true, | ||
href: '#', //TODO: URL to be specified by UX team later | ||
}} | ||
/> | ||
<PageSection> | ||
{isLoading && <p>Loading...</p>} | ||
{error && <p>Error: {error}</p>} | ||
{!isLoading && !error && ( | ||
<DataView selection={selection}> | ||
<DataViewTable isTreeTable aria-label="Repositories table" ouiaId={'ouiaId'} columns={columns} rows={rows} /> | ||
</DataView> | ||
)} | ||
</PageSection> | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
export default WorkspacesDetailedView; |
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