diff --git a/new-lamassu-admin/src/components/editableTable/Row.js b/new-lamassu-admin/src/components/editableTable/Row.js index e138ddb2..eeeb94a5 100644 --- a/new-lamassu-admin/src/components/editableTable/Row.js +++ b/new-lamassu-admin/src/components/editableTable/Row.js @@ -1,268 +1,267 @@ -/* eslint-disable import/order */ -import { makeStyles } from '@material-ui/core' -import classnames from 'classnames' -import { Field, useFormikContext } from 'formik' -import * as R from 'ramda' -import React, { useContext, useState } from 'react' - -import { DeleteDialog } from 'src/components/DeleteDialog' -import { Link, IconButton } from 'src/components/buttons' -import { Td, Tr } from 'src/components/fake-table/Table' -import { Switch } from 'src/components/inputs' -import { TL2 } from 'src/components/typography' -import { ReactComponent as DisabledDeleteIcon } from 'src/styling/icons/action/delete/disabled.svg' -import { ReactComponent as DeleteIcon } from 'src/styling/icons/action/delete/enabled.svg' -import { ReactComponent as DisabledEditIcon } from 'src/styling/icons/action/edit/disabled.svg' -import { ReactComponent as EditIcon } from 'src/styling/icons/action/edit/enabled.svg' -import { ReactComponent as StripesSvg } from 'src/styling/icons/stripes.svg' - -import TableCtx from './Context' -import styles from './Row.styles' - -const useStyles = makeStyles(styles) - -const ActionCol = ({ disabled, editing }) => { - const classes = useStyles() - const { values, submitForm, resetForm } = useFormikContext() - const { - editWidth, - onEdit, - enableEdit, - enableDelete, - disableRowEdit, - onDelete, - deleteWidth, - enableToggle, - onToggle, - toggleWidth, - forceAdd, - clearError, - actionColSize, - error - } = useContext(TableCtx) - - const disableEdit = disabled || (disableRowEdit && disableRowEdit(values)) - const cancel = () => { - clearError() - resetForm() - } - - const [deleteDialog, setDeleteDialog] = useState(false) - - const onConfirmed = () => { - onDelete(values.id).then(res => { - if (!R.isNil(res)) setDeleteDialog(false) - }) - } - - return ( - <> - {editing && ( - - - Save - - {!forceAdd && ( - - Cancel - - )} - - )} - {!editing && enableEdit && ( - - onEdit && onEdit(values.id)}> - {disableEdit ? : } - - - )} - {!editing && enableDelete && ( - - { - setDeleteDialog(true) - }}> - {disabled ? : } - - { - setDeleteDialog(false) - clearError() - }} - errorMessage={error} - /> - - )} - {!editing && enableToggle && ( - - onToggle(values.id)} - /> - - )} - - ) -} - -const ECol = ({ editing, focus, config, extraPaddingRight, extraPadding }) => { - const { - name, - bypassField, - input, - editable = true, - size, - bold, - width, - textAlign, - suffix, - SuffixComponent = TL2, - textStyle = it => {}, - view = it => it?.toString(), - inputProps = {} - } = config - - const { values } = useFormikContext() - const classes = useStyles({ textAlign, size }) - - const innerProps = { - fullWidth: true, - autoFocus: focus, - size, - bold, - textAlign, - ...inputProps - } - - const isEditing = editing && editable - const isField = !bypassField - - return ( - - {isEditing && isField && ( - - )} - {isEditing && !isField && } - {!isEditing && values && ( -
- {view(values[name], values)} -
- )} - {suffix && ( - - {suffix} - - )} - - ) -} - -const groupStriped = elements => { - const [toStripe, noStripe] = R.partition(R.propEq('stripe', true))(elements) - - if (!toStripe.length) { - return elements - } - - const index = R.indexOf(toStripe[0], elements) - const width = R.compose(R.sum, R.map(R.path(['width'])))(toStripe) - - return R.insert( - index, - { width, editable: false, view: () => }, - noStripe - ) -} - -const ERow = ({ editing, disabled, lastOfGroup }) => { - const { touched, errors, values } = useFormikContext() - const { - elements, - enableEdit, - enableDelete, - error, - enableToggle, - rowSize, - stripeWhen - } = useContext(TableCtx) - - const classes = useStyles() - - const shouldStripe = stripeWhen && stripeWhen(values) - - const innerElements = shouldStripe ? groupStriped(elements) : elements - const [toSHeader] = R.partition(R.has('doubleHeader'))(elements) - - const extraPaddingIndex = toSHeader?.length - ? R.indexOf(toSHeader[0], elements) - : -1 - - const extraPaddingRightIndex = toSHeader?.length - ? R.indexOf(toSHeader[toSHeader.length - 1], elements) - : -1 - - const elementToFocusIndex = innerElements.findIndex( - it => it.editable === undefined || it.editable - ) - - const classNames = { - [classes.lastOfGroup]: lastOfGroup - } - - const touchedErrors = R.pick(R.keys(touched), errors) - const hasTouchedErrors = touchedErrors && R.keys(touchedErrors).length > 0 - const hasErrors = hasTouchedErrors || !!error - - const errorMessage = - error || (touchedErrors && R.values(touchedErrors).join(', ')) - - return ( - - {innerElements.map((it, idx) => { - return ( - - ) - })} - {(enableEdit || enableDelete || enableToggle) && ( - - )} - - ) -} - -export default ERow +import { makeStyles } from '@material-ui/core' +import classnames from 'classnames' +import { Field, useFormikContext } from 'formik' +import * as R from 'ramda' +import React, { useContext, useState } from 'react' + +import { DeleteDialog } from 'src/components/DeleteDialog' +import { Link, IconButton } from 'src/components/buttons' +import { Td, Tr } from 'src/components/fake-table/Table' +import { Switch } from 'src/components/inputs' +import { TL2 } from 'src/components/typography' +import { ReactComponent as DisabledDeleteIcon } from 'src/styling/icons/action/delete/disabled.svg' +import { ReactComponent as DeleteIcon } from 'src/styling/icons/action/delete/enabled.svg' +import { ReactComponent as DisabledEditIcon } from 'src/styling/icons/action/edit/disabled.svg' +import { ReactComponent as EditIcon } from 'src/styling/icons/action/edit/enabled.svg' +import { ReactComponent as StripesSvg } from 'src/styling/icons/stripes.svg' + +import TableCtx from './Context' +import styles from './Row.styles' + +const useStyles = makeStyles(styles) + +const ActionCol = ({ disabled, editing }) => { + const classes = useStyles() + const { values, submitForm, resetForm } = useFormikContext() + const { + editWidth, + onEdit, + enableEdit, + enableDelete, + disableRowEdit, + onDelete, + deleteWidth, + enableToggle, + onToggle, + toggleWidth, + forceAdd, + clearError, + actionColSize, + error + } = useContext(TableCtx) + + const disableEdit = disabled || (disableRowEdit && disableRowEdit(values)) + const cancel = () => { + clearError() + resetForm() + } + + const [deleteDialog, setDeleteDialog] = useState(false) + + const onConfirmed = () => { + onDelete(values.id).then(res => { + if (!R.isNil(res)) setDeleteDialog(false) + }) + } + + return ( + <> + {editing && ( + + + Save + + {!forceAdd && ( + + Cancel + + )} + + )} + {!editing && enableEdit && ( + + onEdit && onEdit(values.id)}> + {disableEdit ? : } + + + )} + {!editing && enableDelete && ( + + { + setDeleteDialog(true) + }}> + {disabled ? : } + + { + setDeleteDialog(false) + clearError() + }} + errorMessage={error} + /> + + )} + {!editing && enableToggle && ( + + onToggle(values.id)} + /> + + )} + + ) +} + +const ECol = ({ editing, focus, config, extraPaddingRight, extraPadding }) => { + const { + name, + bypassField, + input, + editable = true, + size, + bold, + width, + textAlign, + suffix, + SuffixComponent = TL2, + textStyle = it => {}, + view = it => it?.toString(), + inputProps = {} + } = config + + const { values } = useFormikContext() + const classes = useStyles({ textAlign, size }) + + const innerProps = { + fullWidth: true, + autoFocus: focus, + size, + bold, + textAlign, + ...inputProps + } + + const isEditing = editing && editable + const isField = !bypassField + + return ( + + {isEditing && isField && ( + + )} + {isEditing && !isField && } + {!isEditing && values && ( +
+ {view(values[name], values)} +
+ )} + {suffix && ( + + {suffix} + + )} + + ) +} + +const groupStriped = elements => { + const [toStripe, noStripe] = R.partition(R.propEq('stripe', true))(elements) + + if (!toStripe.length) { + return elements + } + + const index = R.indexOf(toStripe[0], elements) + const width = R.compose(R.sum, R.map(R.path(['width'])))(toStripe) + + return R.insert( + index, + { width, editable: false, view: () => }, + noStripe + ) +} + +const ERow = ({ editing, disabled, lastOfGroup }) => { + const { touched, errors, values } = useFormikContext() + const { + elements, + enableEdit, + enableDelete, + error, + enableToggle, + rowSize, + stripeWhen + } = useContext(TableCtx) + + const classes = useStyles() + + const shouldStripe = stripeWhen && stripeWhen(values) + + const innerElements = shouldStripe ? groupStriped(elements) : elements + const [toSHeader] = R.partition(R.has('doubleHeader'))(elements) + + const extraPaddingIndex = toSHeader?.length + ? R.indexOf(toSHeader[0], elements) + : -1 + + const extraPaddingRightIndex = toSHeader?.length + ? R.indexOf(toSHeader[toSHeader.length - 1], elements) + : -1 + + const elementToFocusIndex = innerElements.findIndex( + it => it.editable === undefined || it.editable + ) + + const classNames = { + [classes.lastOfGroup]: lastOfGroup + } + + const touchedErrors = R.pick(R.keys(touched), errors) + const hasTouchedErrors = touchedErrors && R.keys(touchedErrors).length > 0 + const hasErrors = hasTouchedErrors || !!error + + const errorMessage = + error || (touchedErrors && R.values(touchedErrors).join(', ')) + + return ( + + {innerElements.map((it, idx) => { + return ( + + ) + })} + {(enableEdit || enableDelete || enableToggle) && ( + + )} + + ) +} + +export default ERow