Skip to content

Commit

Permalink
fix: Reset team store on navigation (#2271)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Oct 2, 2023
1 parent 0f765c7 commit 1222490
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
18 changes: 14 additions & 4 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/team.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -18,7 +17,8 @@ export interface TeamStore {

setTeam: (team: Team) => void;
getTeam: () => Team;
initTeamStore: (teamSlugFromURLParams?: string) => Promise<void>;
initTeamStore: (slug: string) => Promise<void>;
clearTeamStore: () => void;
}

export const teamStore: StateCreator<TeamStore, [], [], TeamStore> = (
Expand Down Expand Up @@ -54,8 +54,7 @@ export const teamStore: StateCreator<TeamStore, [], [], TeamStore> = (
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!) {
Expand Down Expand Up @@ -88,4 +87,15 @@ export const teamStore: StateCreator<TeamStore, [], [], TeamStore> = (
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,
}),
});
2 changes: 2 additions & 0 deletions editor.planx.uk/src/routes/authenticated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const editorRoutes = compose(
`,
});

useStore.getState().clearTeamStore();

return {
title: makeTitle("Teams"),
view: <Teams teams={data.teams} />,
Expand Down
9 changes: 6 additions & 3 deletions editor.planx.uk/src/routes/views/team.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down

0 comments on commit 1222490

Please sign in to comment.