Skip to content

Commit

Permalink
Save custom-project response in query cache
Browse files Browse the repository at this point in the history
  • Loading branch information
atrincas committed Dec 12, 2024
1 parent 51cd506 commit 91f9e4a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
12 changes: 0 additions & 12 deletions client/src/app/projects/preview/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,10 @@ import {
QueryClient,
} from "@tanstack/react-query";

import mockResponse from "@/app/projects/preview/mock-response";

import CustomProject from "@/containers/projects/custom-project";

export const CUSTOM_PROJECT_MOCK_QUERYKEY = ["custom-project-mock-data"];
export default function CustomProjectPreviewPage() {
/**
*
* Mimic queryCache from createCustomProject response
*
* This is only for testing purpose with the current mock data,
* and should be replaced when the complete user flow is done
*/

const queryClient = new QueryClient();
queryClient.setQueryData(CUSTOM_PROJECT_MOCK_QUERYKEY, mockResponse);

return (
<HydrationBoundary state={dehydrate(queryClient)}>
Expand Down
8 changes: 7 additions & 1 deletion client/src/containers/projects/new/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { useFormContext } from "react-hook-form";

import { useRouter } from "next/navigation";

import { CreateCustomProjectForm } from "@/containers/projects/form/setup";

import { Button } from "@/components/ui/button";
Expand All @@ -11,6 +13,7 @@ import { onSubmit } from "./index";

export default function Header() {
const methods = useFormContext<CreateCustomProjectForm>();
const router = useRouter();

return (
<div className="flex items-center justify-between py-3 pr-6">
Expand All @@ -22,7 +25,10 @@ export default function Header() {
<Button variant="secondary">Cancel</Button>
<Button
// disabled={!methods.formState.isValid}
onClick={methods.handleSubmit(onSubmit)}
onClick={() => {
methods.handleSubmit((data) => onSubmit(data))();
router.push("/projects/preview");
}}
>
Continue
</Button>
Expand Down
9 changes: 7 additions & 2 deletions client/src/containers/projects/new/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import ProjectSidebar from "@/containers/projects/new/sidebar";

import { ScrollArea } from "@/components/ui/scroll-area";

export const onSubmit = (data: CreateCustomProjectForm) => {
export const onSubmit = async (data: CreateCustomProjectForm) => {
const queryClient = getQueryClient();

const originalValues = { ...data };
Expand Down Expand Up @@ -97,7 +97,12 @@ export const onSubmit = (data: CreateCustomProjectForm) => {
},
};

client.customProjects.createCustomProject.mutation({ body: data });
const { status, body } =
await client.customProjects.createCustomProject.mutation({ body: data });

if (status === 201) {
queryClient.setQueryData(queryKeys.customProjects.cached.queryKey, body);
}
};

export default function CreateCustomProject() {
Expand Down
4 changes: 1 addition & 3 deletions client/src/hooks/use-get-custom-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import { client } from "@/lib/query-client";
import { queryKeys } from "@/lib/query-keys";
import { getAuthHeader } from "@/lib/utils";

import { CUSTOM_PROJECT_MOCK_QUERYKEY } from "@/app/projects/preview/page";

export function useGetCustomProject(id?: string) {
const { data: session } = useSession();
const queryCache = useQueryClient().getQueryData<{
data: InstanceType<typeof CustomProjectEntity>;
}>(CUSTOM_PROJECT_MOCK_QUERYKEY);
}>(queryKeys.customProjects.cached.queryKey);
const getCustomProjectQuery = client.customProjects.getCustomProject.useQuery(
queryKeys.customProjects.one(id || "").queryKey,
{
Expand Down
1 change: 1 addition & 0 deletions client/src/lib/query-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const tableKeys = createQueryKeys("tables", {
});

export const customProjectKeys = createQueryKeys("customProjects", {
cached: null,
countries: null,
one: (id: string) => [id],
assumptions: ({
Expand Down

0 comments on commit 91f9e4a

Please sign in to comment.