-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
62 changed files
with
2,641 additions
and
2,078 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": "src" | ||
}, | ||
"include": ["src"] | ||
} |
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
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,110 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import { useStoreAPIData } from "redux/hooks"; | ||
import { processPagination, pathToArray } from "redux/reducers/api"; | ||
import { DEFAULT_PAGE_SIZE, PAGE_SIZES } from "utils/constants"; | ||
|
||
import MaterialTable from "material-table"; | ||
import { | ||
Add, Check, Clear, Delete, Edit, GetApp, FilterList, ArrowUpward, | ||
FirstPage, LastPage, NavigateNext, NavigateBefore, Search, ViewColumn | ||
} from '@material-ui/icons'; | ||
|
||
|
||
export const MaterialTableIcons = { | ||
Add: Add, | ||
Check: Check, | ||
Clear: Clear, | ||
Delete: Delete, | ||
// DetailPanel: DetailPanel, | ||
Edit: Edit, | ||
Export: GetApp, | ||
Filter: FilterList, | ||
FirstPage: FirstPage, | ||
LastPage: LastPage, | ||
NextPage: NavigateNext, | ||
PreviousPage: NavigateBefore, | ||
ResetSearch: Clear, | ||
Search: Search, | ||
SortArrow: ArrowUpward, | ||
// ThirdStateCheck: ThirdStateCheck, | ||
ViewColumn: ViewColumn, | ||
}; | ||
|
||
/* | ||
query = { | ||
error: undefined, | ||
filters: [], | ||
orderBy: undefined, | ||
orderDirection: "", | ||
page: 0, | ||
pageSize: 5, | ||
search: "", | ||
totalCount: 12, | ||
} | ||
*/ | ||
// TODO Add sort, search, filtering functionalities | ||
export function paginateResource(resource, transformData = null) { | ||
async function paginateData(query) { | ||
const page = query.page + 1; | ||
const shouldFetch = ( | ||
!resource.fetched | ||
|| !resource.pagination | ||
|| query.pageSize !== resource.pagination.pageSize | ||
|| (resource.pagination.fetchedPages || {})[page] == null | ||
); | ||
|
||
if (shouldFetch) { | ||
const queryParams = { page, page_size: query.pageSize }; | ||
const resp = await resource.fetchData(queryParams, true).payload; | ||
const { data, pagination } = processPagination(resp.data); | ||
return { | ||
data: transformData ? transformData(data) : data, | ||
page: query.page, | ||
totalCount: pagination ? pagination.count : data.length, | ||
} | ||
} else { | ||
const pagination = resource.pagination; | ||
const data = pagination | ||
? pagination.fetchedPages[page].map(id => resource.data[id]) | ||
: resource.data; | ||
return { | ||
data: transformData ? transformData(data) : data, | ||
page: query.page, | ||
totalCount: pagination ? pagination.count : data.length, | ||
}; | ||
} | ||
} | ||
return paginateData; | ||
} | ||
|
||
export default function APIDataTable({ path, queryParams = {}, apiOptions = {}, transformData = null, options = {}, ...props }) { | ||
const [pageSize, setPageSize] = React.useState(options.pageSize || DEFAULT_PAGE_SIZE); | ||
const _queryParams = { ...queryParams, page_size: pageSize }; | ||
const resource = useStoreAPIData(pathToArray(path), _queryParams, apiOptions); | ||
|
||
return ( | ||
<MaterialTable | ||
data={paginateResource(resource, transformData)} | ||
icons={MaterialTableIcons} | ||
onChangeRowsPerPage={setPageSize} | ||
options={{ | ||
...options, | ||
pageSizeOptions: options.pageSizeOptions || PAGE_SIZES, | ||
pageSize, | ||
}} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
APIDataTable.propTypes = { | ||
path: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.array, | ||
]).isRequired, | ||
queryParams: PropTypes.object, | ||
apiOptions: PropTypes.object, | ||
transformData: PropTypes.func, | ||
options: PropTypes.object, | ||
}; |
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,63 +1,66 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import { TableContainer, Table, TableBody, TableCell, TableRow } from '@material-ui/core'; | ||
import { SkeletonTable } from './Skeletons'; | ||
|
||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import { makeStyles } from "@material-ui/core/styles"; | ||
import { TableContainer, Table, TableBody, TableCell, TableRow } from "@material-ui/core"; | ||
import { SkeletonTable } from "./Skeletons"; | ||
|
||
const useStyles = makeStyles({ | ||
topborder: { | ||
borderTop: '1px solid rgba(224, 224, 224, 1)', | ||
}, | ||
label: { | ||
fontWeight: 500, | ||
fontSize: '1em', | ||
paddingRight: '1em', | ||
}, | ||
value: { | ||
fontWeight: 200, | ||
}, | ||
topborder: { | ||
borderTop: "1px solid rgba(224, 224, 224, 1)", | ||
}, | ||
label: { | ||
fontWeight: 500, | ||
fontSize: "1em", | ||
paddingRight: "1em", | ||
}, | ||
value: { | ||
fontWeight: 200, | ||
}, | ||
}); | ||
|
||
function renderDetailValue(value) { | ||
if (typeof value === "object") | ||
return JSON.stringify(value); | ||
if (typeof value === "object") { | ||
return JSON.stringify(value); | ||
} | ||
|
||
return value; | ||
return value; | ||
} | ||
|
||
export default function DetailsTable({ data, labels, renderValue }) { | ||
const classes = useStyles(); | ||
if (!data) | ||
return <SkeletonTable nRows={4} nCols={2} /> | ||
export default function DetailsTable({ data, fetched, labels, renderValue }) { | ||
const classes = useStyles(); | ||
if (!fetched) { | ||
return <SkeletonTable nRows={4} nCols={2} />; | ||
} | ||
|
||
const keys = Object.keys(labels || data); | ||
return ( | ||
<TableContainer> | ||
<Table> | ||
<TableBody> | ||
{keys.map((key, index) => ( | ||
<TableRow key={key} className={index === 0 ? classes.topborder : ''}> | ||
<TableCell className={classes.label}> | ||
{labels ? labels[key] : key} | ||
</TableCell> | ||
<TableCell className={classes.value}> | ||
{renderValue(data[key])} | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</TableContainer> | ||
); | ||
const keys = Object.keys(labels || data); | ||
return ( | ||
<TableContainer> | ||
<Table> | ||
<TableBody> | ||
{keys.map((key, index) => ( | ||
<TableRow key={key} className={index === 0 ? classes.topborder : ""}> | ||
<TableCell className={classes.label}> | ||
{labels ? labels[key] : key} | ||
</TableCell> | ||
<TableCell className={classes.value}>{renderValue(data[key])}</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</TableContainer> | ||
); | ||
} | ||
|
||
DetailsTable.propTypes = { | ||
data: PropTypes.object, | ||
labels: PropTypes.object, | ||
renderValue: PropTypes.func, | ||
data: PropTypes.object, | ||
fetched: PropTypes.bool, | ||
labels: PropTypes.object, | ||
renderValue: PropTypes.func, | ||
}; | ||
|
||
DetailsTable.defaultProps = { | ||
renderValue: renderDetailValue, | ||
data: undefined, | ||
fetched: undefined, | ||
labels: undefined, | ||
renderValue: renderDetailValue, | ||
}; |
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
Oops, something went wrong.