Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

refactor: create entities component for groups and users #37

Merged
merged 32 commits into from Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
993a1e7
refactor: create entities component for groups and users
Jan 5, 2021
4d64ae4
refactor: simplify groups and users components
Jan 5, 2021
00b6777
feat: header styles and theming
Jan 5, 2021
b5a51da
feat: update section headers and actions
Jan 5, 2021
17bada9
feat: redesign tables and fields
Jan 6, 2021
c958cf1
feat: update create dialogs
Jan 6, 2021
4619c32
feat: adding row click navigation
Jan 6, 2021
232a9bd
build: add material ui icons dependency
Jan 6, 2021
1e0695d
build: fix build deps
Jan 6, 2021
5f56e8c
feat: use theme switcher context instead of useTheme hook
Jan 6, 2021
2486b50
feat: useTheme within useFields
Jan 6, 2021
df84949
feat: remove roboto font load
Jan 6, 2021
b2e4d03
fix: removing inline styles on Tab icons
Jan 6, 2021
3ccdcd9
fix: revert page options
Jan 6, 2021
0bc8732
fix: refactor theme overrides
Jan 6, 2021
d8dd694
fix: default theme colours were broken after previous commit
Jan 6, 2021
52206a1
fix: add theme overrides for integration with pages that have their o…
Jan 6, 2021
a6c2182
refactor: create entity component
Jan 6, 2021
d6e8ab4
fix: entity specifix create dialog box
Jan 6, 2021
282c00d
style: arrange props alphabetically
Jan 6, 2021
5e03a80
fix: missing can search group provider check
Jan 6, 2021
e6db992
fix: fix icon component names
Jan 6, 2021
e54b8cd
fix: useMemo for theme provider value
Jan 6, 2021
1ecbbb2
fix: using icons map for provider
Jan 6, 2021
e16634a
fix: update all icons
Jan 6, 2021
153a42c
fix: entities rows interaction
Jan 6, 2021
0bfc62e
Merge branch 'new-design' of https://github.com/nearform/brokeneck in…
Jan 6, 2021
8bef9e0
Merge branch 'master' into refactor/remove-duplication-entity-components
Jan 7, 2021
aee9841
Merge branch 'master' of https://github.com/nearform/brokeneck into r…
Jan 7, 2021
bf11616
Merge branch 'refactor/remove-duplication-entity-components' of https…
Jan 7, 2021
e69f2e9
style: remove commented code
Jan 7, 2021
c15b96c
style: quote styling
Jan 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/brokeneck-react/src/components/Admin.js
Expand Up @@ -3,10 +3,9 @@ import { Switch, Route, Redirect, useLocation } from 'react-router-dom'
import { Box, CircularProgress, makeStyles } from '@material-ui/core'
import { useQuery } from 'graphql-hooks'

import { LOAD_ROOT } from '../graphql'
import { LOAD_ROOT, LOAD_USERS, LOAD_GROUPS } from '../graphql'

import Users from './Users'
import Groups from './Groups'
import Entities from './Entities'
import User from './User'
import Group from './Group'
import Navigation from './Navigation'
Expand Down Expand Up @@ -44,13 +43,23 @@ export default function Admin() {
<Redirect from="/:url*(/+)" to={pathname.slice(0, -1)} />
<Redirect from="/" exact to="/users" />
<Route path="/users" exact>
<Users />
<Entities
entityName="User"
entitiesName="Users"
entitiesKey="users"
query={LOAD_USERS}
/>
RudyRed marked this conversation as resolved.
Show resolved Hide resolved
</Route>
<Route path="/users/:userId">
{({ match }) => <User userId={match.params.userId} />}
</Route>
<Route path="/groups" exact>
<Groups />
<Entities
entityName="Group"
entitiesName="Groups"
entitiesKey="groups"
query={LOAD_GROUPS}
/>
</Route>
<Route path="/groups/:groupId">
{({ match }) => <Group groupId={match.params.groupId} />}
Expand Down
164 changes: 164 additions & 0 deletions packages/brokeneck-react/src/components/Entities.js
@@ -0,0 +1,164 @@
import React from 'react'
import T from 'prop-types'
import {
Box,
Button,
CircularProgress,
IconButton,
Link,
makeStyles,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
Typography
} from '@material-ui/core'
import { Link as RouterLink, useRouteMatch } from 'react-router-dom'
import { useQuery } from 'graphql-hooks'
import startCase from 'lodash.startcase'

import useCreateUserDialog from '../hooks/useCreateUserDialog'
import useFields, { TYPE_NAMES } from '../hooks/useFields'
import usePagination from '../hooks/usePagination'
import useSearch from '../hooks/useSearch'

import Square from './Square'

const useStyles = makeStyles(theme => ({
spacing: {
display: 'flex',
alignItems: 'center',
'& > * + *': {
marginLeft: theme.spacing(2)
}
},
right: {
marginLeft: 'auto'
}
}))

export default function Entities({
query,
entityName,
entitiesName,
entitiesKey
}) {
const match = useRouteMatch()
const classes = useStyles()
const fields = useFields(entityName)
const { search, Search } = useSearch()

const {
pageSize,
currentToken,
useUpdateToken,
useTablePagination
} = usePagination({ pageSizeOptions: [2, 3, 4] }) // TODO: temp small page sizes for testing
// console.log(fields, 'fields')
// console.log(query(fields.all), 'query')
const { data, loading, refetch: loadEntities } = useQuery(query(fields.all), {
variables: {
pageNumber: currentToken,
pageSize,
search
}
})

const [dialog, openDialog] = useCreateUserDialog(loadEntities)
RudyRed marked this conversation as resolved.
Show resolved Hide resolved
// console.log(data, 'data')
// console.log(entitiesKey, 'entitiesKey')
useUpdateToken(data?.[entitiesKey].nextPage)
// console.log('this')
const tablePagination = useTablePagination(data?.[entitiesKey])

return (
<>
{dialog}
<Square mb={3}>
<Box className={classes.spacing} mb={3}>
<Button variant="contained" color="primary" onClick={openDialog}>
{`Create ${entityName}`}
</Button>
{Search}
<div className={classes.right}>
<IconButton onClick={loadEntities} title={`reload ${entitiesName}`}>
<Typography>
<span role="img" aria-label={`reload ${entitiesName}`}>
🔃
</span>
</Typography>
</IconButton>
</div>
</Box>
<TableContainer>
<Table>
<TableHead>
<TableRow>
{fields.all.map(field => (
<TableCell
align={
fields.metadata[field].type === 'checkbox'
? 'center'
: undefined
}
key={field}
>
{startCase(field)}
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{data?.[entitiesKey].data.map(user => (
RudyRed marked this conversation as resolved.
Show resolved Hide resolved
<TableRow key={user[fields.id]}>
{fields.all.map((field, index) => (
<TableCell key={field}>
{!index ? (
<Link
color="secondary"
component={RouterLink}
to={`${match.url}/${user[fields.id]}`}
>
<Typography>
{fields.format(field, user[field])}
</Typography>
</Link>
) : (
<Typography
align={
fields.isType(field, TYPE_NAMES.Boolean)
? 'center'
: undefined
}
>
{fields.format(field, user[field])}
</Typography>
)}
</TableCell>
))}
</TableRow>
))}
{loading && (
<TableRow>
<TableCell colSpan={fields.all.length}>
<CircularProgress />
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</TableContainer>
{tablePagination}
</Square>
</>
)
}

Entities.propTypes = {
query: T.func.isRequired,
entityName: T.string.isRequired,
entitiesName: T.string.isRequired,
entitiesKey: T.string.isRequired
}