@@ -4,75 +4,85 @@ import axios from "@/plugins/axios";
44import { useAlertStore } from "@/stores" ;
55import { getResponseError } from "@/helpers/api" ;
66
7+ import { useGlobalStateStore } from "@/stores" ;
8+
79export default abstract class ModelService extends BaseService {
810
911 constructor ( ) {
1012 super ( ) ;
1113 this . setupAPI ( axios . defaults . baseURL + '/api' ) ;
1214 }
1315
14- public create ( id = null ) {
15- return this . get ( this . url + `/create` , { } , id ) ;
16+ public create ( ) {
17+ return this . get ( this . url + `/create` , { } ) ;
1618 }
1719
18- public find ( object_id , id = null ) {
19- return this . get ( this . url + `/${ object_id } ` , { } , id ) ;
20+ public find ( object_id ) {
21+ return this . get ( this . url + `/${ object_id } ` , { } ) ;
2022 }
2123
22- public edit ( object_id , id = null ) {
23- return this . get ( this . url + `/${ object_id } /edit` , { } , id ) ;
24+ public edit ( object_id ) {
25+ return this . get ( this . url + `/${ object_id } /edit` , { } ) ;
2426 }
2527
26- public store ( payload , id = null ) {
28+ public store ( payload ) {
2729 let data = this . transformPayloadForSubmission ( payload ) ;
2830 return this . post ( this . url , data , {
2931 headers : {
3032 'Content-Type' : 'multipart/form-data'
3133 } ,
32- } , id )
34+ } )
3335 }
3436
35- public update ( object_id , payload , id = null ) {
37+ public update ( object_id , payload ) {
3638 let data = this . transformPayloadForSubmission ( payload ) ;
3739 data . append ( '_method' , 'patch' ) ;
3840 return this . post ( this . url + `/${ object_id } ` , data , {
3941 headers : {
4042 'Content-Type' : 'multipart/form-data'
4143 } ,
42- } , id ) ;
44+ } ) ;
4345 }
4446
45- public delete ( object_id , id = null ) {
46- return super . delete ( this . url + `/${ id } ` , { } , id ) ;
47+ public delete ( object_id ) {
48+ return super . delete ( this . url + `/${ object_id } ` , { } ) ;
4749 }
4850
49- public index ( params = { } , id = null ) {
51+ public index ( params = { } ) {
5052 let path = this . url ;
5153 let query = new URLSearchParams ( params ) . toString ( ) ;
5254 if ( query ) {
5355 path += '?' + query
5456 }
55- return this . get ( path , { } , id ) ;
57+ return this . get ( path , { } ) ;
5658 }
5759
58- public handleUpdate ( object_id , form , id = null ) {
60+ public handleUpdate ( ui_element_id , object_id , data ) {
5961 const alertStore = useAlertStore ( ) ;
60- return this . update ( object_id , form , id ) . then ( ( response ) => {
62+ const globalUserState = useGlobalStateStore ( ) ;
63+ globalUserState . loadingElements [ ui_element_id ] = true ;
64+ return this . update ( object_id , data ) . then ( ( response ) => {
6165 let answer = response . data ;
6266 alertStore . success ( answer . message ) ;
6367 } ) . catch ( ( error ) => {
6468 alertStore . error ( getResponseError ( error ) ) ;
65- } ) ;
69+ } ) . finally ( ( ) => {
70+ globalUserState . loadingElements [ ui_element_id ] = false ;
71+ } )
6672 }
6773
68- public handleCreate ( form , id = null ) {
74+ public handleCreate ( ui_element_id , data ) {
6975 const alertStore = useAlertStore ( ) ;
70- return this . store ( form , id ) . then ( ( response ) => {
76+ const globalUserState = useGlobalStateStore ( ) ;
77+ globalUserState . setElementLoading ( ui_element_id , true ) ;
78+ return this . store ( data ) . then ( ( response ) => {
7179 let answer = response . data ;
7280 alertStore . success ( answer . message ) ;
7381 } ) . catch ( ( error ) => {
7482 alertStore . error ( getResponseError ( error ) ) ;
75- } ) ;
83+ } ) . finally ( ( ) => {
84+ globalUserState . setElementLoading ( ui_element_id , false ) ;
85+ } )
7686 }
7787
7888 public transformPayloadForSubmission ( payload ) {
0 commit comments