Skip to content

Commit

Permalink
fix: Move client to store to avoid circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Jan 15, 2024
1 parent 76c03c3 commit edf30b9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
6 changes: 0 additions & 6 deletions editor.planx.uk/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CoreDomainClient } from "@opensystemslab/planx-core";
import { useStore } from "pages/FlowEditor/lib/store";

/**
* core doesn't expose a graphql interface like the graphql/hasura clients do
Expand All @@ -8,8 +7,3 @@ import { useStore } from "pages/FlowEditor/lib/store";
export const _public = new CoreDomainClient({
targetURL: process.env.REACT_APP_HASURA_URL!,
});

export const _client = new CoreDomainClient({
targetURL: process.env.REACT_APP_HASURA_URL!,
auth: { jwt: useStore.getState().jwt || "" },
});
6 changes: 6 additions & 0 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/shared.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CoreDomainClient } from "@opensystemslab/planx-core";
import { ROOT_NODE_KEY } from "@planx/graph";
import { capitalize } from "lodash";
import { removeSessionIdSearchParam } from "utils";
Expand Down Expand Up @@ -31,6 +32,7 @@ export interface SharedStore extends Store.Store {
setPreviewEnvironment: (previewEnvironment: PreviewEnvironment) => void;
setFlowSlug: (flowSlug: string) => void;
setFlowNameFromSlug: (flowSlug: string) => void;
_client: CoreDomainClient,
}

export const sharedStore: StateCreator<
Expand Down Expand Up @@ -107,4 +109,8 @@ export const sharedStore: StateCreator<
const flowName = capitalize(flowSlug.replaceAll?.("-", " "));
set({ flowName });
},

_client: new CoreDomainClient({
targetURL: process.env.REACT_APP_HASURA_URL!,
}),
});
16 changes: 15 additions & 1 deletion editor.planx.uk/src/pages/FlowEditor/lib/store/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import gql from "graphql-tag";
import { client } from "lib/graphql";
import type { StateCreator } from "zustand";

import { SharedStore } from "./shared";

export interface TeamStore {
teamId: number;
teamTheme?: TeamTheme;
Expand All @@ -21,9 +23,10 @@ export interface TeamStore {
getTeam: () => Team;
initTeamStore: (slug: string) => Promise<void>;
clearTeamStore: () => void;
fetchCurrentTeam: () => Promise<Team>;
}

export const teamStore: StateCreator<TeamStore, [], [], TeamStore> = (
export const teamStore: StateCreator<TeamStore & SharedStore, [], [], TeamStore> = (
set,
get,
) => ({
Expand Down Expand Up @@ -100,4 +103,15 @@ export const teamStore: StateCreator<TeamStore, [], [], TeamStore> = (
notifyPersonalisation: undefined,
boundaryBBox: undefined,
}),

/**
* Fetch current team
* Does not necessarily match team held in store as this is context-based (e.g. we don't use the team theme in the Editor)
*/
fetchCurrentTeam: async () => {
const { teamSlug, _client } = get()
const team = await _client.team.getBySlug(teamSlug);
console.log({team})
return team;
},
});
15 changes: 12 additions & 3 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { CoreDomainClient } from "@opensystemslab/planx-core";
import { Team, User, UserTeams } from "@opensystemslab/planx-core/types";
import axios from "axios";
import { _client } from "client";
import type { StateCreator } from "zustand";

import { EditorStore } from "./editor";

export interface UserStore {
user?: User;
jwt?: string;
Expand All @@ -13,11 +15,18 @@ export interface UserStore {
initUserStore: () => Promise<void>;
}

export const userStore: StateCreator<UserStore, [], [], UserStore> = (
export const userStore: StateCreator<UserStore & EditorStore, [], [], UserStore> = (
set,
get,
) => ({
setUser: ({ jwt, ...user }) => set({ jwt, user }),
setUser: ({ jwt, ...user }) => {
const authenticatedClient = new CoreDomainClient({
targetURL: process.env.REACT_APP_HASURA_URL!,
auth: { jwt },
});
set({client: authenticatedClient });
set({ jwt, user })
},

getUser: () => get().user,

Expand Down

0 comments on commit edf30b9

Please sign in to comment.