Skip to content

Commit

Permalink
refactor(traveller): edit the forms of edit and new identification (#915
Browse files Browse the repository at this point in the history
)
  • Loading branch information
yusualhashash authored Dec 4, 2024
2 parents 993da7c + 9bc76b3 commit cbedb15
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";

import { toast } from "@/components/ui/sonner";
import type {
UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpdatePersonalIdentificationDto,
UniRefund_TravellerService_Travellers_TravellerDetailProfileDto,
Expand All @@ -12,20 +11,22 @@ import AutoForm, {
createFieldConfigWithResource,
CustomCombobox,
} from "@repo/ayasofyazilim-ui/organisms/auto-form";
import { useRouter } from "next/navigation";
import { handlePutResponse } from "src/app/[lang]/app/actions/api-utils-client";
import type { CountryDto } from "src/app/[lang]/app/actions/LocationService/types";
import { putTravellerPersonalIdentificationApi } from "src/app/[lang]/app/actions/TravellerService/put-actions";
import type { TravellerServiceResource } from "src/language-data/TravellerService";

const updateBillingSchema = createZodObject(
const updateTravellerIdentificationSchema = createZodObject(
$UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpdatePersonalIdentificationDto,
[
"travelDocumentNumber",
"firstName",
"lastName",
"middleName",
"lastName",
"birthDate",
"issueDate",
"expirationDate",
"birthDate",
"nationalityCountryCode2",
"residenceCountryCode2",
"identificationType",
Expand All @@ -37,32 +38,24 @@ export default function Form({
travellerId,
travellerData,
countryList,
identificationId,
}: {
languageData: TravellerServiceResource;
travellerId: string;
identificationId: string;
travellerData: UniRefund_TravellerService_Travellers_TravellerDetailProfileDto;
countryList: { data: CountryDto[]; success: boolean };
}) {
async function putTravellerPersonalIdentification(
const router = useRouter();
function putTravellerPersonalIdentification(
data: UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpdatePersonalIdentificationDto,
) {
const response = await putTravellerPersonalIdentificationApi({
void putTravellerPersonalIdentificationApi({
id: travellerId,
requestBody: { ...data, id: travellerData.personalIdentifications[0].id },
}).then((response) => {
handlePutResponse(response, router);
});
if (response.type === "success") {
toast.success(
response.message ||
languageData["Travellers.Identifications.Update.Success"],
);
} else {
toast.error(
`${response.status}: ${
response.message ||
languageData["Travellers.Identifications.Update.Error"]
}`,
);
}
}

const translatedForm = createFieldConfigWithResource({
Expand Down Expand Up @@ -117,13 +110,15 @@ export default function Form({
className="grid gap-4 space-y-0 pb-4 md:grid-cols-1 lg:grid-cols-2 "
fieldConfig={translatedForm}
formClassName=" space-y-0 "
formSchema={updateBillingSchema}
formSchema={updateTravellerIdentificationSchema}
onSubmit={(values) => {
void putTravellerPersonalIdentification(
putTravellerPersonalIdentification(
values as UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpdatePersonalIdentificationDto,
);
}}
values={travellerData.personalIdentifications[0]}
values={travellerData.personalIdentifications.find(
(identification) => identification.id === identificationId,
)}
>
<AutoFormSubmit className="float-right">
{languageData["Edit.Save"]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Form from "./form";
export default async function Page({
params,
}: {
params: { travellerId: string; lang: string };
params: { travellerId: string; lang: string; identificationId: string };
}) {
const { languageData } = await getResourceData(params.lang);
const traveller = await getTravellersDetailsApi(params.travellerId);
Expand All @@ -27,6 +27,7 @@ export default async function Page({
data: countryList,
success: countries.type === "success",
}}
identificationId={params.identificationId}
languageData={languageData}
travellerData={travellerData}
travellerId={params.travellerId}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
"use client";

import { toast } from "@/components/ui/sonner";
import type { UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpdatePersonalIdentificationDto } from "@ayasofyazilim/saas/TravellerService";
import { $UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpdatePersonalIdentificationDto } from "@ayasofyazilim/saas/TravellerService";
import type { UniRefund_TravellerService_PersonalIdentificationCommonDatas_CreatePersonalIdentificationDto } from "@ayasofyazilim/saas/TravellerService";
import { $UniRefund_TravellerService_PersonalIdentificationCommonDatas_CreatePersonalIdentificationDto } from "@ayasofyazilim/saas/TravellerService";
import { createZodObject } from "@repo/ayasofyazilim-ui/lib/create-zod-object";
import AutoForm, {
AutoFormSubmit,
createFieldConfigWithResource,
CustomCombobox,
} from "@repo/ayasofyazilim-ui/organisms/auto-form";
import { useRouter } from "next/navigation";
import { handlePostResponse } from "src/app/[lang]/app/actions/api-utils-client";
import type { CountryDto } from "src/app/[lang]/app/actions/LocationService/types";
import { putTravellerPersonalIdentificationApi } from "src/app/[lang]/app/actions/TravellerService/put-actions";
import { postTravellerIdentificationApi } from "src/app/[lang]/app/actions/TravellerService/post-actions";
import type { TravellerServiceResource } from "src/language-data/TravellerService";

const updateBillingSchema = createZodObject(
$UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpdatePersonalIdentificationDto,
const createTravellerIdentificationSchema = createZodObject(
$UniRefund_TravellerService_PersonalIdentificationCommonDatas_CreatePersonalIdentificationDto,
[
"travelDocumentNumber",
"firstName",
"lastName",
"middleName",
"lastName",
"birthDate",
"issueDate",
"expirationDate",
"birthDate",
"nationalityCountryCode2",
"residenceCountryCode2",
"identificationType",
Expand All @@ -40,33 +40,20 @@ export default function Form({
countryList: { data: CountryDto[]; success: boolean };
}) {
const router = useRouter();
async function putTravellerPersonalIdentification(
data: UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpdatePersonalIdentificationDto,
function postTravellerIdentification(
data: UniRefund_TravellerService_PersonalIdentificationCommonDatas_CreatePersonalIdentificationDto,
) {
const response = await putTravellerPersonalIdentificationApi({
void postTravellerIdentificationApi({
id: travellerId,
requestBody: data,
}).then((response) => {
handlePostResponse(response, router, `..`);
});
if (response.type === "success") {
toast.success(
response.message ||
languageData["Travellers.Identifications.Update.Success"],
);
router.back();
router.refresh();
} else {
toast.error(
`${response.status}: ${
response.message ||
languageData["Travellers.Identifications.Update.Error"]
}`,
);
}
}

const translatedForm = createFieldConfigWithResource({
schema:
$UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpdatePersonalIdentificationDto,
$UniRefund_TravellerService_PersonalIdentificationCommonDatas_CreatePersonalIdentificationDto,
resources: languageData,
name: "Form.personalIdentification",
extend: {
Expand Down Expand Up @@ -115,10 +102,10 @@ export default function Form({
<AutoForm
className="grid gap-4 space-y-0 pb-4 md:grid-cols-1 lg:grid-cols-2 "
fieldConfig={translatedForm}
formSchema={updateBillingSchema}
formSchema={createTravellerIdentificationSchema}
onSubmit={(values) => {
void putTravellerPersonalIdentification(
values as UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpdatePersonalIdentificationDto,
postTravellerIdentification(
values as UniRefund_TravellerService_PersonalIdentificationCommonDatas_CreatePersonalIdentificationDto,
);
}}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"use server";
import type { PostApiTravellerServiceTravellersData } from "@ayasofyazilim/saas/TravellerService";
import type {
PostApiTravellerServiceTravellersByIdCreatePersonalIdentificationData,
PostApiTravellerServiceTravellersData,
} from "@ayasofyazilim/saas/TravellerService";
import { structuredError, structuredResponse } from "../../../../../lib";
import { getApiRequests } from "../api-requests";

Expand All @@ -14,3 +17,15 @@ export async function postTravellerApi(
return structuredError(error);
}
}

export async function postTravellerIdentificationApi(
data: PostApiTravellerServiceTravellersByIdCreatePersonalIdentificationData,
) {
try {
const client = await getApiRequests();
const response = await client.travellers.postTravellerIdentification(data);
return structuredResponse(response);
} catch (error) {
return structuredError(error);
}
}
7 changes: 7 additions & 0 deletions apps/web/src/app/[lang]/app/actions/api-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import type {
import type { GetApiTagServiceTagData } from "@ayasofyazilim/saas/TagService";
import type {
GetApiTravellerServiceTravellersData,
PostApiTravellerServiceTravellersByIdCreatePersonalIdentificationData,
PostApiTravellerServiceTravellersData,
PutApiTravellerServiceTravellersByIdUpdatePersonalIdentificationData,
PutApiTravellerServiceTravellersByIdUpsertPersonalPreferenceData,
Expand Down Expand Up @@ -624,6 +625,12 @@ export async function getApiRequests() {
}),
post: async (data: PostApiTravellerServiceTravellersData) =>
await travellerClient.traveller.postApiTravellerServiceTravellers(data),
postTravellerIdentification: async (
data: PostApiTravellerServiceTravellersByIdCreatePersonalIdentificationData,
) =>
await travellerClient.traveller.postApiTravellerServiceTravellersByIdCreatePersonalIdentification(
data,
),
putPersonalIdentification: async (
data: PutApiTravellerServiceTravellersByIdUpdatePersonalIdentificationData,
) =>
Expand Down
11 changes: 9 additions & 2 deletions apps/web/src/app/[lang]/app/actions/api-utils-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export const handlePutResponse = (
) => {
if (response.type === "success") {
toast.success("Updated successfully");
redirectTo ? router.push(redirectTo) : router.refresh();
if (redirectTo) {
router.push(redirectTo);
}
router.refresh();
} else {
toast.error(response.message);
}
Expand All @@ -33,6 +36,7 @@ export const handlePostResponse = <T>(
const id = (response.data[identifier] as string).toString();
router.push(`${prefix}/${id}/${suffix}`);
}
router.refresh();
} else {
toast.error(response.message);
}
Expand All @@ -44,7 +48,10 @@ export const handleDeleteResponse = (
) => {
if (response.type === "success") {
toast.success("Deleted successfully");
redirectTo ? router.push(redirectTo) : router.refresh();
if (redirectTo) {
router.push(redirectTo);
}
router.refresh();
} else {
toast.error(response.message);
}
Expand Down

0 comments on commit cbedb15

Please sign in to comment.