import React, { useRef, useState } from 'react'; import CodeHinter from '@/AppBuilder/CodeEditor'; import { resolveReferences } from '@/AppBuilder/CodeEditor/utils'; import SolidIcon from '@/_ui/Icon/SolidIcons'; import { ButtonSolid } from '@/_ui/AppButton/AppButton'; import cx from 'classnames'; import OverlayTrigger from 'react-bootstrap/OverlayTrigger'; import { Popover } from 'react-bootstrap'; import _ from 'lodash'; const transformvalue = (value = '') => { if (typeof value !== 'string') { return JSON.stringify(value); } return value; }; export const CellHinterWrapper = ({ isNotNull, defaultValue, selectedValue, setSelectedValue, saveFunction, isEditCell, columnDetails, close, closePopover, show, previousCellValue, }) => { const initialValueRef = useRef(selectedValue); const defaultValueRef = useRef(defaultValue); const [shouldUpdateToDefaultVal, setShouldUpdateDefaultVal] = useState(false); const [shouldUpdateToNullVal, setShouldUpdateNullVal] = useState(false); const [disabledSaveButton, setDisabledSaveButton] = useState(false); const handleInputError = (bool = false) => { setDisabledSaveButton(bool); }; const handleKeyDown = (e) => { e.stopPropagation(); if (e.key === 'Escape') { const event = new Event('click', { bubbles: true, cancelable: true }); document.body.dispatchEvent(event); } }; const darkMode = localStorage.getItem('darkMode') === 'true'; const tranformedValue = (rawValue) => { if (!rawValue) { return JSON.stringify(rawValue); } const [_, __, resolvedValue] = resolveReferences(`{{${rawValue}}}`); return resolvedValue; }; const SaveChangesSection = () => { const handleNullToggle = (value) => { setShouldUpdateNullVal(false); if (value) { setSelectedValue(null); shouldUpdateToDefaultVal && setShouldUpdateDefaultVal(false); } else { setSelectedValue(tranformedValue(previousCellValue)); } setShouldUpdateNullVal(value); }; const handleDefaultToggle = (value) => { setShouldUpdateDefaultVal(false); if (value) { setSelectedValue(defaultValue); shouldUpdateToNullVal && setShouldUpdateNullVal(false); defaultValueRef.current = defaultValue; } else { const val = tranformedValue(previousCellValue); defaultValueRef.current = val; setSelectedValue(val); } setShouldUpdateDefaultVal(true); }; return (
{
Press Enter to go on next line
}
Esc
Discard Changes
{isNotNull === false && (
Set to null
)} {defaultValue !== null && (
Set to default
)}
); }; const handleCancel = () => { const event = new Event('click', { bubbles: true, cancelable: true }); document.body.dispatchEvent(event); setShouldUpdateDefaultVal(false); setShouldUpdateNullVal(false); }; const handleSave = (e) => { if (e) { e.stopPropagation(); } saveFunction(selectedValue); const event = new Event('click', { bubbles: true, cancelable: true }); document.body.dispatchEvent(event); setShouldUpdateDefaultVal(false); setShouldUpdateNullVal(false); defaultValueRef.current = defaultValue; }; const SaveChangesFooter = () => { return (
Cancel handleSave(e)} disabled={disabledSaveButton} variant="primary" size="sm" className="fs-12 p-2" > Save
); }; const popover = ( {disabledSaveButton && (
{' '} Invalid JSON syntax
)} e.stopPropagation()}>
); const customFooter = () => { return (
e.stopPropagation()} > {disabledSaveButton && (
{' '} Invalid JSON syntax
)}
); }; return (
{ const [_, __, resolvedValue] = resolveReferences(`{{${value}}}`); setSelectedValue(resolvedValue); }} enablePreview={false} footerComponent={customFooter} componentName={`{} ${columnDetails.Header}`} errorCallback={handleInputError} defaultValue={defaultValueRef.current} reset={shouldUpdateToDefaultVal} shouldUpdateToNullVal={shouldUpdateToNullVal} columnName={columnDetails?.Header} />
); };