From dc052a87c58657a16871bf9656572504a6e05f99 Mon Sep 17 00:00:00 2001 From: Mike Heneghan Date: Tue, 7 Nov 2023 16:15:33 +0000 Subject: [PATCH 1/2] refactor: update flowSettings syntax to avoid custom setter - Remove the custom setter `setFlowSettings` - Rationalise the way we assign the flowSettings and store it --- .../src/pages/FlowEditor/lib/store/settings.ts | 3 --- editor.planx.uk/src/routes/flow.tsx | 11 +++++++---- editor.planx.uk/src/routes/settings.tsx | 6 +++--- editor.planx.uk/src/routes/views/published.tsx | 6 ++++-- editor.planx.uk/src/routes/views/standalone.tsx | 7 ++++--- editor.planx.uk/src/routes/views/unpublished.tsx | 6 ++++-- editor.planx.uk/src/types.ts | 2 +- 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/store/settings.ts b/editor.planx.uk/src/pages/FlowEditor/lib/store/settings.ts index 55504aa09d..d57242f25f 100644 --- a/editor.planx.uk/src/pages/FlowEditor/lib/store/settings.ts +++ b/editor.planx.uk/src/pages/FlowEditor/lib/store/settings.ts @@ -9,7 +9,6 @@ import { TeamStore } from "./team"; export interface SettingsStore { flowSettings?: FlowSettings; - setFlowSettings: (flowSettings?: FlowSettings) => void; globalSettings?: GlobalSettings; setGlobalSettings: (globalSettings: GlobalSettings) => void; updateFlowSettings: (newSettings: FlowSettings) => Promise; @@ -24,8 +23,6 @@ export const settingsStore: StateCreator< > = (set, get) => ({ flowSettings: undefined, - setFlowSettings: (flowSettings) => set({ flowSettings }), - globalSettings: undefined, setGlobalSettings: (globalSettings) => { diff --git a/editor.planx.uk/src/routes/flow.tsx b/editor.planx.uk/src/routes/flow.tsx index c8f4ede53d..60fbc92cd2 100644 --- a/editor.planx.uk/src/routes/flow.tsx +++ b/editor.planx.uk/src/routes/flow.tsx @@ -185,7 +185,7 @@ const getFlowSettings = async ( where: { slug: { _eq: $slug }, team: { slug: { _eq: $team_slug } } } ) { id - settings + flowSettings: settings } } `, @@ -194,7 +194,7 @@ const getFlowSettings = async ( team_slug: team, }, }); - return data.flows[0]?.settings; + return data.flows[0]?.flowSettings; }; const routes = compose( @@ -204,8 +204,11 @@ const routes = compose( withView(async (req) => { const [flow, ...breadcrumbs] = req.params.flow.split(","); - const settings: FlowSettings = await getFlowSettings(flow, req.params.team); - useStore.getState().setFlowSettings(settings); + const flowSettings: FlowSettings = await getFlowSettings( + flow, + req.params.team, + ); + useStore.setState({ flowSettings }); return ( <> diff --git a/editor.planx.uk/src/routes/settings.tsx b/editor.planx.uk/src/routes/settings.tsx index eedf336b39..cc6c5f80bc 100644 --- a/editor.planx.uk/src/routes/settings.tsx +++ b/editor.planx.uk/src/routes/settings.tsx @@ -27,7 +27,7 @@ const flowSettingsRoutes = compose( } ) { id - settings + flowSettings: settings } } `, @@ -37,8 +37,8 @@ const flowSettingsRoutes = compose( }, }); - const settings: FlowSettings = data.flows[0].settings; - useStore.getState().setFlowSettings(settings); + const flowSettings: FlowSettings = data.flows[0].flowSettings; + useStore.setState({ flowSettings }); return { title: makeTitle( diff --git a/editor.planx.uk/src/routes/views/published.tsx b/editor.planx.uk/src/routes/views/published.tsx index ac121b0c26..cb39f56733 100644 --- a/editor.planx.uk/src/routes/views/published.tsx +++ b/editor.planx.uk/src/routes/views/published.tsx @@ -31,18 +31,20 @@ export const publishedView = async (req: NaviRequest) => { const data = await fetchDataForPublishedView(flowSlug, teamSlug); const flow = data.flows[0]; + const flowSettings = data.flows[0]?.flowSettings; if (!flow) throw new NotFoundError(req.originalUrl); const publishedFlow = flow.publishedFlows[0]?.data; const flowData = publishedFlow ? publishedFlow : await dataMerged(flow.id); setPath(flowData, req); + useStore.setState({ flowSettings }); + const state = useStore.getState(); // XXX: necessary as long as not every flow is published; aim to remove dataMergedHotfix.ts in future // load pre-flattened published flow if exists, else load & flatten flow state.setFlow({ id: flow.id, flow: flowData, flowSlug }); state.setGlobalSettings(data.globalSettings[0]); - state.setFlowSettings(flow.settings); state.setTeam(flow.team); return ( @@ -78,7 +80,7 @@ const fetchDataForPublishedView = async ( notifyPersonalisation: notify_personalisation boundaryBBox: boundary_bbox } - settings + flowSettings: settings publishedFlows: published_flows( limit: 1 order_by: { created_at: desc } diff --git a/editor.planx.uk/src/routes/views/standalone.tsx b/editor.planx.uk/src/routes/views/standalone.tsx index 9169cde616..8b25c1168d 100644 --- a/editor.planx.uk/src/routes/views/standalone.tsx +++ b/editor.planx.uk/src/routes/views/standalone.tsx @@ -10,7 +10,7 @@ import { Flow, GlobalSettings } from "types"; import { getTeamFromDomain } from "../utils"; interface StandaloneViewData { - flows: Pick[]; + flows: Pick[]; globalSettings: GlobalSettings[]; } @@ -25,14 +25,15 @@ const standaloneView = async (req: NaviRequest) => { const data = await fetchDataForStandaloneView(flowSlug, teamSlug); const { - flows: [{ team, settings: flowSettings }], + flows: [{ team, flowSettings }], globalSettings, } = data; + useStore.setState({ flowSettings }); + const state = useStore.getState(); state.setFlowNameFromSlug(flowSlug); state.setGlobalSettings(globalSettings[0]); - state.setFlowSettings(flowSettings); state.setTeam(team); return ( diff --git a/editor.planx.uk/src/routes/views/unpublished.tsx b/editor.planx.uk/src/routes/views/unpublished.tsx index d4de06d5d2..b9a00f7387 100644 --- a/editor.planx.uk/src/routes/views/unpublished.tsx +++ b/editor.planx.uk/src/routes/views/unpublished.tsx @@ -31,11 +31,13 @@ export const unpublishedView = async (req: NaviRequest) => { const flowData = await dataMerged(flow.id); + const flowSettings = flow.flowSettings; + useStore.setState({ flowSettings }); + const state = useStore.getState(); state.setFlow({ id: flow.id, flow: flowData, flowSlug }); state.setFlowNameFromSlug(flowSlug); state.setGlobalSettings(data.globalSettings[0]); - state.setFlowSettings(flow.settings); state.setTeam(flow.team); return ( @@ -69,7 +71,7 @@ const fetchDataForUnpublishedView = async ( notifyPersonalisation: notify_personalisation boundaryBBox: boundary_bbox } - settings + flowSettings: settings slug } diff --git a/editor.planx.uk/src/types.ts b/editor.planx.uk/src/types.ts index 1f1fd03de2..6fcdce8767 100644 --- a/editor.planx.uk/src/types.ts +++ b/editor.planx.uk/src/types.ts @@ -13,7 +13,7 @@ export interface Flow { id: string; slug: string; team: Team; - settings?: FlowSettings; + flowSettings?: FlowSettings; } export interface Team { From 92fa57fcb1ffe655fb6591e8808974448d2ec7e0 Mon Sep 17 00:00:00 2001 From: Mike Heneghan Date: Tue, 7 Nov 2023 17:11:12 +0000 Subject: [PATCH 2/2] --wip-- [skip ci] --- editor.planx.uk/src/routes/flow.tsx | 2 ++ editor.planx.uk/src/routes/views/published.tsx | 2 ++ editor.planx.uk/src/routes/views/standalone.tsx | 2 ++ editor.planx.uk/src/routes/views/unpublished.tsx | 2 ++ 4 files changed, 8 insertions(+) diff --git a/editor.planx.uk/src/routes/flow.tsx b/editor.planx.uk/src/routes/flow.tsx index 60fbc92cd2..67efe7b12e 100644 --- a/editor.planx.uk/src/routes/flow.tsx +++ b/editor.planx.uk/src/routes/flow.tsx @@ -209,6 +209,8 @@ const routes = compose( req.params.team, ); useStore.setState({ flowSettings }); + const state = useStore.getState() + console.log('Flow state:', state) return ( <> diff --git a/editor.planx.uk/src/routes/views/published.tsx b/editor.planx.uk/src/routes/views/published.tsx index cb39f56733..361e3546cd 100644 --- a/editor.planx.uk/src/routes/views/published.tsx +++ b/editor.planx.uk/src/routes/views/published.tsx @@ -47,6 +47,8 @@ export const publishedView = async (req: NaviRequest) => { state.setGlobalSettings(data.globalSettings[0]); state.setTeam(flow.team); + console.log('Published state: ', state) + return ( diff --git a/editor.planx.uk/src/routes/views/standalone.tsx b/editor.planx.uk/src/routes/views/standalone.tsx index 8b25c1168d..558719e7d9 100644 --- a/editor.planx.uk/src/routes/views/standalone.tsx +++ b/editor.planx.uk/src/routes/views/standalone.tsx @@ -36,6 +36,8 @@ const standaloneView = async (req: NaviRequest) => { state.setGlobalSettings(globalSettings[0]); state.setTeam(team); + console.log('Standalone state: ', state) + return ( diff --git a/editor.planx.uk/src/routes/views/unpublished.tsx b/editor.planx.uk/src/routes/views/unpublished.tsx index b9a00f7387..5440a33594 100644 --- a/editor.planx.uk/src/routes/views/unpublished.tsx +++ b/editor.planx.uk/src/routes/views/unpublished.tsx @@ -40,6 +40,8 @@ export const unpublishedView = async (req: NaviRequest) => { state.setGlobalSettings(data.globalSettings[0]); state.setTeam(flow.team); + console.log('Unpublished settings: ', state) + return (