parent
commit
6113c4ebdd

+ 11
- 14
resources/js/components/AppMessage.vue Vedi File

@@ -2,8 +2,6 @@
2 2
 import { usePage } from '@inertiajs/inertia-vue3'
3 3
 
4 4
 const onClose = () => {
5
-  usePage().props.value.errors = {}
6
-
7 5
   usePage().props.value.flash.success = null
8 6
 
9 7
   usePage().props.value.flash.error = null
@@ -11,24 +9,23 @@ const onClose = () => {
11 9
 </script>
12 10
 
13 11
 <template>
14
-  <Message v-if="$page.props.flash.success" severity="success" @close="onClose">
12
+  <Message
13
+    v-if="$page.props.flash.success"
14
+    severity="success"
15
+    :life="3000"
16
+    :sticky="false"
17
+    @close="onClose"
18
+  >
15 19
     {{ $page.props.flash.success }}
16 20
   </Message>
17 21
 
18
-  <Message v-if="$page.props.flash.error" severity="error" @close="onClose">
19
-    {{ $page.props.flash.error }}
20
-  </Message>
21
-
22 22
   <Message
23
-    v-if="Object.keys($page.props.errors).length > 0"
23
+    v-if="$page.props.flash.error"
24 24
     severity="error"
25
+    :life="3000"
26
+    :sticky="false"
25 27
     @close="onClose"
26 28
   >
27
-    <div v-if="Object.keys($page.props.errors).length === 1">
28
-      Ditemukan satu error pada form
29
-    </div>
30
-    <div v-else>
31
-      Ditemukan {{ Object.keys($page.props.errors).length }} error pada form
32
-    </div>
29
+    {{ $page.props.flash.error }}
33 30
   </Message>
34 31
 </template>

+ 12
- 7
resources/js/composables/useForm.js Vedi File

@@ -1,14 +1,19 @@
1
-import { watch, computed } from 'vue'
2
-import { useForm as useInertiaForm, usePage } from '@inertiajs/inertia-vue3'
1
+import { watch } from 'vue'
2
+import { useForm as useInertiaForm } from '@inertiajs/inertia-vue3'
3 3
 
4 4
 export function useForm(obj) {
5 5
   const form = useInertiaForm(obj)
6 6
 
7
-  const errors = computed(() => usePage().props.value.errors)
8
-
9
-  watch(errors, () => {
10
-    form.clearErrors()
11
-  })
7
+  for (const key in obj) {
8
+    watch(
9
+      () => form[key],
10
+      (newVal, oldVal) => {
11
+        if (newVal != oldVal) {
12
+          form.clearErrors(key)
13
+        }
14
+      }
15
+    )
16
+  }
12 17
 
13 18
   return form
14 19
 }

+ 27
- 30
resources/js/pages/Sales/Composables/useProductCart.js Vedi File

@@ -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)

+ 1
- 1
resources/js/pages/Sales/Create.vue Vedi File

@@ -247,7 +247,7 @@ const { onShowCustomerCreate } = useDialog()
247 247
                     class="p-button-outlined"
248 248
                     :class="{ 'p-button-danger': productErrors.length }"
249 249
                     :disabled="
250
-                      !form.price || !form.qty || !form.product?.number
250
+                      !form.price || !Number(form.qty) || !form.product?.number
251 251
                     "
252 252
                     @click="onAddProduct"
253 253
                   />