-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductDetails.tsx
69 lines (64 loc) · 1.93 KB
/
ProductDetails.tsx
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
import React, { FC } from 'react';
import { useLocalService, useTranslation } from 'onekijs-next';
import { ProductType } from '../../../../data/products';
import { currency } from '../../core/libs/format';
import AvailabilityService, { AvailabilityState } from '../services/AvailabilityService';
interface ProductDetailsProps {
product: ProductType;
onBuy: () => void;
}
const initialAvailabilityState: AvailabilityState = {
loading: false,
};
const ProductDetails: FC<ProductDetailsProps> = ({ product, onBuy }) => {
const [T] = useTranslation();
const [availability, availabilityService] = useLocalService(AvailabilityService, initialAvailabilityState);
return (
<div>
<h2>
<T>Product Details</T>
</h2>
<div>
<h3>
<T>{product.name}</T>
</h3>
<h4>
<T>{currency(product.price)}</T>
</h4>
{product.description && (
<p>
<T>{product.description}</T>
</p>
)}
<p>
<button onClick={onBuy}>
<T>Buy</T>
</button>
</p>
<p>
<button onClick={() => availabilityService.checkAvailability(product.id)} disabled={availability.loading}>
<T>Check availability</T>
</button>
<span className="availability">
{availability.loading && (
<span>
<T>Loading</T> ...
</span>
)}
{!availability.loading && availability.available === true && (
<span className="available">
<T>The product is available</T>
</span>
)}
{!availability.loading && availability.available === false && (
<span className="not-available">
<T>The product is not available</T>
</span>
)}
</span>
</p>
</div>
</div>
);
};
export default ProductDetails;