Skip to content

Commit

Permalink
fix: Init user store across all routes (#2242)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Sep 28, 2023
1 parent 165ebe4 commit dfbda09
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
2 changes: 2 additions & 0 deletions editor.planx.uk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -46,6 +47,7 @@ const hasJWT = (): boolean | void => {
],
) > 0
) {
useStore.getState().initUserStore(jwt);
return true;
}
} catch (e) {}
Expand Down
35 changes: 35 additions & 0 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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<UserStore, [], [], UserStore> = (
Expand Down Expand Up @@ -52,4 +56,35 @@ export const userStore: StateCreator<UserStore, [], [], UserStore> = (
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);
},
});
35 changes: 0 additions & 35 deletions editor.planx.uk/src/routes/authenticated.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 {
Expand Down

0 comments on commit dfbda09

Please sign in to comment.