Ver código fonte

fix: components filter

Muhammad Iqbal Afandi 3 anos atrás
pai
commit
ceb761ad4c

+ 22
- 15
resources/js/components/AppDateRangeFilter.vue Ver arquivo

@@ -6,21 +6,28 @@ import { pickBy } from 'lodash'
6 6
 import { computed } from '@vue/reactivity'
7 7
 
8 8
 const props = defineProps({
9
-  initialFilter: {
9
+  initialDateRage: {
10 10
     type: Object,
11 11
     required: true,
12 12
   },
13
+  nameParam: {
14
+    type: Array,
15
+    required: true,
16
+  },
13 17
 })
14 18
 
15
-const initialFilter = computed(() => {
16
-  if (props.initialFilter.start_date || props.initialFilter.end_date) {
17
-    if (props.initialFilter.end_date) {
19
+const initialDateRage = computed(() => {
20
+  if (
21
+    props.initialDateRage[props.nameParam[0]] ||
22
+    props.initialDateRage[props.nameParam[1]]
23
+  ) {
24
+    if (props.initialDateRage[props.nameParam[1]]) {
18 25
       return [
19
-        new Date(props.initialFilter.start_date),
20
-        new Date(props.initialFilter.end_date),
26
+        new Date(props.initialDateRage[props.nameParam[0]]),
27
+        new Date(props.initialDateRage[props.nameParam[1]]),
21 28
       ]
22 29
     } else {
23
-      return [new Date(props.initialFilter.start_date), null]
30
+      return [new Date(props.initialDateRage[props.nameParam[0]]), null]
24 31
     }
25 32
   }
26 33
 })
@@ -33,25 +40,25 @@ const removeParams = (...params) => {
33 40
   history.replaceState({}, '', `${location.pathname}?${urlParams}`)
34 41
 }
35 42
 
36
-const dates = ref(initialFilter.value)
43
+const dates = ref(initialDateRage.value)
37 44
 
38 45
 watch(dates, (value) => {
39 46
   if (value[1]) {
40
-    var start_date = dayjs(value[0]).format('YYYY-MM-DD')
47
+    var start = dayjs(value[0]).format('YYYY-MM-DD')
41 48
 
42
-    var end_date = dayjs(value[1]).format('YYYY-MM-DD')
49
+    var end = dayjs(value[1]).format('YYYY-MM-DD')
43 50
   } else if (value[0]) {
44
-    var start_date = dayjs(value[0]).format('YYYY-MM-DD')
51
+    var start = dayjs(value[0]).format('YYYY-MM-DD')
45 52
 
46
-    var end_date = null
53
+    var end = null
47 54
   }
48 55
 
49
-  removeParams('start_date', 'end_date', 'page')
56
+  removeParams(props.nameParam[0], props.nameParam[1], 'page')
50 57
 
51 58
   Inertia.reload({
52 59
     data: pickBy({
53
-      start_date,
54
-      end_date,
60
+      [props.nameParam[0]]: start,
61
+      [props.nameParam[1]]: end,
55 62
     }),
56 63
   })
57 64
 })

+ 12
- 8
resources/js/components/AppDropdownFilter.vue Ver arquivo

@@ -4,9 +4,13 @@ import { Inertia } from '@inertiajs/inertia'
4 4
 import { pickBy } from 'lodash'
5 5
 import AppDropdown from '@/components/AppDropdown.vue'
6 6
 
7
-defineProps({
8
-  options: {
9
-    type: Array,
7
+const props = defineProps({
8
+  initialDropdown: {
9
+    type: Object,
10
+    required: true,
11
+  },
12
+  nameParam: {
13
+    type: String,
10 14
     required: true,
11 15
   },
12 16
 })
@@ -19,19 +23,19 @@ const removeParams = (...params) => {
19 23
   history.replaceState({}, '', `${location.pathname}?${urlParams}`)
20 24
 }
21 25
 
22
-const status = ref()
26
+const status = ref(props.initialDropdown[props.nameParam])
23 27
 
24
-watch(status, (status) => {
25
-  removeParams('status', 'page')
28
+watch(status, (value) => {
29
+  removeParams(props.nameParam, 'page')
26 30
 
27 31
   Inertia.reload({
28 32
     data: pickBy({
29
-      status,
33
+      [props.nameParam]: value,
30 34
     }),
31 35
   })
32 36
 })
33 37
 </script>
34 38
 
35 39
 <template>
36
-  <AppDropdown placeholder="status" :options="options" v-model="status" />
40
+  <AppDropdown v-model="status" />
37 41
 </template>

+ 11
- 4
resources/js/components/AppSearchFilter.vue Ver arquivo

@@ -5,6 +5,11 @@ import { pickBy } from 'lodash'
5 5
 
6 6
 const props = defineProps({
7 7
   initialSearch: {
8
+    type: Object,
9
+    required: true,
10
+  },
11
+  nameParam: {
12
+    type: String,
8 13
     required: true,
9 14
   },
10 15
 })
@@ -17,13 +22,15 @@ const removeParams = (...params) => {
17 22
   history.replaceState({}, '', `${location.pathname}?${urlParams}`)
18 23
 }
19 24
 
20
-const search = ref(props.initialSearch)
25
+const search = ref(props.initialSearch[props.nameParam])
21 26
 
22
-watch(search, (search) => {
23
-  removeParams('search', 'page')
27
+watch(search, (value) => {
28
+  removeParams(props.nameParam, 'page')
24 29
 
25 30
   Inertia.reload({
26
-    data: pickBy({ search }),
31
+    data: pickBy({
32
+      [props.nameParam]: value,
33
+    }),
27 34
   })
28 35
 })
29 36
 </script>