import React, {Component, useState, useEffect, useReducer, useRef, useLayoutEffect} from 'react';
import _ from 'lodash';
import {Operation, SettableArg, OperationEventFunc, NoArgEventFunc} from './OperationUtils';
import {ActualArg, CommandArgSpec} from './CommandUtils';
import {objWithoutNull, replaceAtIdx, replaceAtKey} from './utils';
const nullSetter = () => 5;
export const OperationDetail = ({
command,
setCommand,
deleteCB,
columns,
commandPatterns
}: {
command: Operation;
setCommand: OperationEventFunc;
deleteCB: NoArgEventFunc;
columns: string[];
commandPatterns: CommandArgSpec;
}) => {
if (command === undefined) {
return
error undefined command
;
}
const commandName = command[0]['symbol'];
const pattern = commandPatterns[commandName];
if (!_.isArray(pattern)) {
//we shouldn't get here
return unknown command {commandName}
;
} else if (_.isEqual(pattern, [null])) {
return (
);
} else {
const fullPattern = pattern as ActualArg[];
return (
);
}
return ;
};
export const ArgGetters = ({
command,
fullPattern,
setCommand,
columns,
deleteCB
}: {
command: Operation;
fullPattern: ActualArg[];
setCommand: OperationEventFunc;
columns: string[];
deleteCB: () => void;
}) => {
const makeArgGetter = (pattern: ActualArg) => {
const idx = pattern[0];
const val = command[idx] as SettableArg;
const valSetter = (newVal) => {
const newCommand = replaceAtIdx(command, idx, newVal);
//console.log('newCommand', newCommand);
setCommand(newCommand as Operation);
};
return (
);
};
return (
{fullPattern.map(makeArgGetter)}
);
};
const ArgGetter = ({
argProps,
val,
setter,
columns
}: {
argProps: ActualArg;
val: SettableArg;
setter: (arg: SettableArg) => void;
columns: string[];
}) => {
const [argPos, label, argType, lastArg] = argProps;
const defaultShim = (event) => setter(event.target.value);
if (argType === 'enum') {
return (
);
} else if (argType === 'type') {
if (lastArg === 'integer') {
const valSetterShim = (event) => setter(parseInt(event.target.value));
return (
);
} else {
return (
);
}
} else if (argType === 'colEnum') {
const widgetRow = columns.map((colName: string) => {
const colSetter = (event) => {
const newColVal = event.target.value;
if (_.isString(newColVal)) {
const updatedColDict = replaceAtKey(
val as Record,
colName,
newColVal as string
); // as Record
setter(objWithoutNull(updatedColDict, ['null']));
}
};
const colVal = _.get(val, colName, 'null');
return (
|
);
});
return (
{columns.map((colName) => (
| {colName} |
))}
{widgetRow}
);
} else {
return unknown argtype
;
}
};