From 8d33931aa8f7c2255fff8dfbf6e1ce8cf8b95a74 Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Sat, 11 Jun 2022 13:19:53 -0400 Subject: [PATCH] ability to watch for change on sticky reducer --- package.json | 2 +- src/components/ep-explorer/EPExplorer.tsx | 31 ++++++++++++----------- src/lib/useStickyState.ts | 14 ++++++++-- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 752fb9f..ae464bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lingdocs/pashto-inflector", - "version": "2.8.6", + "version": "2.8.7", "author": "lingdocs.com", "description": "A Pashto inflection and verb conjugation engine, inculding React components for displaying Pashto text, inflections, and conjugations", "homepage": "https://verbs.lingdocs.com", diff --git a/src/components/ep-explorer/EPExplorer.tsx b/src/components/ep-explorer/EPExplorer.tsx index 6f9a3c0..995670e 100644 --- a/src/components/ep-explorer/EPExplorer.tsx +++ b/src/components/ep-explorer/EPExplorer.tsx @@ -5,12 +5,11 @@ import EquativePicker from "./EquativePicker"; import EPDisplay from "./EPDisplay"; import ButtonSelect from "../ButtonSelect"; import EqCompPicker from "./eq-comp-picker/EqCompPicker"; -import { roleIcon } from "../vp-explorer/VPExplorerExplanationModal"; import EqChartsDisplay from "./EqChartsDisplay"; import epsReducer from "./eps-reducer"; import { useEffect, useRef, useState } from "react"; import { completeEPSelection } from "../../lib/phrase-building/render-ep"; -import { makeEPSBlocks, getSubjectSelection } from "../../lib/phrase-building/blocks-utils"; +import { makeEPSBlocks } from "../../lib/phrase-building/blocks-utils"; import APPicker from "../ap-picker/APPicker"; import autoAnimate from "@formkit/auto-animate"; // @ts-ignore @@ -36,9 +35,17 @@ const blankEps: T.EPSelectionState = { function EPExplorer(props: { opts: T.TextOptions, entryFeeder: T.EntryFeeder, -}) { +} & ({ + eps: T.EPSelectionState, + onChange: (eps: T.EPSelectionState) => void, +} | {})) { const [mode, setMode] = useStickyState<"charts" | "phrases">("charts", "EPExplorerMode"); - const [eps, adjustEps] = useStickyReducer(epsReducer, blankEps, "EPState7", flashMessage); + const [eps, adjustEps] = useStickyReducer( + epsReducer, + "eps" in props ? props.eps : blankEps, + "EPState7", + flashMessage, + ); const [alert, setAlert] = useState(undefined); const [showClipped, setShowClipped] = useState(""); const parent = useRef(null); @@ -56,12 +63,6 @@ function EPExplorer(props: { } // eslint-disable-next-line }, []); - const subject = getSubjectSelection(eps.blocks).selection; - const king = subject?.selection.type === "pronoun" - ? "subject" - : eps.predicate.type === "Complement" - ? "subject" - : "predicate"; function flashMessage(msg: string) { setAlert(msg); setTimeout(() => { @@ -96,7 +97,7 @@ function EPExplorer(props: { ]} handleChange={setMode} /> -
+ {!("eps" in props) &&
{mode === "phrases" ? : ""}
-
+
} {mode === "phrases" &&
adjustEps({ type: "insert new AP" })}>+ AP
}
@@ -135,7 +136,7 @@ function EPExplorer(props: { {block && block.type === "subjectSelection" ? Subject {king === "subject" ? roleIcon.king : ""}
} + heading={
Subject
} entryFeeder={props.entryFeeder} np={block.selection} counterPart={undefined} @@ -155,7 +156,7 @@ function EPExplorer(props: { ))}
-
Predicate {king === "predicate" ? roleIcon.king : ""}
+
Predicate
adjustEps({ type: "set omitSubject", payload })} + setOmitSubject={"eps" in props ? false : payload => adjustEps({ type: "set omitSubject", payload })} />} {alert &&
( defaultValue: T | ((old: T | undefined) => T), key: string, sendAlert?: (msg: string) => void, -): [T, (action: A) => void, ((msg: string) => void) | undefined] { + onChange?: (state: T) => void, +): [ + T, + (action: A) => void, + ((msg: string) => void) | undefined, +] { const [state, unsafeSetState] = useStickyState(defaultValue, key); function adjustState(action: A) { unsafeSetState(oldState => { - return reducer(oldState, action, sendAlert); + const newState = reducer(oldState, action, sendAlert); + if (onChange) { + onChange(newState); + } + return newState; }); } + // TODO: should this really return the sendAlert here? return [state, adjustState, sendAlert]; }