Skip to content

Commit

Permalink
[OPIK-737] abstract extracting entity id from headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav Boiko committed Jan 9, 2025
1 parent 0520487 commit 2a8b04a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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<Dataset>;
Expand All @@ -31,7 +30,7 @@ const useDatasetCreateMutation = () => {
? data
: {
...dataset,
id: last(headers?.location?.split("/")),
id: extractIdFromLocation(headers?.location),
};
},
onMutate: async (params: UseDatasetCreateMutationParams) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project>;
Expand All @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions apps/opik-frontend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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("/"));

0 comments on commit 2a8b04a

Please sign in to comment.