From 4563f816e1e74152db642b4a6b748e592e1cfad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Tue, 1 Mar 2022 05:45:51 +0000 Subject: [PATCH] fix: improve custom info request warning message on delete feat: delete custom info requests from trigger config overrides on delete --- .../src/components/DeleteDialog.js | 2 + .../CustomInfoRequests/CustomInfoRequests.js | 267 +++++++++++------- 2 files changed, 160 insertions(+), 109 deletions(-) diff --git a/new-lamassu-admin/src/components/DeleteDialog.js b/new-lamassu-admin/src/components/DeleteDialog.js index d3f1bd52..50821e81 100644 --- a/new-lamassu-admin/src/components/DeleteDialog.js +++ b/new-lamassu-admin/src/components/DeleteDialog.js @@ -64,6 +64,7 @@ export const DeleteDialog = ({ onDismissed, item = 'item', confirmationMessage = `Are you sure you want to delete this ${item}?`, + extraMessage, errorMessage = '' }) => { const classes = useStyles() @@ -87,6 +88,7 @@ export const DeleteDialog = ({ )} {confirmationMessage &&

{confirmationMessage}

} + {extraMessage}
diff --git a/new-lamassu-admin/src/pages/Triggers/CustomInfoRequests/CustomInfoRequests.js b/new-lamassu-admin/src/pages/Triggers/CustomInfoRequests/CustomInfoRequests.js index 1ec5324c..7bd99c26 100644 --- a/new-lamassu-admin/src/pages/Triggers/CustomInfoRequests/CustomInfoRequests.js +++ b/new-lamassu-admin/src/pages/Triggers/CustomInfoRequests/CustomInfoRequests.js @@ -1,4 +1,4 @@ -import { useMutation } from '@apollo/react-hooks' +import { useMutation, useQuery } from '@apollo/react-hooks' import { makeStyles } from '@material-ui/core' import classnames from 'classnames' import gql from 'graphql-tag' @@ -8,9 +8,10 @@ import React, { useState } from 'react' import { DeleteDialog } from 'src/components/DeleteDialog' import { IconButton, Button, Link } from 'src/components/buttons' import DataTable from 'src/components/tables/DataTable' -import { Info1, Info3 } from 'src/components/typography' +import { Info1, Info3, P } from 'src/components/typography' import { ReactComponent as DeleteIcon } from 'src/styling/icons/action/delete/enabled.svg' import { ReactComponent as EditIcon } from 'src/styling/icons/action/edit/enabled.svg' +import { fromNamespace, namespaces, toNamespace } from 'src/utils/config' import styles from './CustomInfoRequests.styles' import DetailsRow from './DetailsCard' @@ -33,6 +34,18 @@ const constraintTypeDisplay = { spaceSeparation: 'Space separation' } +const GET_DATA = gql` + query getData { + config + } +` + +const SAVE_CONFIG = gql` + mutation Save($config: JSONObject) { + saveConfig(config: $config) + } +` + const ADD_ROW = gql` mutation insertCustomInfoRequest($customRequest: CustomRequestInput!) { insertCustomInfoRequest(customRequest: $customRequest) { @@ -71,6 +84,13 @@ const CustomInfoRequests = ({ const [deleteDialog, setDeleteDialog] = useState(false) const [hasError, setHasError] = useState(false) + const { data: configData, loading: configLoading } = useQuery(GET_DATA) + + const [saveConfig] = useMutation(SAVE_CONFIG, { + refetchQueries: () => ['getData'], + onError: () => setHasError(true) + }) + const [addEntry] = useMutation(ADD_ROW, { onError: () => { console.log('Error while adding custom info request') @@ -108,11 +128,24 @@ const CustomInfoRequests = ({ refetchQueries: () => ['getData', 'customInfoRequests'] }) + const config = R.path(['config'])(configData) ?? [] + const handleDelete = id => { removeEntry({ variables: { id } + }).then(() => { + const triggersConfig = + (config && fromNamespace(namespaces.TRIGGERS)(config)) ?? [] + const cleanConfig = { + overrides: R.reject( + it => it.requirement === id, + triggersConfig.overrides + ) + } + const newConfig = toNamespace(namespaces.TRIGGERS)(cleanConfig) + saveConfig({ variables: { config: newConfig } }) }) } @@ -134,116 +167,132 @@ const CustomInfoRequests = ({ }) } - return ( + const detailedDeleteMsg = ( <> - {customRequests.length > 0 && ( - it.customRequest.name - }, - { - header: 'Data entry type', - width: 300, - textAlign: 'left', - size: 'sm', - view: it => inputTypeDisplay[it.customRequest.input.type] - }, - { - header: 'Constraints', - width: 300, - textAlign: 'left', - size: 'sm', - view: it => - constraintTypeDisplay[it.customRequest.input.constraintType] - }, - { - header: 'Edit', - width: 100, - textAlign: 'center', - size: 'sm', - view: it => { - return ( - { - setToBeEdited(it) - return toggleWizard() - }}> - - - ) - } - }, - { - header: 'Delete', - width: 100, - textAlign: 'center', - size: 'sm', - view: it => { - return ( - { - setToBeDeleted(it.id) - return setDeleteDialog(true) - }}> - - - ) - } - } - ]} - data={customRequests} - Details={DetailsRow} - expandable - rowSize="sm" - /> - )} - {!customRequests.length && ( -
- - It seems you haven't added any custom information requests yet. - - - Please read our{' '} - - Support Article - {' '} - on Compliance before adding new information requests. - - -
- )} - {showWizard && ( - { - setToBeEdited(null) - setHasError(false) - toggleWizard() - }} - toBeEdited={toBeEdited} - onSave={(...args) => handleSave(...args)} - /> - )} - - { - setDeleteDialog(false) - setHasError(false) - }} - onConfirmed={() => handleDelete(toBeDeleted)} - /> +

+ Deleting this item will result in the triggers using it to be removed, + together with the advanced trigger overrides you defined for this item. +

+

+ This action is permanent. +

) + + return ( + !configLoading && ( + <> + {customRequests.length > 0 && ( + it.customRequest.name + }, + { + header: 'Data entry type', + width: 300, + textAlign: 'left', + size: 'sm', + view: it => inputTypeDisplay[it.customRequest.input.type] + }, + { + header: 'Constraints', + width: 300, + textAlign: 'left', + size: 'sm', + view: it => + constraintTypeDisplay[it.customRequest.input.constraintType] + }, + { + header: 'Edit', + width: 100, + textAlign: 'center', + size: 'sm', + view: it => { + return ( + { + setToBeEdited(it) + return toggleWizard() + }}> + + + ) + } + }, + { + header: 'Delete', + width: 100, + textAlign: 'center', + size: 'sm', + view: it => { + return ( + { + setToBeDeleted(it.id) + return setDeleteDialog(true) + }}> + + + ) + } + } + ]} + data={customRequests} + Details={DetailsRow} + expandable + rowSize="sm" + /> + )} + {!customRequests.length && ( +
+ + It seems you haven't added any custom information requests yet. + + + Please read our{' '} + + Support Article + {' '} + on Compliance before adding new information requests. + + +
+ )} + {showWizard && ( + { + setToBeEdited(null) + setHasError(false) + toggleWizard() + }} + toBeEdited={toBeEdited} + onSave={(...args) => handleSave(...args)} + /> + )} + + { + setDeleteDialog(false) + setHasError(false) + }} + item={`custom information request`} + extraMessage={detailedDeleteMsg} + onConfirmed={() => handleDelete(toBeDeleted)} + /> + + ) + ) } export default CustomInfoRequests