import React, {Component, useState, useEffect, useReducer, useRef, useLayoutEffect} from 'react'; import _ from 'lodash'; import {Operation, SetOperationsFunc, OperationEventFunc, NoArgEventFunc} from './OperationUtils'; import {CommandConfigT} from './CommandUtils'; import {replaceInArr} from './utils'; import {bakedCommandConfig} from './bakedOperationDefaults'; import {OperationDetail} from './OperationDetail'; import {AgGridReact} from 'ag-grid-react'; // the AG Grid React Component import {ColDef, Grid, GridOptions} from 'ag-grid-community'; import {updateAtMatch} from './gridUtils'; import 'ag-grid-community/styles/ag-grid.css'; import 'ag-grid-community/styles/ag-theme-alpine.css'; import {bakedOperations} from './staticData'; const getColumns = (passedOperations: Operation[]): ColDef[] => _.map(Array.from(passedOperations.entries()), ([index, element]) => { const name = element[0]['symbol']; const key = name + index.toString(); const column = {field: key, headerName: name}; // width: 20, maxWidth: 60}; return column; }); export const OperationsList = ({ operations, activeKey, setActiveKey }: { operations: Operation[]; activeKey?: string; setActiveKey: React.Dispatch>; }) => { const rowElements = _.map(Array.from(operations.entries()), ([index, element]) => { const name = element[0]['symbol']; const key = name + index.toString(); const rowEl: Record = {}; rowEl[key] = element[2]; return rowEl; }); const rows = [_.merge({}, ...rowElements)]; const columns = getColumns(operations); const styledColumns = updateAtMatch( _.clone(columns), activeKey || '___never', { cellStyle: {background: 'var(--ag-range-selection-background-color-3)'} }, {cellStyle: {}} ); //console.log('OperationsList columns', columns); const gridOptions: GridOptions = { rowSelection: 'single', headerHeight: 30, //onRowClicked: (event) => console.log('A row was clicked'), onCellClicked: (event) => { const colName = event.column.getColId(); //console.log('operationsList onCellClicked'); setActiveKey(colName); } }; return (
); }; export const OperationAdder = ({column, addOperationCb, defaultArgs}) => { const addOperationByName = (localOperationName: string) => { return () => { const defaultOperation = defaultArgs[localOperationName]; addOperationCb(replaceInArr(defaultOperation, 'col', column)); }; }; return (
Column: {column}
{_.keys(defaultArgs).map((optionVal) => ( ))}
); }; export const OperationViewer = ({ operations, setOperations, activeColumn, allColumns, commandConfig }: { operations: Operation[]; setOperations: SetOperationsFunc; activeColumn: string; allColumns: string[]; commandConfig: CommandConfigT; }) => { const operationObjs = _.map(Array.from(operations.entries()), ([index, element]) => { const name = element[0]['symbol']; const key = name + index.toString(); const rowEl: Record = {}; rowEl[key] = element; return rowEl; }); //why am I doing this? probably something so I gauruntee a clean dict const operationDict = _.merge({}, ...operationObjs); const idxObjs = _.map(Array.from(operations.entries()), ([index, element]) => { const name = element[0]['symbol']; const key = name + index.toString(); const rowEl: Record = {}; rowEl[key] = index; return rowEl; }); const keyToIdx = _.merge({}, ...idxObjs); // previously was null const [activeKey, setActiveKey] = useState(''); function getSetOperation(key: string): OperationEventFunc { return (newOperation: Operation) => { const index = keyToIdx[key]; const nextOperations = operations.map((c, i) => { if (i === index) { return newOperation; } else { return c; } }); setOperations(nextOperations); }; } function getDeleteOperation(key: string): NoArgEventFunc { return (): void => { const index = keyToIdx[key]; const nextOperations = operations.map((c, i) => { if (i === index) { return undefined; } else { return c; } }); setActiveKey(''); setOperations(_.filter(nextOperations) as Operation[]); }; } const addOperation: OperationEventFunc = (newOperation: Operation) => { const newOperationArr = [...operations, newOperation]; setOperations(newOperationArr); const newOperationKey = getColumns(newOperationArr)[newOperationArr.length - 1].field; if (newOperationKey !== undefined) { setActiveKey(newOperationKey); } }; const {argspecs, defaultArgs} = commandConfig; //console.log('OperationsViewer operationDict', operationDict, 'activeKey', activeKey); return (

Operations

{activeKey && ( )}
); }; export const Commands = () => { const [c, setC] = useState(bakedOperations); const [commandConfig, setCommandConfig] = useState(bakedCommandConfig); useEffect(() => { fetch('http://localhost:5000/dcf/command-config').then(async (response) => { setCommandConfig(await response.json()); }); }, []); return (
{' '} {JSON.stringify(c, null, '\t\n\r')}{' '}
); };