-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathItemRow.jsx
143 lines (126 loc) · 4.24 KB
/
ItemRow.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
import { useState, useEffect } from 'react';
import { Form, Input, InputNumber, Row, Col } from 'antd';
import { DeleteOutlined } from '@ant-design/icons';
import { useMoney, useDate } from '@/settings';
import calculate from '@/utils/calculate';
import useLanguage from '@/locale/useLanguage';
export default function ItemRow({ field, remove, current = null }) {
const translate = useLanguage();
const [totalState, setTotal] = useState(undefined);
const [price, setPrice] = useState(0);
const [quantity, setQuantity] = useState(0);
const money = useMoney();
const updateQt = (value) => {
setQuantity(value);
};
const updatePrice = (value) => {
setPrice(value);
};
useEffect(() => {
if (current) {
// When it accesses the /payment/ endpoint,
// it receives an invoice.item instead of just item
// and breaks the code, but now we can check if items exists,
// and if it doesn't we can access invoice.items.
const { items, invoice } = current;
if (invoice) {
const item = invoice[field.fieldKey];
if (item) {
setQuantity(item.quantity);
setPrice(item.price);
}
} else {
const item = items[field.fieldKey];
if (item) {
setQuantity(item.quantity);
setPrice(item.price);
}
}
}
}, [current]);
useEffect(() => {
const currentTotal = calculate.multiply(price, quantity);
setTotal(currentTotal);
}, [price, quantity]);
return (
<Row gutter={[12, 12]} style={{ position: 'relative' }}>
<Col className="gutter-row" xs={24} sm={12} md={5}>
<Form.Item
name={[field.name, 'itemName']}
label={translate('Item Name')} // Add label here
rules={[
{
required: true,
message: 'Missing itemName name',
},
{
pattern: /^(?!\s*$)[\s\S]+$/,
message: 'Item Name must contain alphanumeric or special characters',
},
]}
>
<Input placeholder="Item Name" />
</Form.Item>
</Col>
<Col className="gutter-row" xs={24} sm={12} md={7}>
<Form.Item
name={[field.name, 'description']}
label={translate('Description')} // Add label here
>
<Input placeholder="Description" />
</Form.Item>
</Col>
<Col className="gutter-row" xs={12} sm={6} md={3}>
<Form.Item
name={[field.name, 'quantity']}
label={translate('Quantity')} // Add label here
rules={[{ required: true }]}
>
<InputNumber style={{ width: '100%' }} min={0} onChange={updateQt} />
</Form.Item>
</Col>
<Col className="gutter-row" xs={12} sm={6} md={4}>
<Form.Item
name={[field.name, 'price']}
label={translate('Price')} // Add label here
rules={[{ required: true }]}
>
<InputNumber
className="moneyInput"
onChange={updatePrice}
min={0}
controls={false}
addonAfter={money.currency_position === 'after' ? money.currency_symbol : undefined}
addonBefore={money.currency_position === 'before' ? money.currency_symbol : undefined}
/>
</Form.Item>
</Col>
<Col className="gutter-row" xs={24} sm={12} md={5}>
<Form.Item
name={[field.name, 'total']}
label={translate('Total')} // Add label here
>
<InputNumber
readOnly
className="moneyInput"
value={totalState}
min={0}
controls={false}
addonAfter={money.currency_position === 'after' ? money.currency_symbol : undefined}
addonBefore={money.currency_position === 'before' ? money.currency_symbol : undefined}
formatter={(value) =>
money.amountFormatter({ amount: value, currency_code: money.currency_code })
}
/>
</Form.Item>
</Col>
<div style={{ position: 'absolute', right: '-10px', top: '3px' }}>
<DeleteOutlined onClick={() => remove(field.name)}
style={{
fontSize: '18px',
cursor: 'pointer',
}}/>
</div>
</Row>
);
}