From e81f7b2f09181944b5552446deb34c582f009dee Mon Sep 17 00:00:00 2001 From: Eren Date: Wed, 27 Nov 2024 16:50:26 +0300 Subject: [PATCH] feat(templates/refund-fee): add details --- .../templates/refund-fees/[id]/edit.tsx | 304 ------------------ .../templates/refund-fees/[id]/form.tsx | 50 +++ .../templates/refund-fees/[id]/page.tsx | 58 ++-- .../templates/refund-fees/[id]/preview.tsx | 3 - .../templates/refund-fees/[id]/table.tsx | 97 ++++++ .../templates/refund-tables/[id]/page.tsx | 4 +- .../templates/refund-tables/[id]/table.tsx | 2 +- .../actions/ContractService/post-actions.ts | 13 + .../app/[lang]/app/actions/api-requests.ts | 7 + 9 files changed, 193 insertions(+), 345 deletions(-) delete mode 100644 apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/edit.tsx create mode 100644 apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/form.tsx delete mode 100644 apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/preview.tsx create mode 100644 apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/table.tsx diff --git a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/edit.tsx b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/edit.tsx deleted file mode 100644 index ac45098a1..000000000 --- a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/edit.tsx +++ /dev/null @@ -1,304 +0,0 @@ -"use client"; -import { - AlertDialog, - AlertDialogAction, - AlertDialogCancel, - AlertDialogContent, - AlertDialogDescription, - AlertDialogFooter, - AlertDialogHeader, - AlertDialogTitle, - AlertDialogTrigger, -} from "@/components/ui/alert-dialog"; -import { Button } from "@/components/ui/button"; -import { toast } from "@/components/ui/sonner"; -import type { - UniRefund_ContractService_Refunds_RefundFeeDetails_RefundFeeDetailCreateDto as RefundFeeDetailCreateDto, - UniRefund_ContractService_Refunds_RefundFeeDetails_RefundFeeDetailDto as RefundFeeDetailDto, - UniRefund_ContractService_Refunds_RefundFeeDetails_RefundFeeDetailUpdateDto as RefundFeeDetailUpdateDto, - UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderDto as RefundFeeHeaderDto, - UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderUpdateDto as RefundFeeHeaderUpdateDto, -} from "@ayasofyazilim/saas/ContractService"; -import { - $UniRefund_ContractService_Refunds_RefundFeeDetails_RefundFeeDetailCreateDto as detailCreateSchema, - $UniRefund_ContractService_Refunds_RefundFeeDetails_RefundFeeDetailDto as detailSchema, - $UniRefund_ContractService_Refunds_RefundFeeDetails_RefundFeeDetailUpdateDto as detailUpdateSchema, - $UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderUpdateDto as headerUpdateSchema, -} from "@ayasofyazilim/saas/ContractService"; -import { createZodObject } from "@repo/ayasofyazilim-ui/lib/create-zod-object"; -import type { TableActionCustomDialog } from "@repo/ayasofyazilim-ui/molecules/dialog"; -import type { ColumnsType } from "@repo/ayasofyazilim-ui/molecules/tables/types"; -import DataTable from "@repo/ayasofyazilim-ui/molecules/tables"; -import AutoForm, { - AutoFormSubmit, -} from "@repo/ayasofyazilim-ui/organisms/auto-form"; -import { useRouter } from "next/navigation"; -import { useState } from "react"; -import type { ContractServiceResource } from "src/language-data/ContractService"; -import { getBaseLink } from "src/utils"; -import { - deleteRefundTableFeeHeaderDetailsById, - deleteRefundTableFeeHeadersById, - postRefundTableFeeHeaderDetailsByRefundTableHeaderId, - putRefundTableFeeHeaderDetailsById, - putRefundTableFeeHeadersById, -} from "../../refund/action"; - -export default function Edit({ - details, - languageData, -}: { - details: RefundFeeHeaderDto; - languageData: ContractServiceResource; -}) { - const [loading, setLoading] = useState(false); - const router = useRouter(); - const [dialogOpen, setDialogOpen] = useState(false); - - const handleSubmit = (data: RefundFeeHeaderUpdateDto) => { - setLoading(true); - void putRefundTableFeeHeadersById({ - id: details.id || "", - requestBody: data, - }) - .then((response) => { - if (response.type === "success") { - toast.success( - response.message || "Refund fee header updated successfully", - ); - } else if (response.type === "api-error") { - toast.error(response.message || "Refund fee header update failed"); - } else { - toast.error("Fatal error"); - } - }) - .finally(() => { - setLoading(false); - }); - }; - const handleDelete = () => { - setLoading(true); - setDialogOpen(false); - void deleteRefundTableFeeHeadersById({ id: details.id || "" }).then( - (response) => { - if (response.type === "success") { - toast.success( - response.message || "Refund fee header deleted successfully", - ); - router.push(getBaseLink("app/admin/settings/templates/refund-fees")); - } else if (response.type === "api-error") { - toast.error(response.message || "Refund fee header delete failed"); - } else { - toast.error("Fatal error"); - } - }, - ); - }; - - const handleSetupDelete = (row: RefundFeeDetailDto) => { - setLoading(true); - void deleteRefundTableFeeHeaderDetailsById({ id: row.id || "" }) - .then((response) => { - if (response.type === "success") { - toast.success( - response.message || "Refund fee setup deleted successfully", - ); - router.refresh(); - } else if (response.type === "api-error") { - toast.error( - response.message || "Refund fee setup header delete failed", - ); - } else { - toast.error("Fatal error"); - } - }) - .finally(() => { - setLoading(false); - }); - }; - const handleSetupUpdate = ( - row: RefundFeeDetailUpdateDto, - originalRow: RefundFeeDetailDto, - ) => { - setLoading(true); - void putRefundTableFeeHeaderDetailsById({ - id: originalRow.id || "", - requestBody: row, - }) - .then((response) => { - if (response.type === "success") { - toast.success( - response.message || "Refund fee setup updated successfully", - ); - router.refresh(); - } else if (response.type === "api-error") { - toast.error( - response.message || "Refund fee setup header update failed", - ); - } else { - toast.error("Fatal error"); - } - }) - .finally(() => { - setLoading(false); - }); - }; - const handleSetupCreate = (row: RefundFeeDetailCreateDto) => { - setLoading(true); - void postRefundTableFeeHeaderDetailsByRefundTableHeaderId({ - id: details.id || "", - requestBody: row, - }) - .then((response) => { - if (response.type === "success") { - toast.success( - response.message || "Refund fee setup created successfully", - ); - router.refresh(); - } else if (response.type === "api-error") { - toast.error( - response.message || "Refund fee setup header create failed", - ); - } else { - toast.error("Fatal error"); - } - }) - .finally(() => { - setLoading(false); - }); - }; - const columnsData: ColumnsType = { - type: "Auto", - data: { - tableType: detailSchema, - excludeList: [], - positions: [ - "amountFrom", - "amountTo", - "fixedFeeValue", - "percentFeeValue", - "minFee", - "maxFee", - ], - actionList: [ - { - type: "Sheet", - cta: languageData["RefundFees.Page.Edit.Fee.Edit"], - description: - languageData["RefundFees.Page.Edit.Fee.Edit.Description"], - autoFormArgs: { - formSchema: createZodObject(detailUpdateSchema, undefined), - submit: { - cta: languageData["RefundFees.Page.Edit.Fee.Edit.Save"], - }, - }, - callback: (row: unknown, originalRow: unknown) => { - handleSetupUpdate( - row as RefundFeeDetailUpdateDto, - originalRow as RefundFeeDetailDto, - ); - }, - componentType: "Autoform", - }, - { - type: "Action", - cta: languageData["RefundFees.Page.Edit.Fee.Delete"], - callback: handleSetupDelete, - }, - ], - }, - }; - const setupRefund: TableActionCustomDialog = { - type: "Dialog", - cta: languageData["RefundFees.Page.Edit.Fee.Create"], - description: languageData["RefundFees.Page.Edit.Fee.Create.Description"], - autoFormArgs: { - formSchema: createZodObject(detailCreateSchema), - fieldConfig: { - refundFeeHeaderId: { - containerClassName: "hidden", - }, - }, - submit: { - cta: languageData["RefundFees.Page.Edit.Fee.Create.Save"], - }, - }, - componentType: "Autoform", - callback: handleSetupCreate, - }; - - return ( - //
- <> - { - handleSubmit(values as RefundFeeHeaderUpdateDto); - }} - values={details} - > -
- - - - - - - - {languageData["RefundFees.Page.Edit.Delete.Title"]} - - - {languageData["RefundFees.Page.Edit.Delete.Description"]} - - - - { - setDialogOpen(false); - }} - > - {languageData["RefundFees.Page.Edit.Delete.Cancel"]} - - - {languageData["RefundFees.Page.Edit.Delete.Confirm"]} - - - - - - {languageData["RefundFees.Page.Edit.Save"]} - -
-
- - {/*
*/} - - ); -} diff --git a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/form.tsx b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/form.tsx new file mode 100644 index 000000000..0eb70878c --- /dev/null +++ b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/form.tsx @@ -0,0 +1,50 @@ +"use client"; +import type { + UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderDto, + UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderUpdateDto, +} from "@ayasofyazilim/saas/ContractService"; +import { $UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderUpdateDto } from "@ayasofyazilim/saas/ContractService"; +import { SchemaForm } from "@repo/ayasofyazilim-ui/organisms/schema-form"; +import { createUiSchemaWithResource } from "@repo/ayasofyazilim-ui/organisms/schema-form/utils"; +import { useRouter } from "next/navigation"; +import { handlePutResponse } from "src/app/[lang]/app/actions/api-utils-client"; +import { putRefundFeeHeadersApi } from "src/app/[lang]/app/actions/ContractService/put-actions"; +import type { ContractServiceResource } from "src/language-data/ContractService"; + +function Form({ + response, + languageData, +}: { + response: UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderDto; + languageData: ContractServiceResource; +}) { + const router = useRouter(); + const uiSchema = createUiSchemaWithResource({ + resources: languageData, + schema: + $UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderUpdateDto, + }); + return ( + { + const formData = { + id: response.id, + requestBody: + data.formData as UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderUpdateDto, + }; + void putRefundFeeHeadersApi(formData).then((res) => { + handlePutResponse(res, router); + }); + }} + schema={ + $UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderUpdateDto + } + submitText={languageData.Save} + uiSchema={uiSchema} + /> + ); +} + +export default Form; diff --git a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/page.tsx b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/page.tsx index 03dc482f4..ed805aa60 100644 --- a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/page.tsx +++ b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/page.tsx @@ -1,44 +1,32 @@ -import { - SectionLayout, - SectionLayoutContent, -} from "@repo/ayasofyazilim-ui/templates/section-layout-v2"; +import { notFound } from "next/navigation"; +import { getRefundFeeHeadersByIdApi } from "src/app/[lang]/app/actions/ContractService/action"; import { getResourceData } from "src/language-data/ContractService"; -import { getRefundTableFeeHeadersDetailById } from "../../refund/action"; -import Edit from "./edit"; -import Preview from "./preview"; +import Form from "./form"; +import RefundFeeDetailsForm from "./table"; export default async function Page({ params, }: { - params: { id: string; lang: string; type: string }; -}) { - const details = await getRefundTableFeeHeadersDetailById({ id: params.id }); + params: { lang: string; id: string }; +}): Promise { + const response = await getRefundFeeHeadersByIdApi({ id: params.id }); + if (response.type !== "success") return notFound(); + const { languageData } = await getResourceData(params.lang); - if (details.type === "api-error") { - return <>Api Error; - } - if (details.type === "error") { - return <>Error; - } + return ( - - - - - - - - + <> +
+ +
+ {response.data.name} +
+
+ {languageData["RefundTables.Edit.Description"]} +
+ ); } diff --git a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/preview.tsx b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/preview.tsx deleted file mode 100644 index 0b63bf425..000000000 --- a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/preview.tsx +++ /dev/null @@ -1,3 +0,0 @@ -export default function Preview() { - return
Preview will be here
; -} diff --git a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/table.tsx b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/table.tsx new file mode 100644 index 000000000..1dca74d33 --- /dev/null +++ b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-fees/[id]/table.tsx @@ -0,0 +1,97 @@ +"use client"; +// import { UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderDto } from "@ayasofyazilim/saas/ContractService"; +// import type { +// UniRefund_ContractService_Refunds_RefundFeeDetails_RefundFeeDetailCreateDto, +// UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderDto, +// } from "@ayasofyazilim/saas/ContractService"; + +// import { $UniRefund_ContractService_Refunds_RefundFeeDetails_RefundFeeDetailCreateByListDto } from "@ayasofyazilim/saas/ContractService"; +// import { tanstackTableEditableColumnsByRowData } from "@repo/ayasofyazilim-ui/molecules/tanstack-table/utils"; +// import { SchemaForm } from "@repo/ayasofyazilim-ui/organisms/schema-form"; +// import { TableField } from "@repo/ayasofyazilim-ui/organisms/schema-form/fields"; +// import { PlusCircle, Trash2 } from "lucide-react"; +// import { useRouter } from "next/navigation"; +// import { handlePostResponse } from "src/app/[lang]/app/actions/api-utils-client"; +// import { postRefundFeeHeadersRefundFeeDetailsApi } from "src/app/[lang]/app/actions/ContractService/post-actions"; +// import type { ContractServiceResource } from "src/language-data/ContractService"; + +// type TypeWithId = Type & { +// id: IdType; +// }; + +export default function RefundFeeDetailsForm() { + // { + // // response, + // // languageData, + // }: { + // // response: UniRefund_ContractService_Refunds_RefundFeeHeaders_RefundFeeHeaderDto; + // // languageData: ContractServiceResource; + // }, + return null; + // const router = useRouter(); + // const RebateFeeColumns = tanstackTableEditableColumnsByRowData< + // TypeWithId + // >({ + // rows: $UniRefund_ContractService_Refunds_RefundFeeDetails_RefundFeeDetailCreateByListDto + // .properties.refundFeeDetails.items.properties, + // excludeColumns: ["extraProperties"], + // }); + // return ( + // + // >({ + // editable: true, + // showPagination: false, + // columns: RebateFeeColumns, + // data: response.refundFeeDetails || [], + // fillerColumn: "id", + // tableActions: [ + // { + // type: "create-row", + // actionLocation: "table", + // cta: languageData[ + // "RebateTables.Templates.Form.rebateTableDetails.add" + // ], + // icon: PlusCircle, + // }, + // ], + // rowActions: [ + // { + // actionLocation: "row", + // cta: languageData[ + // "RebateTables.Templates.Form.rebateTableDetails.delete" + // ], + // icon: Trash2, + // type: "delete-row", + // }, + // ], + // }), + // }} + // formData={response.refundFeeDetails || []} + // onSubmit={(data) => { + // const formData = (data.formData || + // []) as UniRefund_ContractService_Refunds_RefundFeeDetails_RefundFeeDetailCreateDto[]; + + // void postRefundFeeHeadersRefundFeeDetailsApi({ + // id: response.id, + // requestBody: { + // // @ts-expect-error UNI-582 + // refundFeeDetails: formData, + // }, + // }).then((res) => { + // handlePostResponse(res, router); + // }); + // }} + // schema={ + // $UniRefund_ContractService_Refunds_RefundFeeDetails_RefundFeeDetailCreateByListDto + // .properties.refundFeeDetails + // } + // submitText={languageData["RefundTables.Create.Submit"]} + // uiSchema={{ + // "ui:field": "RebateTable", + // }} + // /> + // ); +} diff --git a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-tables/[id]/page.tsx b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-tables/[id]/page.tsx index 1b7c3e9b4..85c95aae4 100644 --- a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-tables/[id]/page.tsx +++ b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-tables/[id]/page.tsx @@ -2,7 +2,7 @@ import { notFound } from "next/navigation"; import { getResourceData } from "src/language-data/ContractService"; import { getRefundTableHeadersById } from "../../refund/action"; import Form from "./form"; -import RefundTablesRuleForm from "./table"; +import RefundTableDetailsForm from "./table"; export default async function Page({ params, @@ -17,7 +17,7 @@ export default async function Page({ return ( <> - diff --git a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-tables/[id]/table.tsx b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-tables/[id]/table.tsx index 0e8b0ba6d..c5e65bd7c 100644 --- a/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-tables/[id]/table.tsx +++ b/apps/web/src/app/[lang]/app/[type]/settings/templates/refund-tables/[id]/table.tsx @@ -17,7 +17,7 @@ type TypeWithId = Type & { id: IdType; }; -export default function RefundTablesRuleForm({ +export default function RefundTableDetailsForm({ response, languageData, }: { diff --git a/apps/web/src/app/[lang]/app/actions/ContractService/post-actions.ts b/apps/web/src/app/[lang]/app/actions/ContractService/post-actions.ts index 4ee03179d..f3037a5b5 100644 --- a/apps/web/src/app/[lang]/app/actions/ContractService/post-actions.ts +++ b/apps/web/src/app/[lang]/app/actions/ContractService/post-actions.ts @@ -2,6 +2,7 @@ import type { PostApiContractServiceRebateTablesRebateTableHeadersTemplatesData, + PostApiContractServiceRefundTablesRefundFeeHeadersByIdRefundFeeDetailsData, PostApiContractServiceRefundTablesRefundFeeHeadersData, PostApiContractServiceRefundTablesRefundTableHeadersByIdRefundTableDetailsData, PostApiContractServiceRefundTablesRefundTableHeadersData, @@ -54,3 +55,15 @@ export async function postRefundFeeHeadersApi( return structuredError(error); } } +export async function postRefundFeeHeadersRefundFeeDetailsApi( + data: PostApiContractServiceRefundTablesRefundFeeHeadersByIdRefundFeeDetailsData, +) { + try { + const requests = await getApiRequests(); + const response = + await requests.templates.postRefundFeeHeadersRefundTableDetails(data); + return structuredResponse(response); + } catch (error) { + return structuredError(error); + } +} diff --git a/apps/web/src/app/[lang]/app/actions/api-requests.ts b/apps/web/src/app/[lang]/app/actions/api-requests.ts index 4aee96293..292661dca 100644 --- a/apps/web/src/app/[lang]/app/actions/api-requests.ts +++ b/apps/web/src/app/[lang]/app/actions/api-requests.ts @@ -16,6 +16,7 @@ import type { PostApiContractServiceMerchantsByIdContractsContractHeadersData, PostApiContractServiceMerchantsContractsContractHeadersByIdContractSettingsData, PostApiContractServiceRebateTablesRebateTableHeadersTemplatesData, + PostApiContractServiceRefundTablesRefundFeeHeadersByIdRefundFeeDetailsData, PostApiContractServiceRefundTablesRefundFeeHeadersData, PostApiContractServiceRefundTablesRefundTableHeadersByIdRefundTableDetailsData, PostApiContractServiceRefundTablesRefundTableHeadersData, @@ -741,6 +742,12 @@ export async function getApiRequests() { await contractsClient.refundTables.getApiContractServiceRefundTablesRefundFeeHeadersDetailById( data, ), + postRefundFeeHeadersRefundTableDetails: async ( + data: PostApiContractServiceRefundTablesRefundFeeHeadersByIdRefundFeeDetailsData, + ) => + await contractsClient.refundTables.postApiContractServiceRefundTablesRefundFeeHeadersByIdRefundFeeDetails( + data, + ), postRefundFeeHeaders: async ( data: PostApiContractServiceRefundTablesRefundFeeHeadersData, ) =>