|
| 1 | +/* |
| 2 | +Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, |
| 11 | +software distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React, { useEffect } from "react"; |
| 18 | +import { useSelector, useDispatch } from "react-redux"; |
| 19 | +import PropTypes from "prop-types"; |
| 20 | + |
| 21 | +import lodash from "lodash"; |
| 22 | + |
| 23 | +import Typography from "@material-ui/core/Typography"; |
| 24 | + |
| 25 | +import { |
| 26 | + selectAuthState, |
| 27 | + selectRecipesState, |
| 28 | + searchRecipesThunk, |
| 29 | + selectRecipesTable, |
| 30 | +} from "../../store"; |
| 31 | +import makeStyles from "./recipe-table-styles"; |
| 32 | +import ZooTable from "../zoo-table"; |
| 33 | + |
| 34 | +function RecipeTable({ domain, subdomain, includePagination, includeHeader, queries }) { |
| 35 | + const useStyles = makeStyles(); |
| 36 | + const classes = useStyles(); |
| 37 | + const dispatch = useDispatch(); |
| 38 | + |
| 39 | + const authState = useSelector(selectAuthState); |
| 40 | + const recipesState = useSelector(selectRecipesState); |
| 41 | + console.log(recipesState); |
| 42 | + const results = useSelector(selectRecipesTable); |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + const status = lodash.get(recipesState.status, `${domain}.${subdomain}`, "idle"); |
| 46 | + if (authState.token !== null && status === "idle") { |
| 47 | + dispatch( |
| 48 | + searchRecipesThunk({ |
| 49 | + domain, |
| 50 | + subdomain, |
| 51 | + token: authState.token, |
| 52 | + queries, |
| 53 | + }) |
| 54 | + ); |
| 55 | + } |
| 56 | + }, [authState.token, recipesState.status, dispatch, domain, subdomain, queries]); |
| 57 | + |
| 58 | + const rows = lodash |
| 59 | + .get(results, `${domain}.${subdomain}.data`, []) |
| 60 | + .map((data) => data.row); |
| 61 | + const status = lodash.get(results, `${domain}.${subdomain}.status`, "idle"); |
| 62 | + const loaded = status !== "idle" && status !== "loading"; |
| 63 | + return ( |
| 64 | + <div className={classes.root}> |
| 65 | + {loaded && includeHeader && ( |
| 66 | + <Typography variant="h6"> |
| 67 | + {lodash.get( |
| 68 | + results, |
| 69 | + `${domain}.${subdomain}.displayName`, |
| 70 | + `${domain} ${subdomain}` |
| 71 | + )} |
| 72 | + </Typography> |
| 73 | + )} |
| 74 | + <ZooTable |
| 75 | + ariaLabel={`${domain}.${subdomain}`} |
| 76 | + headers={lodash.get(results, `${domain}.${subdomain}.headers`, [])} |
| 77 | + rows={rows} |
| 78 | + status={status} |
| 79 | + error={authState.error || recipesState.error} |
| 80 | + aligns={lodash.get(results, `${domain}.${subdomain}.aligns`, "left")} |
| 81 | + copy={lodash.get(results, `${domain}.${subdomain}.copy`, false)} |
| 82 | + width={lodash.get(results, `${domain}.${subdomain}.width`)} |
| 83 | + includePagination={includePagination} |
| 84 | + loadingMessage="Loading recipes" |
| 85 | + /> |
| 86 | + </div> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +RecipeTable.propTypes = { |
| 91 | + domain: PropTypes.string.isRequired, |
| 92 | + subdomain: PropTypes.string.isRequired, |
| 93 | + queries: PropTypes.object, |
| 94 | + includePagination: PropTypes.bool, |
| 95 | + includeHeader: PropTypes.bool, |
| 96 | +}; |
| 97 | + |
| 98 | +RecipeTable.defaultProps = { |
| 99 | + includePagination: false, |
| 100 | + includeHeader: false, |
| 101 | + queries: {}, |
| 102 | +}; |
| 103 | + |
| 104 | +export default RecipeTable; |
0 commit comments