diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/store/editor.ts b/editor.planx.uk/src/pages/FlowEditor/lib/store/editor.ts index 82d17a78c5..3c525fa937 100644 --- a/editor.planx.uk/src/pages/FlowEditor/lib/store/editor.ts +++ b/editor.planx.uk/src/pages/FlowEditor/lib/store/editor.ts @@ -16,6 +16,7 @@ import debounce from "lodash/debounce"; import isEmpty from "lodash/isEmpty"; import omitBy from "lodash/omitBy"; import type { FlowSettings, TextContent } from "types"; +import { getLoggedInUserId } from "utils"; import type { GetState, SetState } from "zustand/vanilla"; import { FlowLayout } from "../../components/Flow"; @@ -147,11 +148,23 @@ export const editorStore = ( createFlow: async (teamId, newSlug) => { const data = { [ROOT_NODE_KEY]: { edges: [] } }; + const creatorId = getLoggedInUserId(); let response = (await client.mutate({ mutation: gql` - mutation CreateFlow($data: jsonb, $slug: String, $teamId: Int) { + mutation CreateFlow( + $data: jsonb + $slug: String + $teamId: Int + $creatorId: Int + ) { insert_flows_one( - object: { data: $data, slug: $slug, team_id: $teamId, version: 1 } + object: { + data: $data + slug: $slug + team_id: $teamId + version: 1 + creator_id: $creatorId + } ) { id data @@ -162,6 +175,7 @@ export const editorStore = ( slug: newSlug, teamId, data, + creatorId, }, })) as any; diff --git a/editor.planx.uk/src/utils.ts b/editor.planx.uk/src/utils.ts index 8e8bdc29e6..1c5f04bc06 100644 --- a/editor.planx.uk/src/utils.ts +++ b/editor.planx.uk/src/utils.ts @@ -1,3 +1,7 @@ +import jwtDecode from "jwt-decode"; + +import { getCookie } from "./lib/cookie"; + export function removeAt(index: number, arr: Array): Array { return arr.filter((_item, i) => { return i !== index; @@ -52,3 +56,12 @@ export function slugify(name: string): string { .replace(/[\s_-]+/g, "-") // swap any length of whitespace, underscore, hyphen characters with a single - .replace(/^-+|-+$/g, ""); // remove leading, trailing - } + +export function getLoggedInUserId(): number | undefined { + const jwt = getCookie("jwt"); + if (!jwt) return; + const userId = Number( + (jwtDecode(jwt) as any)["https://hasura.io/jwt/claims"]["x-hasura-user-id"] + ); + return userId; +}