diff --git a/src/app/product/components/ActionProduct.tsx b/src/app/product/components/ActionProduct.tsx index e70eddd..20b8919 100644 --- a/src/app/product/components/ActionProduct.tsx +++ b/src/app/product/components/ActionProduct.tsx @@ -9,6 +9,7 @@ import { useCheckWishProduct } from '../hooks/useProductData' import { useProductStore } from '../store/Store' import PriceHistory from './PriceHistory' import Link from 'next/link' +import ChangePrice from './ChangePrice' interface ActionProductProps { product: Product @@ -120,13 +121,7 @@ const ActionProduct: FC = ({ product }) => { /> - {/* */} + diff --git a/src/app/product/components/ChangePrice.tsx b/src/app/product/components/ChangePrice.tsx new file mode 100644 index 0000000..437f42e --- /dev/null +++ b/src/app/product/components/ChangePrice.tsx @@ -0,0 +1,118 @@ +import { useChangeEmail, useGetProfile } from '@/app/profile/hooks/useProfileData' +import DefaulModal from '@/components/DefaulModal' +import Input from '@/components/Input' +import { Button } from '@/components/ui/button' +import { Notification, Send2 } from 'iconsax-react' +import { useState, type FC, FormEvent } from 'react' +import { toast } from '@/components/Toast' +import { extractErrorMessage } from '@/helpers/errorUtils' + +const ChangePrice: FC = () => { + + const [open, setOpen] = useState(false) + const [email, setEmail] = useState('') + const [isLoading, setIsLoading] = useState(false) + const { data } = useGetProfile() + const { mutateAsync: changeEmail } = useChangeEmail() + const hasEmail = data?.results?.info?.email + + const handleSubmit = async (e: FormEvent) => { + e.preventDefault() + + const emailToSend = hasEmail || email + if (!emailToSend) { + toast('لطفا ایمیل خود را وارد کنید') + return + } + + setIsLoading(true) + + try { + // اگر ایمیل وجود ندارد، ابتدا با changeEmail ذخیره کن + if (!hasEmail && email) { + await changeEmail(email, { + onError: (error) => { + toast(extractErrorMessage(error), 'error') + } + }) + } + + // TODO: Add API call for price change notification + // await axios.post('/product/price-change-notification', { + // email: emailToSend, + // productId: productId // از context یا props دریافت شود + // }) + + toast('درخواست شما با موفقیت ثبت شد') + setOpen(false) + setEmail('') + } catch { + toast('خطا در ارسال درخواست') + } finally { + setIsLoading(false) + } + } + + return ( +
+ + + setOpen(false)} + title_header='تغییر قیمت' + isHeader + > +
+

+ قیمت های شگفت انگیز برای این کالا از طریق ایمیل به شما اطلاع رسانی شود؟ +

+ + {hasEmail ? ( +
+

ایمیل شما:

+

{data?.results?.info?.email}

+
+ ) : ( + setEmail(e.target.value)} + required + /> + )} + +
+ + +
+
+
+
+ ) +} + +export default ChangePrice \ No newline at end of file diff --git a/src/app/profile/hooks/useProfileData.ts b/src/app/profile/hooks/useProfileData.ts index 11b5e82..ba1addc 100644 --- a/src/app/profile/hooks/useProfileData.ts +++ b/src/app/profile/hooks/useProfileData.ts @@ -52,3 +52,9 @@ export const useGetComments = () => { queryFn: api.getComments, }); }; + +export const useChangeEmail = () => { + return useMutation({ + mutationFn: (email: string) => api.changeEmail(email), + }); +}; diff --git a/src/app/profile/service/Service.ts b/src/app/profile/service/Service.ts index ea4db5a..f2ff5d9 100644 --- a/src/app/profile/service/Service.ts +++ b/src/app/profile/service/Service.ts @@ -43,3 +43,8 @@ export const getComments = async (): Promise => { const { data } = await axios.get("/user/profile/comments"); return data; }; + +export const changeEmail = async (email: string) => { + const { data } = await axios.post("/user/profile/email/change", { email }); + return data; +}; diff --git a/src/share/types/SharedTypes.ts b/src/share/types/SharedTypes.ts index b92b04f..d95eb13 100644 --- a/src/share/types/SharedTypes.ts +++ b/src/share/types/SharedTypes.ts @@ -47,6 +47,7 @@ export type UserMeResponseType = ApiResponse<{ info: { _id: string; fullName: string; + email: string; phoneNumber: string; dateOfBirth: string | null; address: string;