Skip to content

Commit

Permalink
feat(traveller): add form of create new identification (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusualhashash authored Nov 25, 2024
2 parents d136747 + 02e6409 commit dfd1b3e
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
$UniRefund_TravellerService_PersonalSummaries_UpsertPersonalSummaryDto,
} from "@ayasofyazilim/saas/TravellerService";
import { createZodObject } from "@repo/ayasofyazilim-ui/lib/create-zod-object";
import jsonToCsv from "@repo/ayasofyazilim-ui/lib/json-to-csv";
import DataTable from "@repo/ayasofyazilim-ui/molecules/tables";
import AutoForm, {
AutoFormSubmit,
Expand All @@ -31,6 +32,7 @@ import {
putTravellerPersonalSummaryApi,
} from "src/app/[lang]/app/actions/TravellerService/put-actions";
import type { TravellerServiceResource } from "src/language-data/TravellerService";
import { getBaseLink } from "src/utils";

export default function Page({
languageData,
Expand Down Expand Up @@ -120,6 +122,25 @@ export default function Page({
>
<SectionLayoutContent sectionId="identifications">
<DataTable
action={[
{
cta: languageData["Travellers.New.Identification"],
type: "NewPage",
href: getBaseLink(
`app/admin/parties/traveller/${travellerId}/identification/new`,
),
},
{
cta: languageData.ExportCSV,
callback: () => {
jsonToCsv(
travellerData.personalIdentifications,
"Identifications",
);
},
type: "Action",
},
]}
columnsData={{
type: "Auto",
data: {
Expand Down Expand Up @@ -160,7 +181,7 @@ export default function Page({
"Travellers.Identifications.Delete.Success"
],
);
router.back();
router.refresh();
}
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default function Form({
name: "Form.personalIdentification",
extend: {
nationalityCountryCode2: {
containerClassName: "gap-2",
renderer: (props) => (
<CustomCombobox<CountryDto>
childrenProps={props}
Expand All @@ -90,6 +91,7 @@ export default function Form({
),
},
residenceCountryCode2: {
containerClassName: "gap-2",
renderer: (props) => (
<CustomCombobox<CountryDto>
childrenProps={props}
Expand All @@ -114,6 +116,7 @@ export default function Form({
<AutoForm
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}
onSubmit={(values) => {
void putTravellerPersonalIdentification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export default async function Page({
params: { travellerId: string; lang: string };
}) {
const { languageData } = await getResourceData(params.lang);
const Traveller = await getTravellersDetailsApi(params.travellerId);
const traveller = await getTravellersDetailsApi(params.travellerId);
const countries = await getCountriesApi();
const travellerData =
Traveller.data as UniRefund_TravellerService_Travellers_TravellerDetailProfileDto;
traveller.data as UniRefund_TravellerService_Travellers_TravellerDetailProfileDto;
const countryList =
(countries.type === "success" && countries.data.items) || [];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"use client";

import { toast } from "@/components/ui/sonner";
import type { UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpsertPersonalIdentificationDto } from "@ayasofyazilim/saas/TravellerService";
import { $UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpsertPersonalIdentificationDto } 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 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(
$UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpsertPersonalIdentificationDto,
[
"travelDocumentNumber",
"firstName",
"lastName",
"middleName",
"issueDate",
"expirationDate",
"birthDate",
"nationalityCountryCode2",
"residenceCountryCode2",
"identificationType",
],
);

export default function Form({
languageData,
travellerId,
countryList,
}: {
languageData: TravellerServiceResource;
travellerId: string;
countryList: { data: CountryDto[]; success: boolean };
}) {
const router = useRouter();
async function putTravellerPersonalIdentification(
data: UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpsertPersonalIdentificationDto,
) {
const response = await putTravellerPersonalIdentificationApi({
id: travellerId,
requestBody: data,
});
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_UpsertPersonalIdentificationDto,
resources: languageData,
name: "Form.personalIdentification",
extend: {
nationalityCountryCode2: {
containerClassName: "gap-2",
renderer: (props) => (
<CustomCombobox<CountryDto>
childrenProps={props}
disabled={!countryList.success}
emptyValue={
countryList.success
? languageData["Country.Select"]
: languageData["Country.Fetch.Fail"]
}
list={countryList.data}
searchPlaceholder={languageData["Select.Placeholder"]}
searchResultLabel={languageData["Select.ResultLabel"]}
selectIdentifier="code2"
selectLabel="name"
/>
),
},
residenceCountryCode2: {
containerClassName: "gap-2",
renderer: (props) => (
<CustomCombobox<CountryDto>
childrenProps={props}
disabled={!countryList.success}
emptyValue={
countryList.success
? languageData["Country.Select"]
: languageData["Country.Fetch.Fail"]
}
list={countryList.data}
searchPlaceholder={languageData["Select.Placeholder"]}
searchResultLabel={languageData["Select.ResultLabel"]}
selectIdentifier="code2"
selectLabel="name"
/>
),
},
},
});

return (
<AutoForm
className="grid gap-4 space-y-0 pb-4 md:grid-cols-1 lg:grid-cols-2 "
fieldConfig={translatedForm}
formSchema={updateBillingSchema}
onSubmit={(values) => {
void putTravellerPersonalIdentification(
values as UniRefund_TravellerService_PersonalIdentificationCommonDatas_UpsertPersonalIdentificationDto,
);
}}
>
<AutoFormSubmit className="float-right">
{languageData.Save}
</AutoFormSubmit>
</AutoForm>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use server";

import type { UniRefund_TravellerService_Travellers_TravellerDetailProfileDto } from "@ayasofyazilim/saas/TravellerService";
import { getResourceData } from "src/language-data/TravellerService";
import { getTravellersDetailsApi } from "src/app/[lang]/app/actions/TravellerService/actions";
import { getCountriesApi } from "src/app/[lang]/app/actions/LocationService/actions";
import { getBaseLink } from "src/utils";
import Form from "./form";

export default async function Page({
params,
}: {
params: { travellerId: string; lang: string };
}) {
const { languageData } = await getResourceData(params.lang);
const traveller = await getTravellersDetailsApi(params.travellerId);
const countries = await getCountriesApi();
const countryList =
(countries.type === "success" && countries.data.items) || [];
const travellerData =
traveller.data as UniRefund_TravellerService_Travellers_TravellerDetailProfileDto;

return (
<>
<Form
countryList={{
data: countryList,
success: countries.type === "success",
}}
languageData={languageData}
travellerId={params.travellerId}
/>
<div className="hidden" id="page-title">
{`${languageData.Traveller} (${travellerData.personalIdentifications[0].fullName})`}
</div>
<div className="hidden" id="page-description">
{languageData["Travellers.Create.Identification.Description"]}
</div>
<div className="hidden" id="page-back-link">
{getBaseLink(`/app/admin/parties/traveller/${params.travellerId}`)}
</div>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ export default async function Page({
params: { travellerId: string; lang: string };
}) {
const { languageData } = await getResourceData(params.lang);
const Traveller = await getTravellersDetailsApi(params.travellerId);
const traveller = await getTravellersDetailsApi(params.travellerId);

if (Traveller.type !== "success") {
if (traveller.type !== "success") {
return (
<div className="error-message">
{Traveller.type + Traveller.message ||
{traveller.type + traveller.message ||
languageData["Travellers.Fetch.Fail"]}
</div>
);
}
const travellerData = Traveller.data;
const travellerData = traveller.data;

return (
<>
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/language-data/TravellerService/resources/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Travellers": "Travellers",
"Traveller": "Traveller",
"Travellers.New.Identification": "New identification",
"Travellers.Edit.Description": "You can update the traveller's details from here.",
"Travellers.Identifications.Edit.Description": "You can update the traveller's personal identification from here.",
"Travellers.Personal.Identifications": "Personal Identifications",
Expand All @@ -21,6 +22,7 @@
"Travellers.Identifications.Delete.Success": "Personal identification deleted successfully",
"Travellers.Identifications.Update.Success": "personal identification Updated successfully",
"Travellers.Identifications.Update.Error": "Failed to update personal identification",
"Travellers.Create.Identification.Description": "You can add a new personal identification from here.",

"Form.personalIdentification": "Personal Information",
"Form.personalIdentification.firstName": "First Name",
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/language-data/TravellerService/resources/tr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Travellers": "Yolcular",
"Traveller": "Yolcu",
"Travellers.New.Identification": "Yeni kimlik",
"Travellers.Edit.Description": "Yolcu bilgilerini buradan güncelleyebilirsiniz.",
"Travellers.Identifications.Edit.Description": "Yolcu kişisel bilgilerini buradan güncelleyebilirsiniz.",
"Travellers.Personal.Identifications": "Kişisel Kimlikler",
Expand All @@ -21,6 +22,7 @@
"Travellers.Identifications.Delete.Success": "Kişisel kimlik basarılıyla silindi",
"Travellers.Identifications.Update.Success": "Kişisel kimlik başarıyla güncellendi",
"Travellers.Identifications.Update.Error": "Kişisel kimlik güncellenirken bir hata oluştu.",
"Travellers.Create.Identification.Description": "Buradan yeni bir kişisel kimlik ekleyebilirsiniz.",

"Form.personalIdentification": "Kişisel Bilgiler",
"Form.personalIdentification.firstName": "Ad",
Expand Down

0 comments on commit dfd1b3e

Please sign in to comment.