From dfbda0910eff969498ac1f2e8cd92db746b68934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Thu, 28 Sep 2023 10:04:06 +0100 Subject: [PATCH] fix: Init user store across all routes (#2242) --- editor.planx.uk/src/index.tsx | 2 ++ .../src/pages/FlowEditor/lib/store/user.ts | 35 +++++++++++++++++++ editor.planx.uk/src/routes/authenticated.tsx | 35 ------------------- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/editor.planx.uk/src/index.tsx b/editor.planx.uk/src/index.tsx index ba91f8afe2..e1e46e621b 100644 --- a/editor.planx.uk/src/index.tsx +++ b/editor.planx.uk/src/index.tsx @@ -13,6 +13,7 @@ import jwtDecode from "jwt-decode"; import { getCookie, setCookie } from "lib/cookie"; import ErrorPage from "pages/ErrorPage"; import { AnalyticsProvider } from "pages/FlowEditor/lib/analyticsProvider"; +import { useStore } from "pages/FlowEditor/lib/store"; import React, { Suspense, useEffect } from "react"; import { createRoot } from "react-dom/client"; import { NotFoundBoundary, Router, useLoadingRoute, View } from "react-navi"; @@ -46,6 +47,7 @@ const hasJWT = (): boolean | void => { ], ) > 0 ) { + useStore.getState().initUserStore(jwt); return true; } } catch (e) {} diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts b/editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts index d31cffd7ac..ca4685e5d8 100644 --- a/editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts +++ b/editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts @@ -1,4 +1,7 @@ import { User, UserTeams } from "@opensystemslab/planx-core/types"; +import gql from "graphql-tag"; +import jwtDecode from "jwt-decode"; +import { client } from "lib/graphql"; import { Team } from "types"; import type { StateCreator } from "zustand"; @@ -13,6 +16,7 @@ export interface UserStore { setUser: (user: User) => void; getUser: () => User; canUserEditTeam: (teamSlug: Team["slug"]) => boolean; + initUserStore: (jwt: string) => void; } export const userStore: StateCreator = ( @@ -52,4 +56,35 @@ export const userStore: StateCreator = ( get().teams.filter((team) => team.role === "teamEditor" && team.team.slug === teamSlug).length > 0 ); }, + + async initUserStore (jwt: string) { + const email = (jwtDecode(jwt) as any)["email"]; + const users = await client.query({ + query: gql` + query GetUserByEmail($email: String!) { + users: users(where: { email: { _eq: $email } }) { + id + firstName: first_name + lastName: last_name + email + isPlatformAdmin: is_platform_admin + teams { + role + team { + name + slug + id + } + } + } + } + `, + variables: { email }, + }); + + const user: User = users.data.users[0]; + if (!user) throw new Error(`Failed to get user ${email}`); + + this.setUser(user); + }, }); diff --git a/editor.planx.uk/src/routes/authenticated.tsx b/editor.planx.uk/src/routes/authenticated.tsx index dc7aaf5078..62f33b339a 100644 --- a/editor.planx.uk/src/routes/authenticated.tsx +++ b/editor.planx.uk/src/routes/authenticated.tsx @@ -1,7 +1,4 @@ -import { User } from "@opensystemslab/planx-core/types"; import gql from "graphql-tag"; -import jwtDecode from "jwt-decode"; -import { getCookie } from "lib/cookie"; import { compose, lazy, mount, route, withData, withView } from "navi"; import { useStore } from "pages/FlowEditor/lib/store"; import React from "react"; @@ -21,38 +18,6 @@ const editorRoutes = compose( mount({ "/": route(async () => { - const jwt = getCookie("jwt"); - const email = jwt && (jwtDecode(jwt) as any)["email"]; - const users = await client.query({ - query: gql` - query GetUserByEmail($email: String!) { - users: users(where: { email: { _eq: $email } }) { - id - firstName: first_name - lastName: last_name - email - isPlatformAdmin: is_platform_admin - teams { - role - team { - name - slug - id - } - } - } - } - `, - variables: { email }, - }); - - if (users) { - const user: User = users.data.users[0]; - useStore.getState().setUser(user); - } else { - throw new Error(`Failed to get user ${email}`); - } - const { data } = await client.query({ query: gql` query {