-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductDetail.jsx
227 lines (191 loc) · 7.21 KB
/
ProductDetail.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import React, { useEffect, useState } from 'react';
import { useDisclosure } from '@chakra-ui/react';
import { useParams } from 'react-router-dom';
import baseURL from '../constants/constants';
import LoadingCircleIcons from '../constants/icons/LoadingCircleIcons';
import { useAuth } from '../context/AuthProviderContext';
import { deleteOffer, addOffer } from '../services/offerService';
import OfferModal from '../components/OfferModal';
import useDisplayErrorMess from '../hooks/useDisplayErrorMess';
import { getProductById, putProductSold } from '../services/productService';
import BuyModal from '../components/BuyModal';
import useDisplaySuccessMess from '../hooks/useDisplaySuccessMes';
import NoImage from '../images/undefinedProduct.jpg';
function ProductDetail() {
const { id } = useParams();
const { auth } = useAuth();
const [product, setProduct] = useState({});
const [userOffer, setUserOffer] = useState({});
const [isOffer, setIsOffer] = useState(false);
const [isSold, setIsSold] = useState(false);
const [activeOfferInModal, setActiveOfferInModal] = useState('');
const [offer, setOffer] = useState({});
const [loading, setLoading] = useState(false);
const [getOfferLoading, setGetOfferLoading] = useState(false);
const [removeOfferLoading, setRemoveOfferLoading] = useState(false);
const [buyLoading, setBuyLoading] = useState(false);
const [noFindProduct, setNoFindProduct] = useState(false);
//for chakra modal
const { isOpen: isOfferOpen, onOpen: onOfferOpen, onClose: onOfferClose } = useDisclosure();
const { isOpen: isBuyOpen, onOpen: onBuyOpen, onClose: onBuyClose } = useDisclosure();
useEffect(() => {
getProduct();
//auto scroll to top.
window.scrollTo(0, 0);
}, [isOffer, isSold]);
useEffect(() => {
}, [isOffer, isSold]);
const getProduct = async () => {
setLoading(true);
const res = await getProductById(id);
console.log(res);
if (res.status == 200) {
setProduct(res.data);
//if auth context has id, that mean user is loggin. offers will be shown.
let offers = res?.data?.offers;
let isUserOffer = offers.filter(offer => offer.users_permissions_user === auth.id);
setUserOffer(isUserOffer);
setLoading(false);
}
else {
setNoFindProduct(true);
setLoading(false);
}
};
const addOfferFunc = async () => {
setGetOfferLoading(true);
let newOffer;
if (offer.isTextOffer) {
newOffer = offer.value;
}
else {
newOffer = (product.price * offer.value) / 100;
}
await addOffer(product?.id, auth?.id, newOffer);
setIsOffer(true);
setOffer({});
setActiveOfferInModal('');
document.querySelector('[type="number"]').value = '';
let radioChecked = document.querySelector('[type="radio"]:checked');
if (radioChecked) {
radioChecked.checked = false;
}
setGetOfferLoading(false);
onOfferClose();
};
const removeOffer = async (offerId) => {
setRemoveOfferLoading(true);
await deleteOffer(offerId);
setRemoveOfferLoading(false);
setIsOffer(false);
};
const getBuyFunc = async (id) => {
setBuyLoading(true);
const res = await putProductSold(id);
if (res.status == 200) {
setBuyLoading(false);
useDisplaySuccessMess('Satın Alındı');
}
else {
setBuyLoading(true);
}
onBuyClose();
setIsSold(true);
};
const openModalsIfLoggin = (state) => {
//if there is not token in auth. do not offer or buy.
if (!auth.authToken) {
useDisplayErrorMess('Lütfen giriş yapınız.');
}
else {
if (state == 'Satın Al') {
return onBuyOpen();
}
if (state == 'Teklif Ver') {
return onOfferOpen();
}
}
};
return (
<div className='productDetail'>
<div className="productCard">
{
loading ? <LoadingCircleIcons size={30} />
: (
noFindProduct ? <div className='noFoundProduct'>Böyle bir ürün bulunmamakta.</div>
:
<>
<img src={product?.image?.url ? baseURL + product?.image?.url : NoImage} alt={product.name} />
<div className='productBody'>
<h1 className='title'>{product.name}</h1>
<div className='details'>
<div className='brand'>Marka:</div>
<div>{product.brand}</div>
</div>
<div className='details'>
<div className='color'>Renk:</div>
<div>{product.color}</div>
</div>
<div className='details'>
<div className='status'>Kullanım Durumu:</div>
<div>{product.status}</div>
</div>
<div className='price'>
{product.price} TL
{
!product.isSold && (
auth?.authToken &&
userOffer.length > 0 &&
<div className='offer'>Verilen Teklif: <span>{userOffer[0]?.offerPrice.toLocaleString()} TL</span> </div>
)
}
</div>
{
product?.isSold
? <div className='soldContainer'><div className='sold'>Bu Ürün Satışta Değil</div></div>
:
<div className='buttons'>
<button className='buy' onClick={() => openModalsIfLoggin('Satın Al')} >Satın Al</button>
{
auth?.authToken
? (
userOffer.length > 0
?
<button className='offer' disabled={removeOfferLoading && true} onClick={() => removeOffer(userOffer[0]?.id)}>
{removeOfferLoading ? <LoadingCircleIcons size={20} /> : 'Teklifi Geri Çek'}
</button>
: product.isOfferable && <button className='offer' onClick={() => openModalsIfLoggin('Teklif Ver')}>Teklif Ver</button>)
: <button className='offer' onClick={() => openModalsIfLoggin('Teklif Ver')}>Teklif Ver</button>
}
</div>
}
<div className='description'>
<div>Açıklama</div>
<p>{product.description}</p>
</div>
</div >
</>
)
}
</div >
<OfferModal
isOfferOpen={isOfferOpen}
onOfferClose={onOfferClose}
getOfferLoading={getOfferLoading}
activeOfferInModal={activeOfferInModal}
setActiveOfferInModal={setActiveOfferInModal}
setOffer={setOffer}
addOfferFunc={addOfferFunc}
product={product}
/>
<BuyModal
isBuyOpen={isBuyOpen}
onBuyClose={onBuyClose}
productId={product?.id}
getBuyFunc={getBuyFunc}
buyLoading={buyLoading}
/>
</div >
);
}
export default ProductDetail;