From 05ebc926dde88b1b39a64e6d5867731666af5199 Mon Sep 17 00:00:00 2001 From: "MSI\\2Fi" Date: Tue, 14 May 2024 16:27:20 +0800 Subject: [PATCH] Add validate when re-fetch --- src/app/(main)/settings/company/edit/page.tsx | 2 +- src/app/api/companys/actions.ts | 8 +++++++- src/app/api/companys/index.ts | 2 +- src/app/api/departments/actions.ts | 7 ++++++- src/app/api/positions/actions.ts | 12 ++++++++++-- src/components/CreateCompany/CompanyDetails.tsx | 2 +- .../CreateCompany/CreateCompanyWrapper.tsx | 4 ++-- 7 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/app/(main)/settings/company/edit/page.tsx b/src/app/(main)/settings/company/edit/page.tsx index 3ff9e17..8886a24 100644 --- a/src/app/(main)/settings/company/edit/page.tsx +++ b/src/app/(main)/settings/company/edit/page.tsx @@ -18,7 +18,7 @@ const Companys: React.FC = async ({searchParams}) => { return( <> - {t("Create Company")} + {t("Edit Company")} diff --git a/src/app/api/companys/actions.ts b/src/app/api/companys/actions.ts index 9afc9c7..0f5d249 100644 --- a/src/app/api/companys/actions.ts +++ b/src/app/api/companys/actions.ts @@ -3,6 +3,7 @@ import { serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil"; import { BASE_API_URL } from "@/config/api"; import { Dayjs } from "dayjs"; +import { revalidateTag } from "next/cache"; import { cache } from "react"; export interface comboProp { @@ -49,17 +50,21 @@ export interface EditCompanyInputs { } export const saveCompany = async (data: CreateCompanyInputs) => { - return serverFetchJson(`${BASE_API_URL}/companys/new`, { + const newCompany = serverFetchJson(`${BASE_API_URL}/companys/new`, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, }); + + revalidateTag("companys"); + return newCompany }; export const fetchCompanyCombo = cache(async () => { return serverFetchJson(`${BASE_API_URL}/companys/combo`, { next: { tags: ["company"] }, }); + }); export const deleteCompany = async (id: number) => { @@ -71,5 +76,6 @@ export const deleteCompany = async (id: number) => { }, ); + revalidateTag("companys"); return department }; diff --git a/src/app/api/companys/index.ts b/src/app/api/companys/index.ts index a517f38..bded71d 100644 --- a/src/app/api/companys/index.ts +++ b/src/app/api/companys/index.ts @@ -28,7 +28,7 @@ export const fetchCompanyDetails = cache(async (companyId: string) => { return serverFetchJson( `${BASE_API_URL}/companys/companyDetails/${companyId}`, { - next: { tags: [`departmentDetail${companyId}`] }, + next: { tags: [`departmentDetail`] }, }, ); }); \ No newline at end of file diff --git a/src/app/api/departments/actions.ts b/src/app/api/departments/actions.ts index 48a3174..5fde581 100644 --- a/src/app/api/departments/actions.ts +++ b/src/app/api/departments/actions.ts @@ -2,6 +2,7 @@ import { serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil"; import { BASE_API_URL } from "@/config/api"; +import { revalidateTag } from "next/cache"; import { cache } from "react"; @@ -21,11 +22,14 @@ export interface CreateDepartmentInputs { } export const saveDepartment = async (data: CreateDepartmentInputs) => { - return serverFetchJson(`${BASE_API_URL}/departments/new`, { + const newDepartment = serverFetchJson(`${BASE_API_URL}/departments/new`, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, }); + + revalidateTag("departments") + return newDepartment }; export const deleteDepartment = async (id: number) => { @@ -37,6 +41,7 @@ export const deleteDepartment = async (id: number) => { }, ); + revalidateTag("departments") return department }; diff --git a/src/app/api/positions/actions.ts b/src/app/api/positions/actions.ts index 3caf1ab..ab35842 100644 --- a/src/app/api/positions/actions.ts +++ b/src/app/api/positions/actions.ts @@ -4,6 +4,7 @@ import { serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil import { BASE_API_URL } from "@/config/api"; import { cache } from "react"; import { PositionResult } from "."; +import { revalidateTag } from "next/cache"; export interface comboProp { id: any; @@ -30,19 +31,25 @@ export interface EditPositionInputs { } export const savePosition = async (data: CreatePositionInputs) => { - return serverFetchJson(`${BASE_API_URL}/positions/new`, { + const newPosition = serverFetchJson(`${BASE_API_URL}/positions/new`, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, }); + + revalidateTag("positions") + return newPosition }; export const editPosition = async (data: EditPositionInputs) => { - return serverFetchJson(`${BASE_API_URL}/positions/new`, { + const editPostion = serverFetchJson(`${BASE_API_URL}/positions/new`, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, }); + + revalidateTag("positions") + return editPostion }; export const deletePosition = async (id: number) => { @@ -54,6 +61,7 @@ export const deletePosition = async (id: number) => { }, ); + revalidateTag("positions") return position }; diff --git a/src/components/CreateCompany/CompanyDetails.tsx b/src/components/CreateCompany/CompanyDetails.tsx index c1599f5..347580e 100644 --- a/src/components/CreateCompany/CompanyDetails.tsx +++ b/src/components/CreateCompany/CompanyDetails.tsx @@ -44,7 +44,7 @@ const CompanyDetails: React.FC = ({ getValues, } = useFormContext(); - console.log(content) + // console.log(content) useEffect(() => { setValue("normalHourFrom", convertTimeArrayToString(content.normalHourFrom, "HH:mm:ss", false)); diff --git a/src/components/CreateCompany/CreateCompanyWrapper.tsx b/src/components/CreateCompany/CreateCompanyWrapper.tsx index fa26f45..c4bb681 100644 --- a/src/components/CreateCompany/CreateCompanyWrapper.tsx +++ b/src/components/CreateCompany/CreateCompanyWrapper.tsx @@ -11,8 +11,8 @@ type Props = CreateCompanyProps | EditCompanyProps; const CreateCompanyWrapper: React.FC = async (props) => { - console.log(props) - + // console.log(props) + const companyDetails = props.isEdit ? await fetchCompanyDetails(props.companyId!) : undefined;