change price: echo (Not completed yet, but)

This commit is contained in:
hamid zarghami
2025-10-06 14:31:35 +03:30
parent 5aaf0aa6bd
commit 7b3a5d9a12
5 changed files with 132 additions and 7 deletions
+2 -7
View File
@@ -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<ActionProductProps> = ({ product }) => {
/>
</button>
{/* <button
onClick={handleShare}
className='p-2 rounded-full bg-white/80 backdrop-blur-sm hover:bg-white transition-colors'
title='اشتراک‌گذاری'
>
<Notification size={20} color='#333333' />
</button> */}
<ChangePrice />
<PriceHistory productId={product._id.toString()} />
+118
View File
@@ -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 (
<div>
<button
onClick={() => setOpen(true)}
className='p-2 rounded-full bg-white/80 backdrop-blur-sm hover:bg-white transition-colors'
title='تغییر قیمت'
>
<Notification size={20} color='#333333' />
</button>
<DefaulModal
open={open}
close={() => setOpen(false)}
title_header='تغییر قیمت'
isHeader
>
<form onSubmit={handleSubmit} className='space-y-4'>
<p className='text-sm text-black font-light'>
قیمت های شگفت انگیز برای این کالا از طریق ایمیل به شما اطلاع رسانی شود؟
</p>
{hasEmail ? (
<div className='p-3 bg-gray-50 rounded-lg'>
<p className='text-sm text-gray-600'>ایمیل شما:</p>
<p className='font-medium'>{data?.results?.info?.email}</p>
</div>
) : (
<Input
label='ایمیل'
type='email'
placeholder='ایمیل خود را وارد کنید'
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
)}
<div className='flex gap-2 pt-4'>
<Button
type='submit'
className='flex-1'
isLoading={isLoading}
>
<Send2 size={16} />
ارسال درخواست
</Button>
<Button
type='button'
variant='outline'
onClick={() => setOpen(false)}
className='flex-1'
>
انصراف
</Button>
</div>
</form>
</DefaulModal>
</div>
)
}
export default ChangePrice
+6
View File
@@ -52,3 +52,9 @@ export const useGetComments = () => {
queryFn: api.getComments,
});
};
export const useChangeEmail = () => {
return useMutation({
mutationFn: (email: string) => api.changeEmail(email),
});
};
+5
View File
@@ -43,3 +43,8 @@ export const getComments = async (): Promise<CommentsResponse> => {
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;
};
+1
View File
@@ -47,6 +47,7 @@ export type UserMeResponseType = ApiResponse<{
info: {
_id: string;
fullName: string;
email: string;
phoneNumber: string;
dateOfBirth: string | null;
address: string;