From f3bef430b2364183b0ccde9a28ba7b64ceba7175 Mon Sep 17 00:00:00 2001 From: Shams mosowi Date: Fri, 3 Jul 2020 20:17:15 +0800 Subject: [PATCH] tableConnect and subtable columns settings --- .../Table/ColumnMenu/MenuContents.tsx | 1 - .../components/Table/ColumnMenu/NewColumn.tsx | 1 + .../components/Table/ColumnMenu/Settings.tsx | 97 ++++++++++++++++--- www/src/components/Table/ColumnMenu/index.tsx | 49 ++++++---- .../Table/formatters/ConnectTable.tsx | 2 +- .../components/Table/formatters/SubTable.tsx | 15 ++- 6 files changed, 130 insertions(+), 35 deletions(-) diff --git a/www/src/components/Table/ColumnMenu/MenuContents.tsx b/www/src/components/Table/ColumnMenu/MenuContents.tsx index fe26d5c5..8da82a9a 100644 --- a/www/src/components/Table/ColumnMenu/MenuContents.tsx +++ b/www/src/components/Table/ColumnMenu/MenuContents.tsx @@ -9,7 +9,6 @@ import { ListSubheader, Divider, } from "@material-ui/core"; -import { fade } from "@material-ui/core/styles"; const useStyles = makeStyles(theme => createStyles({ diff --git a/www/src/components/Table/ColumnMenu/NewColumn.tsx b/www/src/components/Table/ColumnMenu/NewColumn.tsx index 15dc3a07..ab075d32 100644 --- a/www/src/components/Table/ColumnMenu/NewColumn.tsx +++ b/www/src/components/Table/ColumnMenu/NewColumn.tsx @@ -88,6 +88,7 @@ export default function FormDialog({ name, fieldName, key: fieldName, + config: {}, ...data.initializeColumn, }); }} diff --git a/www/src/components/Table/ColumnMenu/Settings.tsx b/www/src/components/Table/ColumnMenu/Settings.tsx index 5c198b52..4ab49939 100644 --- a/www/src/components/Table/ColumnMenu/Settings.tsx +++ b/www/src/components/Table/ColumnMenu/Settings.tsx @@ -1,6 +1,5 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import Button from "@material-ui/core/Button"; - import Dialog from "@material-ui/core/Dialog"; import Grid from "@material-ui/core/Grid"; import DialogActions from "@material-ui/core/DialogActions"; @@ -8,27 +7,62 @@ import DialogContent from "@material-ui/core/DialogContent"; import { Typography, IconButton, TextField, Switch } from "@material-ui/core"; import CloseIcon from "@material-ui/icons/Close"; import { FieldType } from "constants/fields"; -import FieldsDropdown from "./FieldsDropdown"; import OptionsInput from "./ConfigFields/OptionsInput"; import { useFiretableContext } from "contexts/firetableContext"; import MultiSelect from "@antlerengineering/multiselect"; +import { db } from "../../../firebase"; +import _sortBy from "lodash/sortBy"; +const ColumnSelector = ({ + tableColumns, + handleChange, + validTypes, + table, + value, +}: { + tableColumns?: any[]; + handleChange: any; + validTypes: FieldType[]; + table?: string; + value: any; +}) => { + const [columns, setColumns] = useState(tableColumns ?? []); + const getColumns = async table => { + const tableConfigDoc = await db + .doc(`_FIRETABLE_/settings/schema/${table}`) + .get(); + const tableConfig = tableConfigDoc.data(); -const ColumnSelector = ({ columns, handleChange, validTypes }) => { + if (tableConfig) setColumns(tableConfig.columns ?? []); + }; + useEffect(() => { + if (table) { + console.log({ table }); + getColumns(table); + } + }, [table]); + console.log({ columns }); const options = columns - .filter(col => validTypes.includes(col.type)) - .map(col => ({ value: col.key, label: col.name })); - return ; + ? columns + .filter(col => validTypes.includes(col.type)) + .map(col => ({ value: col.key, label: col.name })) + : []; + return ( + + ); }; const ConfigForm = ({ type, config, handleChange }) => { const { tableState, tables } = useFiretableContext(); - console.log(tables); - if (!tableState) return <>; + if (!tableState) return <>; + const { columns } = tableState; switch (type) { case FieldType.singleSelect: case FieldType.multiSelect: - const { columns } = tableState; return ( <> { ); + case FieldType.connectTable: + const tableOptions = _sortBy( + tables?.map(t => ({ + label: `${t.section} - ${t.name}`, + value: t.collection, + })) ?? [], + "label" + ); + + return ( + <> + + + { + handleChange("filters")(e.target.value); + }} + /> + + ); case FieldType.subTable: return ( diff --git a/www/src/components/Table/ColumnMenu/index.tsx b/www/src/components/Table/ColumnMenu/index.tsx index 5ce64b09..9de0ffd2 100644 --- a/www/src/components/Table/ColumnMenu/index.tsx +++ b/www/src/components/Table/ColumnMenu/index.tsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import { createStyles, makeStyles, Menu } from "@material-ui/core"; import LockOpenIcon from "@material-ui/icons/LockOpen"; @@ -24,7 +24,7 @@ import TypeChange from "./TypeChange"; import Settings from "./Settings"; import { useFiretableContext } from "contexts/firetableContext"; -import { FIELDS } from "constants/fields"; +import { FIELDS, FieldType } from "constants/fields"; import _find from "lodash/find"; import { Column } from "react-data-grid"; import { PopoverProps } from "@material-ui/core"; @@ -70,11 +70,22 @@ export default function ColumnMenu() { setSelectedColumnHeader, } as any; + const { column, anchorEl } = (selectedColumnHeader ?? {}) as any; + + useEffect(() => { + if (column && column.type === FieldType.last) { + setModal({ + type: ModalStates.new, + data: { + initializeColumn: { index: column.index ? column.index + 1 : 0 }, + }, + }); + } + }, [column]); if (!tableState || !tableActions) return null; const { orderBy } = tableState; const actions = tableActions!.column; - const { column, anchorEl } = (selectedColumnHeader ?? {}) as any; const handleClose = () => { if (!setSelectedColumnHeader) return; @@ -225,23 +236,25 @@ export default function ColumnMenu() { const clearModal = () => { setModal(INITIAL_MODAL); }; - console.log({ column }); + return ( <> - - - + {column.type !== FieldType.last && ( + + + + )} {column && ( <> ( diff --git a/www/src/components/Table/formatters/SubTable.tsx b/www/src/components/Table/formatters/SubTable.tsx index 00e0c9fd..b659c0ca 100644 --- a/www/src/components/Table/formatters/SubTable.tsx +++ b/www/src/components/Table/formatters/SubTable.tsx @@ -19,7 +19,14 @@ const useStyles = makeStyles(theme => export default function SubTable({ column, row }: CustomCellProps) { const classes = useStyles(); - const { parentLabel } = column as any; + const { parentLabel, config } = column as any; + + const label = parentLabel + ? row[parentLabel] + : config.parentLabel.reduce((acc, curr) => { + if (acc !== "") return `${acc} - ${row[curr]}`; + else return row[curr]; + }, ""); const fieldName = column.key as string; const router = useRouter(); @@ -31,11 +38,11 @@ export default function SubTable({ column, row }: CustomCellProps) { if (parentLabels) subTablePath = encodeURIComponent(`${row.ref.path}/${fieldName}`) + - `?parentLabel=${parentLabels},${row[parentLabel]}`; + `?parentLabel=${parentLabels},${label}`; else subTablePath = encodeURIComponent(`${row.ref.path}/${fieldName}`) + - `?parentLabel=${encodeURIComponent(row[parentLabel])}`; + `?parentLabel=${encodeURIComponent(label)}`; return ( - {column.name}: {row[parentLabel]} + {column.name}: {label}