import { createPortal } from 'react-dom' import PropTypes from 'prop-types' import { useState, useEffect } from 'react' import { useDispatch } from 'react-redux' import { enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackbarAction } from 'store/actions' // Material import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Box, Typography, OutlinedInput } from '@mui/material' // Project imports import { StyledButton } from 'ui-component/button/StyledButton' import ConfirmDialog from 'ui-component/dialog/ConfirmDialog' // Icons import { IconX, IconVariable } from '@tabler/icons' // API import variablesApi from 'api/variables' // Hooks // utils import useNotifier from 'utils/useNotifier' // const import { HIDE_CANVAS_DIALOG, SHOW_CANVAS_DIALOG } from 'store/actions' import { Dropdown } from '../../ui-component/dropdown/Dropdown' const variableTypes = [ { label: 'Static', name: 'static', description: 'Variable value will be read from the value entered below' }, { label: 'Runtime', name: 'runtime', description: 'Variable value will be read from .env file' } ] const AddEditVariableDialog = ({ show, dialogProps, onCancel, onConfirm }) => { const portalElement = document.getElementById('portal') const dispatch = useDispatch() // ==============================|| Snackbar ||============================== // useNotifier() const enqueueSnackbar = (...args) => dispatch(enqueueSnackbarAction(...args)) const closeSnackbar = (...args) => dispatch(closeSnackbarAction(...args)) const [variableName, setVariableName] = useState('') const [variableValue, setVariableValue] = useState('') const [variableType, setVariableType] = useState('static') const [dialogType, setDialogType] = useState('ADD') const [variable, setVariable] = useState({}) useEffect(() => { if (dialogProps.type === 'EDIT' && dialogProps.data) { setVariableName(dialogProps.data.name) setVariableValue(dialogProps.data.value) setVariableType(dialogProps.data.type) setDialogType('EDIT') setVariable(dialogProps.data) } else if (dialogProps.type === 'ADD') { setVariableName('') setVariableValue('') setVariableType('static') setDialogType('ADD') setVariable({}) } return () => { setVariableName('') setVariableValue('') setVariableType('static') setDialogType('ADD') setVariable({}) } }, [dialogProps]) useEffect(() => { if (show) dispatch({ type: SHOW_CANVAS_DIALOG }) else dispatch({ type: HIDE_CANVAS_DIALOG }) return () => dispatch({ type: HIDE_CANVAS_DIALOG }) }, [show, dispatch]) const addNewVariable = async () => { try { const obj = { name: variableName, value: variableValue, type: variableType } const createResp = await variablesApi.createVariable(obj) if (createResp.data) { enqueueSnackbar({ message: 'New Variable added', options: { key: new Date().getTime() + Math.random(), variant: 'success', action: (key) => ( ) } }) onConfirm(createResp.data.id) } } catch (err) { const errorData = typeof err === 'string' ? err : err.response?.data || `${err.response?.status}: ${err.response?.statusText}` enqueueSnackbar({ message: `Failed to add new Variable: ${errorData}`, options: { key: new Date().getTime() + Math.random(), variant: 'error', persist: true, action: (key) => ( ) } }) onCancel() } } const saveVariable = async () => { try { const saveObj = { name: variableName, value: variableValue, type: variableType } const saveResp = await variablesApi.updateVariable(variable.id, saveObj) if (saveResp.data) { enqueueSnackbar({ message: 'Variable saved', options: { key: new Date().getTime() + Math.random(), variant: 'success', action: (key) => ( ) } }) onConfirm(saveResp.data.id) } } catch (error) { const errorData = error.response?.data || `${error.response?.status}: ${error.response?.statusText}` enqueueSnackbar({ message: `Failed to save Variable: ${errorData}`, options: { key: new Date().getTime() + Math.random(), variant: 'error', persist: true, action: (key) => ( ) } }) onCancel() } } const component = show ? (
{dialogProps.type === 'ADD' ? 'Add Variable' : 'Edit Variable'}
Variable Name *
setVariableName(e.target.value)} value={variableName ?? ''} />
Type *
setVariableType(newValue)} value={variableType ?? 'choose an option'} />
{variableType === 'static' && (
Value *
setVariableValue(e.target.value)} value={variableValue ?? ''} />
)}
(dialogType === 'ADD' ? addNewVariable() : saveVariable())} > {dialogProps.confirmButtonName}
) : null return createPortal(component, portalElement) } AddEditVariableDialog.propTypes = { show: PropTypes.bool, dialogProps: PropTypes.object, onCancel: PropTypes.func, onConfirm: PropTypes.func } export default AddEditVariableDialog