@iankibetsh/vue-streamline 1.2.3 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -48,6 +48,14 @@ tasksService.getTasks().then(response => {
48
48
  console.log(response)
49
49
  })
50
50
  ```
51
+ A swal popup asking the user to confirm the action will be shown before the action is called
52
+
53
+ ## Pre Confirm Calling a action/method
54
+
55
+ You can pre confirm calling an action/method in the service by calling the method on the service object
56
+ ```js
57
+ const res = await tasksService.confirm('Are you sure you want to delete this task?').deleteTask(1)
58
+ ```
51
59
 
52
60
 
53
61
  ## Getting url to an action/method in the service
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iankibetsh/vue-streamline",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "description": "Vue library for streamlining laravel backend services with @iankibet/streamline composer package",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/App.vue CHANGED
@@ -2,7 +2,7 @@
2
2
  import useStreamline from './composables/useStreamline.js'
3
3
  import { onMounted, ref, toRefs } from 'vue'
4
4
 
5
- const {loading, service:paybillService, getActionUrl,props} = useStreamline('mpesa/paybill')
5
+ const {loading, service:paybillService, getActionUrl,props,confirmAction} = useStreamline('mpesa/paybill')
6
6
 
7
7
 
8
8
  const foundPaybill = ref(null)
@@ -13,8 +13,14 @@ onMounted(()=>{
13
13
  // })
14
14
  })
15
15
  const findPaybill = async ()=>{
16
- foundPaybill.value = await paybillService.getPaybill(32)
17
- console.log(getActionUrl('getPaybill',32))
16
+ // const res = confirmAction('Are you sure?')
17
+ // res.getPaybill(32).then(res=>{
18
+ // foundPaybill.value = res
19
+ // })
20
+ foundPaybill.value = await paybillService.confirm().getPaybill(32)
21
+ .catch(err=>{
22
+ console.log(err)
23
+ })
18
24
  }
19
25
  </script>
20
26
 
@@ -1,12 +1,13 @@
1
1
  import { inject, onMounted, reactive, ref } from 'vue'
2
- import { shApis } from '@iankibetsh/shframework'
2
+ import { shApis, shRepo } from '@iankibetsh/shframework'
3
3
 
4
4
  const useStreamline = (stream, ...initialArgs) => {
5
5
  let formData = {}
6
6
  const loading = ref(false)
7
7
  const propertiesFetched = ref(false)
8
- const fetching = ref(false)
9
- // cache key for local storage, include initialArgs in the key
8
+ const confirmationMessage = ref(null)
9
+
10
+ // Cache key for local storage, include initialArgs in the key
10
11
  const cacheKey = `streamline_${stream}_${initialArgs.join('_')}`
11
12
 
12
13
  // Inject headers and API endpoint
@@ -15,7 +16,7 @@ const useStreamline = (stream, ...initialArgs) => {
15
16
 
16
17
  const originalProps = reactive({})
17
18
 
18
- // Proxy to intercept access
19
+ // Proxy to intercept access
19
20
  const props = new Proxy(originalProps, {
20
21
  get(target, property, receiver) {
21
22
  if (!propertiesFetched.value) {
@@ -29,8 +30,7 @@ const useStreamline = (stream, ...initialArgs) => {
29
30
  })
30
31
 
31
32
  const fetchServiceProperties = async () => {
32
- if (loading.value || fetching.value || propertiesFetched.value) return
33
- fetching.value = true
33
+ if (loading.value || propertiesFetched.value) return
34
34
 
35
35
  try {
36
36
  loading.value = true
@@ -46,14 +46,13 @@ const useStreamline = (stream, ...initialArgs) => {
46
46
  throw error
47
47
  } finally {
48
48
  propertiesFetched.value = true
49
- fetching.value = false
50
49
  loading.value = false
51
50
  }
52
51
  }
53
52
 
54
53
  const assignProperties = (data) => {
55
- Object.assign(service, data.properties) // Assign the properties to the reactive service object
56
- Object.assign(originalProps, data.properties) // Assign the properties to the reactive service object
54
+ Object.assign(service, data.properties)
55
+ Object.assign(originalProps, data.properties)
57
56
  }
58
57
 
59
58
  const handler = {
@@ -63,22 +62,46 @@ const useStreamline = (stream, ...initialArgs) => {
63
62
  fetchServiceProperties().then(() => target[prop])
64
63
  }
65
64
 
66
- // Handle existing properties
67
65
  if (prop in target) {
68
66
  return target[prop]
69
67
  }
70
- // Handle nonexistent properties or dynamic methods
68
+
71
69
  return (...args) => {
70
+ if(prop === 'confirm'){
71
+ return confirmAction(args[0] ?? 'Are you sure?')
72
+ }
73
+ let repo = null
74
+ const data = {
75
+ action: prop,
76
+ stream,
77
+ ...formData,
78
+ params: args
79
+ }
80
+ if (confirmationMessage.value) {
81
+ repo = shRepo.runPlainRequest(streamlineUrl,null, confirmationMessage.value,data)
82
+ } else {
83
+ repo = shApis
84
+ .doPost(streamlineUrl, data);
85
+ }
86
+
72
87
  loading.value = true
73
- return shApis
74
- .doPost(streamlineUrl, {
75
- action: prop,
76
- stream,
77
- ...formData,
78
- params: args
79
- })
80
- .then((response) => {
81
- loading.value = false
88
+ return repo.then((response) => {
89
+ loading.value = false
90
+ if(confirmationMessage.value){
91
+ confirmationMessage.value = null
92
+
93
+ if(!response.isConfirmed){
94
+ return
95
+ }
96
+ if(response.value?.success){
97
+ return response.value.response
98
+ } else {
99
+ // throw error
100
+ throw response.value.error
101
+ }
102
+
103
+ }
104
+
82
105
  return response.data
83
106
  })
84
107
  .catch((error) => {
@@ -89,7 +112,6 @@ const useStreamline = (stream, ...initialArgs) => {
89
112
  }
90
113
  }
91
114
  }
92
-
93
115
  const getActionUrl = (action, ...args) => {
94
116
  // console.log('getActionUrl called with:', action, args);
95
117
  const post = {
@@ -99,8 +121,13 @@ const useStreamline = (stream, ...initialArgs) => {
99
121
  }
100
122
  return `${streamlineUrl}?${new URLSearchParams(post).toString()}`
101
123
  }
124
+ const service = reactive({})
102
125
 
103
- const service = reactive({}) // Make the service object reactive
126
+ // Confirmation wrapper
127
+ const confirmAction = (message) => {
128
+ confirmationMessage.value = message
129
+ return new Proxy(service, handler)
130
+ }
104
131
 
105
132
  onMounted(() => {
106
133
  if (!enableCache) return
@@ -111,13 +138,13 @@ const useStreamline = (stream, ...initialArgs) => {
111
138
  if (initialArgs.length > 0) {
112
139
  fetchServiceProperties()
113
140
  }
114
-
115
141
  })
116
142
 
117
143
  return {
118
144
  loading,
119
145
  service: new Proxy(service, handler),
120
146
  getActionUrl,
147
+ confirmAction,
121
148
  props
122
149
  }
123
150
  }