diff --git a/new-lamassu-admin/src/components/PromptWhenDirty.js b/new-lamassu-admin/src/components/PromptWhenDirty.js new file mode 100644 index 00000000..075bd15c --- /dev/null +++ b/new-lamassu-admin/src/components/PromptWhenDirty.js @@ -0,0 +1,16 @@ +import { useFormikContext } from 'formik' +import React from 'react' +import { Prompt } from 'react-router-dom' + +const PROMPT_DEFAULT_MESSAGE = + 'You have unsaved changes on this page. Are you sure you want to leave?' + +const PromptWhenDirty = ({ message = PROMPT_DEFAULT_MESSAGE }) => { + const formik = useFormikContext() + + return ( + + ) +} + +export default PromptWhenDirty diff --git a/new-lamassu-admin/src/components/booleanPropertiesTable/BooleanPropertiesTable.js b/new-lamassu-admin/src/components/booleanPropertiesTable/BooleanPropertiesTable.js index 66045266..ca739b89 100644 --- a/new-lamassu-admin/src/components/booleanPropertiesTable/BooleanPropertiesTable.js +++ b/new-lamassu-admin/src/components/booleanPropertiesTable/BooleanPropertiesTable.js @@ -1,117 +1,110 @@ import { makeStyles } from '@material-ui/core/styles' import classnames from 'classnames' +import { useFormikContext, Form, Formik, Field as FormikField } from 'formik' +import _ from 'lodash' import React, { useState, memo } from 'react' +import * as Yup from 'yup' +import PromptWhenDirty from 'src/components/PromptWhenDirty' import { Link } from 'src/components/buttons' -import { RadioGroup } from 'src/components/inputs' +import { RadioGroup } from 'src/components/inputs/formik' import { Table, TableBody, TableRow, TableCell } from 'src/components/table' -import BooleanCell from 'src/components/tables/BooleanCell' import { H4 } from 'src/components/typography' import { ReactComponent as EditIconDisabled } from 'src/styling/icons/action/edit/disabled.svg' import { ReactComponent as EditIcon } from 'src/styling/icons/action/edit/enabled.svg' +import { ReactComponent as FalseIcon } from 'src/styling/icons/table/false.svg' +import { ReactComponent as TrueIcon } from 'src/styling/icons/table/true.svg' import { booleanPropertiesTableStyles } from './BooleanPropertiesTable.styles' const useStyles = makeStyles(booleanPropertiesTableStyles) +const BooleanCell = ({ name }) => { + const { values } = useFormikContext() + return values[name] === 'true' ? : +} + const BooleanPropertiesTable = memo( ({ title, disabled, data, elements, save }) => { + const initialValues = _.fromPairs(elements.map(it => [it.name, ''])) + const schemaValidation = _.fromPairs( + elements.map(it => [it.name, Yup.boolean().required()]) + ) + const [editing, setEditing] = useState(false) - const [radioGroupValues, setRadioGroupValues] = useState(elements) const classes = useStyles() - const innerSave = () => { - radioGroupValues.forEach(element => { - data[element.name] = element.value - }) - - save(data) + const innerSave = async value => { + save(value) setEditing(false) } - const innerCancel = () => { - setRadioGroupValues(elements) - setEditing(false) - } - - const handleRadioButtons = (elementName, newValue) => { - setRadioGroupValues( - radioGroupValues.map(element => - element.name === elementName - ? { ...element, value: newValue } - : element - ) - ) - } + const innerCancel = () => setEditing(false) const radioButtonOptions = [ - { display: 'Yes', code: true }, - { display: 'No', code: false } + { display: 'Yes', code: 'true' }, + { display: 'No', code: 'false' } ] - if (!elements || radioGroupValues?.length === 0) return null - return (
-
-

{title}

- {editing ? ( -
- - Cancel - - - Save - + +
+
+

{title}

+ {editing ? ( +
+ + Cancel + + + Save + +
+ ) : ( +
+ +
+ )}
- ) : ( -
- -
- )} -
- - - {radioGroupValues && - radioGroupValues.map((element, idx) => ( - - - {element.display} - - - {editing && ( - - handleRadioButtons( - element.name, - event.target.value === 'true' - ) - } - className={classnames( - classes.radioButtons, - classes.rightTableCell - )} - /> - )} - {!editing && ( - - )} - - - ))} - -
+ + + + {elements.map((it, idx) => ( + + + {it.display} + + + {editing && ( + + )} + {!editing && } + + + ))} + +
+ +
) } diff --git a/new-lamassu-admin/src/components/editableTable/Table.js b/new-lamassu-admin/src/components/editableTable/Table.js index 094ceca0..4a769ca5 100644 --- a/new-lamassu-admin/src/components/editableTable/Table.js +++ b/new-lamassu-admin/src/components/editableTable/Table.js @@ -4,6 +4,7 @@ import * as R from 'ramda' import React, { useState } from 'react' import { v4 } from 'uuid' +import PromptWhenDirty from 'src/components/PromptWhenDirty' import Link from 'src/components/buttons/Link.js' import { AddButton } from 'src/components/buttons/index.js' import { TBody, Table } from 'src/components/fake-table/Table' @@ -159,6 +160,7 @@ const ETable = ({ validationSchema={validationSchema} onSubmit={innerSave}>
+ diff --git a/new-lamassu-admin/src/components/tables/BooleanCell.js b/new-lamassu-admin/src/components/tables/BooleanCell.js deleted file mode 100644 index 08febd27..00000000 --- a/new-lamassu-admin/src/components/tables/BooleanCell.js +++ /dev/null @@ -1,8 +0,0 @@ -import React from 'react' - -import { ReactComponent as FalseIcon } from 'src/styling/icons/table/false.svg' -import { ReactComponent as TrueIcon } from 'src/styling/icons/table/true.svg' - -const BooleanCell = ({ value }) => (value ? : ) - -export default BooleanCell diff --git a/new-lamassu-admin/src/pages/Notifications/components/SingleFieldEditableNumber.js b/new-lamassu-admin/src/pages/Notifications/components/SingleFieldEditableNumber.js index 52a0e866..355b817f 100644 --- a/new-lamassu-admin/src/pages/Notifications/components/SingleFieldEditableNumber.js +++ b/new-lamassu-admin/src/pages/Notifications/components/SingleFieldEditableNumber.js @@ -2,6 +2,8 @@ import { Form, Formik } from 'formik' import React, { useContext } from 'react' import * as Yup from 'yup' +import PromptWhenDirty from 'src/components/PromptWhenDirty' + import NotificationsCtx from '../NotificationsContext' import Header from './EditHeader' @@ -41,6 +43,7 @@ const SingleFieldEditableNumber = ({ setEditing(name, false) }}>
+
+
{ name: 'phone', label: 'Phone number', value: - info.phone && - parsePhoneNumberFromString( - info.phone, - locale.country - ).formatInternational(), - component: NumberInput + info.phone && locale.country + ? parsePhoneNumberFromString( + info.phone, + locale.country + ).formatInternational() + : '', + component: TextInput }, { name: 'email', @@ -250,6 +252,7 @@ const ContactInfo = () => { setError(null) }}> +
( + + + + {innerRoutes.map(({ route, component: Page, key }) => ( + + + + ))} + +) const OperatorInfo = () => { - const [selected, setSelected] = useState(CONTACT_INFORMATION) const classes = useStyles() + const history = useHistory() + const location = useLocation() - const isSelected = it => selected === it + const isSelected = it => location.pathname === it.route + + const onClick = it => history.push(it.route) return ( <> it} - onClick={it => setSelected(it)} + displayName={it => it.label} + onClick={onClick} />
- {isSelected(CONTACT_INFORMATION) && } - {isSelected(RECEIPT) && } - {isSelected(TERMS_CONDITIONS) && } - {isSelected(COIN_ATM_RADAR) && } +
diff --git a/new-lamassu-admin/src/pages/OperatorInfo/ReceiptPrinting/ReceiptPrinting.js b/new-lamassu-admin/src/pages/OperatorInfo/ReceiptPrinting/ReceiptPrinting.js index 54b1ecb0..0dd89260 100644 --- a/new-lamassu-admin/src/pages/OperatorInfo/ReceiptPrinting/ReceiptPrinting.js +++ b/new-lamassu-admin/src/pages/OperatorInfo/ReceiptPrinting/ReceiptPrinting.js @@ -99,43 +99,35 @@ const ReceiptPrinting = memo(() => { elements={[ { name: 'operatorWebsite', - display: 'Operator website', - value: receiptPrintingConfig.operatorWebsite + display: 'Operator website' }, { name: 'operatorEmail', - display: 'Operator email', - value: receiptPrintingConfig.operatorEmail + display: 'Operator email' }, { name: 'operatorPhone', - display: 'Operator phone', - value: receiptPrintingConfig.operatorPhone + display: 'Operator phone' }, { name: 'companyNumber', - display: 'Company number', - value: receiptPrintingConfig.companyNumber + display: 'Company number' }, { name: 'machineLocation', - display: 'Machine location', - value: receiptPrintingConfig.machineLocation + display: 'Machine location' }, { name: 'customerNameOrPhoneNumber', - display: 'Customer name or phone number (if known)', - value: receiptPrintingConfig.customerNameOrPhoneNumber + display: 'Customer name or phone number (if known)' }, { name: 'exchangeRate', - display: 'Exchange rate', - value: receiptPrintingConfig.exchangeRate + display: 'Exchange rate' }, { name: 'addressQRCode', - display: 'Address QR code', - value: receiptPrintingConfig.addressQRCode + display: 'Address QR code' } ]} save={save} diff --git a/new-lamassu-admin/src/pages/OperatorInfo/TermsConditions.js b/new-lamassu-admin/src/pages/OperatorInfo/TermsConditions.js index 7e7167e8..ab5beb99 100644 --- a/new-lamassu-admin/src/pages/OperatorInfo/TermsConditions.js +++ b/new-lamassu-admin/src/pages/OperatorInfo/TermsConditions.js @@ -8,6 +8,7 @@ import React, { useState } from 'react' import * as Yup from 'yup' import ErrorMessage from 'src/components/ErrorMessage' +import PromptWhenDirty from 'src/components/PromptWhenDirty' import { Link } from 'src/components/buttons' import { Switch } from 'src/components/inputs' import { TextInput } from 'src/components/inputs/formik' @@ -218,6 +219,7 @@ const TermsConditions = () => { setError(null) }}> + {fields.map((f, idx) => (