Skip to content

Commit

Permalink
refactor(Contract): convert sections to page (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusualhashash authored Dec 3, 2024
2 parents 47f79f2 + b418304 commit fbec0a3
Show file tree
Hide file tree
Showing 13 changed files with 395 additions and 333 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { UniRefund_ContractService_ContractsForMerchant_ContractHeaders_ContractHeaderDetailForMerchantDto as ContractHeaderDetailForMerchantDto } from "@ayasofyazilim/saas/ContractService";
import { notFound } from "next/navigation";
import type { UniRefund_LocationService_AddressCommonDatas_AddressCommonDataDto as AddressCommonDataDto } from "@ayasofyazilim/saas/LocationService";
import { getResourceData } from "src/language-data/ContractService";
import { getMerchantContractHeaderContractSettingsByHeaderIdApi } from "src/app/[lang]/app/actions/ContractService/action";
import { ContractSettings } from "./contract-settings";

export default async function ContractSettingsPage({
lang,
contractId,
contractHeaderDetails,
addressList,
}: {
lang: string;
contractId: string;
contractHeaderDetails: ContractHeaderDetailForMerchantDto;
addressList: AddressCommonDataDto[];
}) {
const { languageData } = await getResourceData(lang);
const contractSettings =
await getMerchantContractHeaderContractSettingsByHeaderIdApi({
id: contractId,
});
if (contractSettings.type !== "success") return notFound();
return (
<ContractSettings
addressList={addressList}
contractHeaderDetails={contractHeaderDetails}
contractSettings={contractSettings.data}
lang={lang}
languageData={languageData}
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use client";
import { Button } from "@/components/ui/button";
import {
Dialog,
Expand All @@ -12,21 +13,22 @@ import { toast } from "@/components/ui/sonner";
import { cn } from "@/lib/utils";
import type {
UniRefund_ContractService_ContractsForMerchant_ContractSettings_ContractSettingCreateDto as ContractSettingCreateUpdateDto,
UniRefund_ContractService_ContractsForMerchant_ContractHeaders_ContractHeaderDetailForMerchantDto as ContractHeaderDetailForMerchantDto,
UniRefund_ContractService_ContractsForMerchant_ContractSettings_ContractSettingDto as ContractSettingDto,
PagedResultDto_ContractSettingDto,
} from "@ayasofyazilim/saas/ContractService";
import { $UniRefund_ContractService_ContractsForMerchant_ContractSettings_ContractSettingCreateDto as $ContractSettingCreateUpdateDto } from "@ayasofyazilim/saas/ContractService";
import type { UniRefund_LocationService_AddressCommonDatas_AddressCommonDataDto as AddressCommonDataDto } from "@ayasofyazilim/saas/LocationService";
import TanstackTable from "@repo/ayasofyazilim-ui/molecules/tanstack-table";
import { tanstackTableCreateColumnsByRowData } from "@repo/ayasofyazilim-ui/molecules/tanstack-table/utils";
import { SchemaForm } from "@repo/ayasofyazilim-ui/organisms/schema-form";
import {
bulkCreateUiSchema,
createUiSchemaWithResource,
} from "@repo/ayasofyazilim-ui/organisms/schema-form/utils";
import { SectionLayoutContent } from "@repo/ayasofyazilim-ui/templates/section-layout-v2";
import type { Dispatch, SetStateAction } from "react";
import { useCallback, useState } from "react";
import type { TanstackTableTableActionsType } from "@repo/ayasofyazilim-ui/molecules/tanstack-table/types";
import type { UniRefund_LocationService_AddressCommonDatas_AddressCommonDataDto as AddressCommonDataDto } from "@ayasofyazilim/saas/LocationService";
import {
deleteMerchantContractContractSettingsByIdApi,
getMerchantContractHeaderContractSettingsByHeaderIdApi as getContractSettings,
Expand All @@ -35,8 +37,7 @@ import {
putMerchantContractContractSettingsByIdApi,
} from "src/app/[lang]/app/actions/ContractService/action";
import type { ContractServiceResource } from "src/language-data/ContractService";
import { MerchantAddressWidget } from "../contract-widgets";
import type { SectionProps } from "./details";
import { MerchantAddressWidget } from "../../../contract-widgets";

interface ContractSettingsTable {
id: string;
Expand All @@ -58,18 +59,23 @@ const $ContractSettingsTable = {
type: "string",
},
};
export function ContractSettingsSection({
export function ContractSettings({
languageData,
contractSettings,
contractHeaderDetails,
addresses,
addressList,
lang,
loading,
setLoading,
}: SectionProps) {
const { data } = contractSettings;
}: {
languageData: ContractServiceResource;
contractSettings: PagedResultDto_ContractSettingDto;
contractHeaderDetails: ContractHeaderDetailForMerchantDto;
addressList: AddressCommonDataDto[];
lang: string;
}) {
const [loading, setLoading] = useState(false);
const { items } = contractSettings;
const [settings, setSettings] = useState<ContractSettingsTable[]>(
data.items?.map((item) => {
items?.map((item) => {
return {
id: item.id,
name: item.name,
Expand Down Expand Up @@ -132,7 +138,7 @@ export function ContractSettingsSection({
(row: ContractSettingsTable) => {
return (
<SchemaFormForContractSettings
addressList={addresses}
addressList={addressList}
formData={{
...row.details,
invoicingAddressCommonDataId:
Expand All @@ -148,7 +154,7 @@ export function ContractSettingsSection({
/>
);
},
[addresses, tempSettings, contractHeaderDetails],
[addressList, tempSettings, contractHeaderDetails],
);

const ColumnIsDefault = useCallback((row: ContractSettingsTable) => {
Expand Down Expand Up @@ -209,7 +215,7 @@ export function ContractSettingsSection({
]
: undefined;
return (
<SectionLayoutContent className="px-5 py-0" sectionId="contract-setting">
<>
{settings.length > 0 ? (
<TanstackTable
columnVisibility={{
Expand All @@ -224,7 +230,7 @@ export function ContractSettingsSection({
/>
) : (
<SchemaFormForContractSettings
addressList={addresses}
addressList={addressList}
formData={{}}
handleFetch={handleFetch}
languageData={languageData}
Expand All @@ -234,7 +240,7 @@ export function ContractSettingsSection({
type="create"
/>
)}
</SectionLayoutContent>
</>
);
}
function SchemaFormForContractSettings({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use client";

import type {
UniRefund_ContractService_ContractsForMerchant_ContractHeaders_ContractHeaderDetailForMerchantDto as ContractHeaderDetailForMerchantDto,
UniRefund_ContractService_ContractsForMerchant_ContractHeaders_ContractHeaderForMerchantUpdateDto as ContractHeaderForMerchantUpdateDto,
} from "@ayasofyazilim/saas/ContractService";
import type { UniRefund_LocationService_AddressCommonDatas_AddressCommonDataDto as AddressCommonDataDto } from "@ayasofyazilim/saas/LocationService";
import type { ContractServiceResource } from "src/language-data/ContractService";
import ContractHeaderForm from "../../../contract-header-form";

export function ContractHeader({
contractHeaderDetails,
partyName,
partyId,
addressList,
languageData,
}: {
contractHeaderDetails: ContractHeaderDetailForMerchantDto;
partyName: "merchants";
partyId: string;
addressList: AddressCommonDataDto[];
languageData: ContractServiceResource;
}) {
const refundTableHeaders =
contractHeaderDetails.contractHeaderRefundTableHeaders.map((header) => {
return {
refundTableHeaderId: header.refundTableHeader.id,
validFrom: header.validFrom,
validTo: header.validTo,
isDefault: header.isDefault,
};
});
return (
<ContractHeaderForm<ContractHeaderForMerchantUpdateDto>
addresses={addressList}
formData={{
...contractHeaderDetails,
status: contractHeaderDetails.status || "None",
addressCommonDataId: contractHeaderDetails.addressCommonData.id,
refundTableHeaders,
}}
formType="Update"
languageData={languageData}
partyId={partyId}
partyName={partyName}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { UniRefund_ContractService_ContractsForMerchant_ContractHeaders_ContractHeaderDetailForMerchantDto as ContractHeaderDetailForMerchantDto } from "@ayasofyazilim/saas/ContractService";
import type { UniRefund_LocationService_AddressCommonDatas_AddressCommonDataDto as AddressCommonDataDto } from "@ayasofyazilim/saas/LocationService";
import { getResourceData } from "src/language-data/ContractService";
import { ContractHeader } from "./contract-header";

export default async function ContractPage({
lang,
partyName,
partyId,
contractHeaderDetails,
addressList,
}: {
lang: string;
partyName: "merchants";
partyId: string;
contractHeaderDetails: ContractHeaderDetailForMerchantDto;
addressList: AddressCommonDataDto[];
}) {
const { languageData } = await getResourceData(lang);
return (
<ContractHeader
addressList={addressList}
contractHeaderDetails={contractHeaderDetails}
languageData={languageData}
partyId={partyId}
partyName={partyName}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip";
import {
SectionLayout,
SectionLayoutContent,
} from "@repo/ayasofyazilim-ui/templates/section-layout-v2";
import { Circle } from "lucide-react";
import Link from "next/link";
import { notFound } from "next/navigation";
import {
getMerchantContractHeaderByIdApi,
getMerchantContractHeaderMissingStepsByIdApi,
} from "src/app/[lang]/app/actions/ContractService/action";
import {
getResourceData,
type ContractServiceResource,
} from "src/language-data/ContractService";
import { getBaseLink } from "src/utils";
import { getAdressesApi } from "src/app/[lang]/app/actions/CrmService/actions";
import ContractPage from "./contract/contract-page";
import ContractSettingsPage from "./contract-settings/contract-settings-page";
import RebateSettingsPage from "./rebate-settings/rebate-settings-page";

export default async function Page({
params,
}: {
params: {
lang: string;
contractId: string;
partyName: "merchants";
partyId: string;
section: string;
};
}) {
const { lang, contractId, partyId, partyName, section } = params;
const { languageData } = await getResourceData(lang);
const missingSteps = await getMerchantContractHeaderMissingStepsByIdApi({
id: contractId,
});
const contractHeaderDetails =
await getMerchantContractHeaderByIdApi(contractId);

const addressList = await getAdressesApi(partyId, partyName);

if (
missingSteps.type !== "success" ||
contractHeaderDetails.type !== "success" ||
addressList.type !== "success"
) {
return notFound();
}
return (
<>
<SectionLayout
defaultActiveSectionId={section}
linkElement={Link}
sections={[
{
id: "contract",
link: "contract",
name: languageData["Contracts.Create.ContractHeader"],
disabled: false,
},
{
id: "rebate-settings",
link: "rebate-settings",
disabled: false,
...setSectionOptions(
"RebateSettings",
languageData["Contracts.Create.RebateSettings"],
languageData,
missingSteps.data,
),
},
{
id: "contract-settings",
link: "contract-settings",
disabled: false,
...setSectionOptions(
"ContractSetting",
languageData["Contracts.Create.ContractSettings"],
languageData,
missingSteps.data,
),
},
]}
vertical
>
<SectionLayoutContent sectionId={section}>
{section === "contract" && (
<ContractPage
{...params}
addressList={addressList.data}
contractHeaderDetails={contractHeaderDetails.data}
/>
)}
{section === "contract-settings" && (
<ContractSettingsPage
{...params}
addressList={addressList.data}
contractHeaderDetails={contractHeaderDetails.data}
/>
)}
{section === "rebate-settings" && <RebateSettingsPage {...params} />}
</SectionLayoutContent>
</SectionLayout>
<div className="hidden" id="page-title">
{languageData["Contracts.Edit.Title"]} - (
{contractHeaderDetails.data.name})
</div>
<div className="hidden" id="page-description">
{languageData["Contracts.Edit.Description"]}
</div>
<div className="hidden" id="page-back-link">
{getBaseLink(`/app/admin/parties/${partyName}/${partyId}`)}
</div>
</>
);
}

function BadgeWithTooltip({
label,
languageData,
}: {
label: string;
languageData: ContractServiceResource;
}) {
return (
<Tooltip>
<TooltipTrigger asChild>
<div className="flex items-center gap-2">
<Circle className="text-destructive w-3" />
<span>{label}</span>
</div>
</TooltipTrigger>
<TooltipContent>
{languageData["Contracts.MissingSteps.Missing"]}
</TooltipContent>
</Tooltip>
);
}
function setSectionOptions(
key: string,
label: string,
languageData: ContractServiceResource,
missingSteps: string[] = [],
) {
const isMissingStep = missingSteps.includes(key);
const options = {
name: label,
className: isMissingStep ? "data-[active=false]:text-xs" : "",
};
if (isMissingStep)
Object.assign(options, {
children: <BadgeWithTooltip label={label} languageData={languageData} />,
});
return options;
}
Loading

0 comments on commit fbec0a3

Please sign in to comment.