Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reset team store on navigation #2271

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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