diff --git a/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSection.tsx b/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSection.tsx index d8d4444..78894e7 100644 --- a/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSection.tsx +++ b/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSection.tsx @@ -14,7 +14,7 @@ import useToggle from '@/hooks/helpers/useToggle'; import { AddressSelectionModal } from './AddressSelectionModal'; const formatAddress = (address: Address) => { - return `${address.address}، ${address.city}، ${address.province}`; + return address.address; }; export const AddressSection = () => { diff --git a/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSelectionModal.tsx b/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSelectionModal.tsx index 4848fbc..9ee5f71 100644 --- a/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSelectionModal.tsx +++ b/src/app/[name]/(Dialogs)/order/checkout/[id]/components/AddressSelectionModal.tsx @@ -9,7 +9,7 @@ import { ef } from '@/lib/helpers/utfNumbers'; import { Edit2 } from 'iconsax-react'; const formatAddress = (address: Address) => { - return `${address.address}، ${address.city}، ${address.province}`; + return address.address; }; interface AddressSelectionModalProps { diff --git a/src/app/[name]/(Dialogs)/order/checkout/types/Types.ts b/src/app/[name]/(Dialogs)/order/checkout/types/Types.ts index b1bfac8..d2e4fee 100644 --- a/src/app/[name]/(Dialogs)/order/checkout/types/Types.ts +++ b/src/app/[name]/(Dialogs)/order/checkout/types/Types.ts @@ -196,8 +196,6 @@ export interface OrderAddress { user: OrderUser; title: string; address: string; - city: string; - province: string; postalCode: string; latitude: number; longitude: number; @@ -439,12 +437,10 @@ export interface OrderDetailCarAddress { } export interface OrderDetailUserAddress { - city: string; phone: string; address: string; fullName: string; latitude: number; - province: string; longitude: number; postalCode: string; } diff --git a/src/app/[name]/(Main)/order/history/types/Types.ts b/src/app/[name]/(Main)/order/history/types/Types.ts index 7841eb2..7ce67db 100644 --- a/src/app/[name]/(Main)/order/history/types/Types.ts +++ b/src/app/[name]/(Main)/order/history/types/Types.ts @@ -79,12 +79,10 @@ export interface HistoryOrderDeliveryMethod { } export interface HistoryOrderUserAddress { - city: string; phone: string; address: string; fullName: string; latitude: number; - province: string; longitude: number; postalCode: string; } @@ -97,8 +95,6 @@ export interface HistoryOrderAddress { user: string; title: string; address: string; - city: string; - province: string; postalCode: string; latitude: number; longitude: number; diff --git a/src/app/[name]/(Profile)/profile/address/new/components/AddressForm.tsx b/src/app/[name]/(Profile)/profile/address/new/components/AddressForm.tsx index 966de8c..4157544 100644 --- a/src/app/[name]/(Profile)/profile/address/new/components/AddressForm.tsx +++ b/src/app/[name]/(Profile)/profile/address/new/components/AddressForm.tsx @@ -1,90 +1,80 @@ -import InputField from '@/components/input/InputField'; -import { NominatimReverseGeocodingResponse } from '../../types/Types'; -import { useGetProfile } from '../../../hooks/userProfileData'; -import { useEffect } from 'react'; +import InputField from "@/components/input/InputField"; +import { useEffect } from "react"; +import { useGetProfile } from "../../../hooks/userProfileData"; +import { NominatimReverseGeocodingResponse } from "../../types/Types"; interface AddressFormProps { - formData: { - title: string; - addressDetails: string; - phone: string; - postalCode: string; - isDefault: boolean; - }; - selectedAddress: NominatimReverseGeocodingResponse | null; - onFormDataChange: (data: Partial) => void; + formData: { + title: string; + addressDetails: string; + phone: string; + postalCode: string; + isDefault: boolean; + }; + selectedAddress: NominatimReverseGeocodingResponse | null; + onFormDataChange: (data: Partial) => void; } -export const AddressForm = ({ - formData, - selectedAddress, - onFormDataChange, -}: AddressFormProps) => { +export const AddressForm = ({ formData, selectedAddress, onFormDataChange }: AddressFormProps) => { + const { data: profileData } = useGetProfile(); - const { data: profileData } = useGetProfile() + useEffect(() => { + if (profileData?.data?.phone && !formData.phone) { + onFormDataChange({ phone: "0" + profileData.data.phone }); + } + }, [profileData?.data?.phone, formData.phone, onFormDataChange]); - useEffect(() => { - if (profileData?.data?.phone && !formData.phone) { - onFormDataChange({ phone: '0' + profileData.data.phone }); - } - }, [profileData?.data?.phone, formData.phone, onFormDataChange]); - - return ( -
- onFormDataChange({ title: e.target.value })} - - /> - onFormDataChange({ addressDetails: e.target.value })} - inputClassName='text-xs!' - /> - onFormDataChange({ phone: e.target.value })} - inputClassName='text-xs!' - /> - onFormDataChange({ postalCode: e.target.value })} - inputClassName='text-xs!' - /> -
- - onFormDataChange({ isDefault: e.target.checked })} - type='checkbox' - checked={formData.isDefault} - /> -
-
- ); + return ( +
+ onFormDataChange({ title: e.target.value })} + /> + onFormDataChange({ addressDetails: e.target.value })} + inputClassName="text-xs!" + /> + onFormDataChange({ phone: e.target.value })} + inputClassName="text-xs!" + /> + onFormDataChange({ postalCode: e.target.value })} + inputClassName="text-xs!" + /> +
+ + onFormDataChange({ isDefault: e.target.checked })} + type="checkbox" + checked={formData.isDefault} + /> +
+
+ ); }; - diff --git a/src/app/[name]/(Profile)/profile/address/new/hooks/useAddressForm.ts b/src/app/[name]/(Profile)/profile/address/new/hooks/useAddressForm.ts index a5d5db3..28aeb3d 100644 --- a/src/app/[name]/(Profile)/profile/address/new/hooks/useAddressForm.ts +++ b/src/app/[name]/(Profile)/profile/address/new/hooks/useAddressForm.ts @@ -1,134 +1,124 @@ -import { useState, useEffect } from 'react'; -import { useRouter, useSearchParams } from 'next/navigation'; -import { useCreateAddress, useUpdateAddress } from '../../hooks/useAddressData'; -import { toast } from '@/components/Toast'; -import { CreateAddressType, UpdateAddressType, NominatimReverseGeocodingResponse, Address } from '../../types/Types'; +import { toast } from "@/components/Toast"; +import { useRouter, useSearchParams } from "next/navigation"; +import { useEffect, useState } from "react"; +import { useCreateAddress, useUpdateAddress } from "../../hooks/useAddressData"; +import { Address, CreateAddressType, NominatimReverseGeocodingResponse, UpdateAddressType } from "../../types/Types"; interface UseAddressFormProps { - selectedPosition: [number, number] | null; - selectedAddress: NominatimReverseGeocodingResponse | null; - fallbackPosition?: [number, number] | null; - initialPhone?: string; - editMode?: boolean; - addressData?: Address | null; + selectedPosition: [number, number] | null; + selectedAddress: NominatimReverseGeocodingResponse | null; + fallbackPosition?: [number, number] | null; + initialPhone?: string; + editMode?: boolean; + addressData?: Address | null; } -export const useAddressForm = ({ - selectedPosition, - selectedAddress, - fallbackPosition = [0, 0], - initialPhone = '', - editMode = false, - addressData = null, -}: UseAddressFormProps) => { - const router = useRouter(); - const searchParams = useSearchParams(); - const redirect = searchParams.get('redirect'); - const { mutate: createAddress, isPending: isCreating } = useCreateAddress(); - const { mutate: updateAddress, isPending: isUpdating } = useUpdateAddress(); - const isPending = isCreating || isUpdating; - - const [formData, setFormData] = useState({ - title: '', - addressDetails: '', - phone: initialPhone, - postalCode: '', - isDefault: false, - }); +export const useAddressForm = ({ selectedPosition, selectedAddress, fallbackPosition = [0, 0], initialPhone = "", editMode = false, addressData = null }: UseAddressFormProps) => { + const router = useRouter(); + const searchParams = useSearchParams(); + const redirect = searchParams.get("redirect"); + const { mutate: createAddress, isPending: isCreating } = useCreateAddress(); + const { mutate: updateAddress, isPending: isUpdating } = useUpdateAddress(); + const isPending = isCreating || isUpdating; - useEffect(() => { - if (editMode && addressData) { - setFormData({ - title: addressData.title || '', - addressDetails: addressData.address || '', - phone: addressData.phone || initialPhone, - postalCode: addressData.postalCode || '', - isDefault: addressData.isDefault || false, - }); - } - }, [editMode, addressData, initialPhone]); + const [formData, setFormData] = useState({ + title: "", + addressDetails: "", + phone: initialPhone, + postalCode: "", + isDefault: false, + }); - const handleFormDataChange = (data: Partial) => { - setFormData((prev) => ({ ...prev, ...data })); + useEffect(() => { + if (editMode && addressData) { + setFormData({ + title: addressData.title || "", + addressDetails: addressData.address || "", + phone: addressData.phone || initialPhone, + postalCode: addressData.postalCode || "", + isDefault: addressData.isDefault || false, + }); + } + }, [editMode, addressData, initialPhone]); + + const handleFormDataChange = (data: Partial) => { + setFormData((prev) => ({ ...prev, ...data })); + }; + + const submitAddress = (e: React.FormEvent) => { + e.preventDefault(); + + const positionToUse: [number, number] = selectedPosition || fallbackPosition || [0, 0]; + + if (!formData.title.trim()) { + toast("لطفا عنوان آدرس را وارد کنید", "error"); + return; + } + + if (!formData.addressDetails.trim()) { + toast("لطفا جزئیات آدرس را وارد کنید", "error"); + return; + } + + if (!formData.phone.trim()) { + toast("لطفا شماره تلفن را وارد کنید", "error"); + return; + } + + const baseAddressData = { + title: formData.title, + address: formData.addressDetails, + postalCode: formData.postalCode || selectedAddress?.address?.postcode || addressData?.postalCode || "", + latitude: positionToUse[0], + longitude: positionToUse[1], + phone: formData.phone, + isDefault: formData.isDefault, }; - const submitAddress = (e: React.FormEvent) => { - e.preventDefault(); + if (editMode && addressData) { + const updateData: UpdateAddressType = { + ...baseAddressData, + id: addressData.id, + }; - const positionToUse: [number, number] = selectedPosition || fallbackPosition || [0, 0]; + updateAddress(updateData, { + onSuccess: () => { + toast("آدرس با موفقیت به‌روزرسانی شد", "success"); + if (redirect) { + router.replace(redirect); + } else { + router.back(); + } + }, + onError: (error) => { + const errorMessage = error instanceof Error ? error.message : "خطا در به‌روزرسانی آدرس"; + toast(errorMessage, "error"); + }, + }); + } else { + const createData: CreateAddressType = baseAddressData; - if (!formData.title.trim()) { - toast('لطفا عنوان آدرس را وارد کنید', 'error'); - return; - } + createAddress(createData, { + onSuccess: () => { + toast("آدرس با موفقیت ثبت شد", "success"); + if (redirect) { + router.replace(redirect); + } else { + router.back(); + } + }, + onError: (error) => { + const errorMessage = error instanceof Error ? error.message : "خطا در ثبت آدرس"; + toast(errorMessage, "error"); + }, + }); + } + }; - if (!formData.addressDetails.trim()) { - toast('لطفا جزئیات آدرس را وارد کنید', 'error'); - return; - } - - if (!formData.phone.trim()) { - toast('لطفا شماره تلفن را وارد کنید', 'error'); - return; - } - - const baseAddressData = { - title: formData.title, - address: formData.addressDetails, - city: selectedAddress?.address?.city || selectedAddress?.address?.district || addressData?.city || '', - province: selectedAddress?.address?.province || selectedAddress?.address?.state || addressData?.province || '', - postalCode: formData.postalCode || selectedAddress?.address?.postcode || addressData?.postalCode || '', - latitude: positionToUse[0], - longitude: positionToUse[1], - phone: formData.phone, - isDefault: formData.isDefault, - }; - - if (editMode && addressData) { - const updateData: UpdateAddressType = { - ...baseAddressData, - id: addressData.id, - }; - - updateAddress(updateData, { - onSuccess: () => { - toast('آدرس با موفقیت به‌روزرسانی شد', 'success'); - if (redirect) { - router.replace(redirect); - } else { - router.back(); - } - }, - onError: (error) => { - const errorMessage = error instanceof Error ? error.message : 'خطا در به‌روزرسانی آدرس'; - toast(errorMessage, 'error'); - }, - }); - } else { - const createData: CreateAddressType = baseAddressData; - - createAddress(createData, { - onSuccess: () => { - toast('آدرس با موفقیت ثبت شد', 'success'); - if (redirect) { - router.replace(redirect); - } else { - router.back(); - } - }, - onError: (error) => { - const errorMessage = error instanceof Error ? error.message : 'خطا در ثبت آدرس'; - toast(errorMessage, 'error'); - }, - }); - } - }; - - return { - formData, - isPending, - handleFormDataChange, - submitAddress, - }; + return { + formData, + isPending, + handleFormDataChange, + submitAddress, + }; }; - diff --git a/src/app/[name]/(Profile)/profile/address/new/utils/formatAddress.ts b/src/app/[name]/(Profile)/profile/address/new/utils/formatAddress.ts index 5d0d2dd..7ebdbc6 100644 --- a/src/app/[name]/(Profile)/profile/address/new/utils/formatAddress.ts +++ b/src/app/[name]/(Profile)/profile/address/new/utils/formatAddress.ts @@ -1,7 +1,7 @@ import { Address, NominatimReverseGeocodingResponse } from '../../types/Types'; export const formatAddress = (address: Address): string => { - return `${address.address}، ${address.city}، ${address.province}`; + return address.address; }; export const formatSelectedAddress = (selectedAddress: NominatimReverseGeocodingResponse | null): string => { @@ -11,8 +11,6 @@ export const formatSelectedAddress = (selectedAddress: NominatimReverseGeocoding try { return [ - selectedAddress.address.state ?? selectedAddress.address.province, - selectedAddress.address.city, selectedAddress.address.neighbourhood, !selectedAddress.address.road.startsWith('میدان') && !selectedAddress.address.road.startsWith('بلوار') && diff --git a/src/app/[name]/(Profile)/profile/address/page.tsx b/src/app/[name]/(Profile)/profile/address/page.tsx index c9b7c06..d6340d5 100644 --- a/src/app/[name]/(Profile)/profile/address/page.tsx +++ b/src/app/[name]/(Profile)/profile/address/page.tsx @@ -70,7 +70,7 @@ function UserAddressesPage({ }: Props) { }; const formatAddress = (address: Address) => { - return `${address.address}، ${address.city}، ${address.province}`; + return address.address; }; const getEditHref = (addressId: string) => { diff --git a/src/app/[name]/(Profile)/profile/address/types/Types.ts b/src/app/[name]/(Profile)/profile/address/types/Types.ts index 6bee534..4c28916 100644 --- a/src/app/[name]/(Profile)/profile/address/types/Types.ts +++ b/src/app/[name]/(Profile)/profile/address/types/Types.ts @@ -9,8 +9,6 @@ export interface Address { user: string; title: string; address: string; - city: string; - province: string; postalCode: string; latitude: number; longitude: number; @@ -25,8 +23,6 @@ export type AddressResponse = BaseResponse
; export type CreateAddressType = { title: string; address: string; - city: string; - province: string; postalCode: string; latitude: number; longitude: number;