|
|
@@ -11,21 +11,16 @@ export function useProductCart(form, initialProducts = []) {
|
|
11
|
11
|
const productValidation = () => {
|
|
12
|
12
|
onClearProductErrors()
|
|
13
|
13
|
|
|
14
|
|
- productCart.find((product) => {
|
|
15
|
|
- if (product.number === form.product.number) {
|
|
16
|
|
- // productErrors.push({
|
|
17
|
|
- // message: 'Produk sudah ada dikeranjang',
|
|
18
|
|
- // field: 'product',
|
|
19
|
|
- // })
|
|
20
|
|
-
|
|
21
|
|
- if (form.qty + product.qty > form.product.qty) {
|
|
22
|
|
- productErrors.push({
|
|
23
|
|
- message: 'Stok tidak mencukupi',
|
|
24
|
|
- field: 'qty',
|
|
25
|
|
- })
|
|
26
|
|
- }
|
|
27
|
|
- }
|
|
28
|
|
- })
|
|
|
14
|
+ const productExists = productCart.find(
|
|
|
15
|
+ (product) => product.number === form.product.number
|
|
|
16
|
+ )
|
|
|
17
|
+
|
|
|
18
|
+ if (Number(form.qty) + (productExists?.qty ?? 0) > form.product.qty) {
|
|
|
19
|
+ productErrors.push({
|
|
|
20
|
+ message: 'Stok tidak mencukupi',
|
|
|
21
|
+ field: 'qty',
|
|
|
22
|
+ })
|
|
|
23
|
+ }
|
|
29
|
24
|
|
|
30
|
25
|
if (productErrors.length) {
|
|
31
|
26
|
throw new FormValidationError('form error', productErrors)
|
|
|
@@ -38,22 +33,24 @@ export function useProductCart(form, initialProducts = []) {
|
|
38
|
33
|
|
|
39
|
34
|
productValidation()
|
|
40
|
35
|
|
|
41
|
|
- productCart.find((product) => {
|
|
42
|
|
- if (product.number === form.product.number) {
|
|
43
|
|
- product.qty += form.qty
|
|
44
|
|
- } else {
|
|
45
|
|
- productCart.push({
|
|
46
|
|
- label: 'add',
|
|
47
|
|
- number: form.product.number,
|
|
48
|
|
- name: form.product.name,
|
|
49
|
|
- price: form.price,
|
|
50
|
|
- qty: form.qty,
|
|
51
|
|
- unit: form.product.unit,
|
|
52
|
|
- })
|
|
53
|
|
- }
|
|
54
|
|
- })
|
|
|
36
|
+ const productExists = productCart.find(
|
|
|
37
|
+ (product) => product.number === form.product.number
|
|
|
38
|
+ )
|
|
|
39
|
+
|
|
|
40
|
+ if (productExists) {
|
|
|
41
|
+ productExists.qty += Number(form.qty)
|
|
|
42
|
+ } else {
|
|
|
43
|
+ productCart.push({
|
|
|
44
|
+ label: 'add',
|
|
|
45
|
+ number: form.product.number,
|
|
|
46
|
+ name: form.product.name,
|
|
|
47
|
+ price: form.price,
|
|
|
48
|
+ qty: Number(form.qty),
|
|
|
49
|
+ unit: form.product.unit,
|
|
|
50
|
+ })
|
|
|
51
|
+ }
|
|
55
|
52
|
|
|
56
|
|
- form.reset('product', 'qty')
|
|
|
53
|
+ form.reset('product', 'price', 'qty')
|
|
57
|
54
|
} catch (e) {
|
|
58
|
55
|
e.errors.forEach((error) => {
|
|
59
|
56
|
form.setError(error.field, error.message)
|