From 122249076ff4cdd223aa2b75b62ff1d8c9a97863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Mon, 2 Oct 2023 15:16:21 +0100 Subject: [PATCH] fix: Reset team store on navigation (#2271) --- .../src/pages/FlowEditor/lib/store/team.ts | 18 ++++++++++++++---- editor.planx.uk/src/routes/authenticated.tsx | 2 ++ editor.planx.uk/src/routes/views/team.tsx | 9 ++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/store/team.ts b/editor.planx.uk/src/pages/FlowEditor/lib/store/team.ts index d09d8b29df..d120327e6d 100644 --- a/editor.planx.uk/src/pages/FlowEditor/lib/store/team.ts +++ b/editor.planx.uk/src/pages/FlowEditor/lib/store/team.ts @@ -1,7 +1,6 @@ import { GeoJSONObject } from "@turf/helpers"; import gql from "graphql-tag"; import { client } from "lib/graphql"; -import { getTeamFromDomain } from "routes/utils"; import { NotifyPersonalisation, TeamSettings } from "types"; import { TeamTheme } from "types"; import { Team } from "types"; @@ -18,7 +17,8 @@ export interface TeamStore { setTeam: (team: Team) => void; getTeam: () => Team; - initTeamStore: (teamSlugFromURLParams?: string) => Promise; + initTeamStore: (slug: string) => Promise; + clearTeamStore: () => void; } export const teamStore: StateCreator = ( @@ -54,8 +54,7 @@ export const teamStore: StateCreator = ( boundaryBBox: get().boundaryBBox, }), - initTeamStore: async (teamSlugFromURLParams) => { - const slug = teamSlugFromURLParams || await getTeamFromDomain(window.location.hostname) + initTeamStore: async (slug) => { const { data } = await client.query({ query: gql` query GetTeams($slug: String!) { @@ -88,4 +87,15 @@ export const teamStore: StateCreator = ( if (!team) throw new Error("Team not found"); get().setTeam(team); }, + + clearTeamStore: () => + set({ + teamId: 0, + teamTheme: undefined, + teamName: "", + teamSettings: undefined, + teamSlug: "", + notifyPersonalisation: undefined, + boundaryBBox: undefined, + }), }); diff --git a/editor.planx.uk/src/routes/authenticated.tsx b/editor.planx.uk/src/routes/authenticated.tsx index 11b809c624..eb6a45ac85 100644 --- a/editor.planx.uk/src/routes/authenticated.tsx +++ b/editor.planx.uk/src/routes/authenticated.tsx @@ -26,6 +26,8 @@ const editorRoutes = compose( `, }); + useStore.getState().clearTeamStore(); + return { title: makeTitle("Teams"), view: , diff --git a/editor.planx.uk/src/routes/views/team.tsx b/editor.planx.uk/src/routes/views/team.tsx index 453a8e7763..bbf1898d6e 100644 --- a/editor.planx.uk/src/routes/views/team.tsx +++ b/editor.planx.uk/src/routes/views/team.tsx @@ -2,16 +2,19 @@ import { NaviRequest, NotFoundError } from "navi" import { useStore } from "pages/FlowEditor/lib/store"; import React from "react"; import { View } from "react-navi" +import { getTeamFromDomain } from "routes/utils"; /** * View wrapper for /team routes * Initialises TeamStore if not already set */ export const teamView = async (req: NaviRequest) => { - const { teamId, initTeamStore } = useStore.getState(); - if (!teamId) { + const { initTeamStore, teamSlug: currentSlug } = useStore.getState(); + const routeSlug = req.params.team || await getTeamFromDomain(window.location.hostname) + + if (currentSlug !== routeSlug) { try { - await initTeamStore(req.params.team); + await initTeamStore(routeSlug); } catch (error) { throw new NotFoundError(`Team not found: ${error}`); }