From 2a8b04ab0e4077ff2ee58ffe3acfc9ed1061de19 Mon Sep 17 00:00:00 2001 From: Yaroslav Boiko Date: Thu, 9 Jan 2025 17:58:52 +0100 Subject: [PATCH] [OPIK-737] abstract extracting entity id from headers --- .../src/api/datasets/useDatasetCreateMutation.ts | 5 ++--- .../src/api/projects/useProjectCreateMutation.ts | 9 ++++++--- apps/opik-frontend/src/lib/utils.ts | 4 ++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/apps/opik-frontend/src/api/datasets/useDatasetCreateMutation.ts b/apps/opik-frontend/src/api/datasets/useDatasetCreateMutation.ts index 165d1399a7..74c4649cf1 100644 --- a/apps/opik-frontend/src/api/datasets/useDatasetCreateMutation.ts +++ b/apps/opik-frontend/src/api/datasets/useDatasetCreateMutation.ts @@ -1,11 +1,10 @@ import { useMutation, useQueryClient } from "@tanstack/react-query"; import { AxiosError } from "axios"; import get from "lodash/get"; -import last from "lodash/last"; - import api, { DATASETS_REST_ENDPOINT } from "@/api/api"; import { Dataset } from "@/types/datasets"; import { useToast } from "@/components/ui/use-toast"; +import { extractIdFromLocation } from "@/lib/utils"; type UseDatasetCreateMutationParams = { dataset: Partial; @@ -31,7 +30,7 @@ const useDatasetCreateMutation = () => { ? data : { ...dataset, - id: last(headers?.location?.split("/")), + id: extractIdFromLocation(headers?.location), }; }, onMutate: async (params: UseDatasetCreateMutationParams) => { diff --git a/apps/opik-frontend/src/api/projects/useProjectCreateMutation.ts b/apps/opik-frontend/src/api/projects/useProjectCreateMutation.ts index a0f3e7beea..e9fdf76f55 100644 --- a/apps/opik-frontend/src/api/projects/useProjectCreateMutation.ts +++ b/apps/opik-frontend/src/api/projects/useProjectCreateMutation.ts @@ -4,7 +4,7 @@ import api, { PROJECTS_REST_ENDPOINT } from "@/api/api"; import { Project } from "@/types/projects"; import { AxiosError } from "axios"; import { useToast } from "@/components/ui/use-toast"; -import { last } from "lodash"; +import { extractIdFromLocation } from "@/lib/utils"; type UseProjectCreateMutationParams = { project: Partial; @@ -16,11 +16,14 @@ const useProjectCreateMutation = () => { return useMutation({ mutationFn: async ({ project }: UseProjectCreateMutationParams) => { - const res = await api.post(PROJECTS_REST_ENDPOINT, { + const { data, headers } = await api.post(PROJECTS_REST_ENDPOINT, { ...project, }); - return { ...res.data, id: last(res.headers?.location?.split("/")) }; + // TODO workaround to return just created resource while implementation on BE is not done + const id = extractIdFromLocation(headers?.location); + + return { ...data, id }; }, onError: (error: AxiosError) => { const message = get( diff --git a/apps/opik-frontend/src/lib/utils.ts b/apps/opik-frontend/src/lib/utils.ts index 154fa9ea9f..faa4e5bea5 100644 --- a/apps/opik-frontend/src/lib/utils.ts +++ b/apps/opik-frontend/src/lib/utils.ts @@ -7,6 +7,7 @@ import sample from "lodash/sample"; import mapKeys from "lodash/mapKeys"; import snakeCase from "lodash/snakeCase"; import { DEFAULT_WORKSPACE_NAME } from "@/constants/user"; +import { last } from "lodash"; const BASE_DOCUMENTATION_URL = "https://www.comet.com/docs/opik"; @@ -81,3 +82,6 @@ export const calculateWorkspaceName = ( workspaceName: string, defaultName = "Personal", ) => (workspaceName === DEFAULT_WORKSPACE_NAME ? defaultName : workspaceName); + +export const extractIdFromLocation = (location: string) => + last(location?.split("/"));