From 5886fb40070cb6f80650651f4b626799ccfad48f Mon Sep 17 00:00:00 2001 From: Nikolaj Frey Date: Sun, 13 Aug 2023 18:05:51 +1000 Subject: [PATCH] feat: feature/mvp-sprint-1 Added thing resetting and deletion --- remix/app/components/Thingtime/Thingtime.tsx | 39 +++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/remix/app/components/Thingtime/Thingtime.tsx b/remix/app/components/Thingtime/Thingtime.tsx index 83890d0..115eb2d 100644 --- a/remix/app/components/Thingtime/Thingtime.tsx +++ b/remix/app/components/Thingtime/Thingtime.tsx @@ -28,7 +28,7 @@ export const Thingtime = (props) => { // and add button to expand circular reference // up to 1 level deep - const { thingtime, setThingtime, loading } = useThingtime() + const { thingtime, setThingtime, getThingtime, loading } = useThingtime() const [uuid, setUuid] = React.useState(undefined) @@ -106,8 +106,12 @@ export const Thingtime = (props) => { return props.thing }, [props.thing, childrenRef.current]) + const path = React.useMemo(() => { + return props?.path?.key || props?.path || "" + }, [props?.path]) + const fullPath = React.useMemo(() => { - const ret = props?.fullPath || props?.path + const ret = props?.fullPath || props?.path?.key || props?.path // store this thing in the global db try { @@ -346,6 +350,25 @@ export const Thingtime = (props) => { [fullPath, setThingtime] ) + const resetValue = React.useCallback(() => { + updateValue({ value: null }) + }, [updateValue]) + + const deleteValue = React.useCallback(() => { + // use parent path to clone parent object but without this key + const parentPath = fullPath.split(".").slice(0, -1).join(".") + const parent = getThingtime(parentPath) + const clone = { ...parent } + + console.log("nik parentPath", parentPath) + console.log("nik path", path) + console.log("nik clone", clone) + + delete clone[path] + + setThingtime(parentPath, clone) + }, [fullPath, path, getThingtime, setThingtime]) + const atomicValue = React.useMemo(() => { if (props?.edit) { if (type === "boolean") { @@ -616,10 +639,22 @@ export const Thingtime = (props) => { opacity={showContextIcon ? 1 : 0} cursor="pointer" transition="all 0.2s ease-in-out" + onClick={resetValue} > )} + {pathDom && ( + + + + )} {/* {showContextMenu && contextMenu} */}