@@ -25,9 +25,57 @@ const store = new Vuex.Store({
2525 state : {
2626 customers : [ ] ,
2727 alerts : null ,
28+ loading : false ,
29+ customerLoading : false ,
30+ currentCustomer : null ,
2831 } ,
2932 getters : { } ,
3033 actions : {
34+ customers : async context => {
35+ const query = JSON . stringify ( {
36+ query : `
37+ query {
38+ customers {
39+ email
40+ _id
41+ firstName
42+ lastName
43+ }
44+ }
45+ ` ,
46+ } ) ;
47+ context . commit ( 'SET_LOADING' , true ) ;
48+ const res = await sendRequest ( query ) ;
49+ context . commit ( 'SET_LOADING' , false ) ;
50+ if ( res . errors ) {
51+ return res . errors ;
52+ }
53+ context . commit ( 'SET_CUSTOMERS' , res . data . customers ) ;
54+ } ,
55+ customer : async ( context , payload ) => {
56+ context . commit ( 'SET_CUSTOMER_LOADING' , true ) ;
57+ const query = JSON . stringify ( {
58+ query : `
59+ query {
60+ customer (_id: "${ payload } ") {
61+ email
62+ phone
63+ firstName
64+ lastName
65+ address
66+ state
67+ city
68+ }
69+ }
70+ ` ,
71+ } ) ;
72+ const res = await sendRequest ( query ) ;
73+ context . commit ( 'SET_CUSTOMER_LOADING' , false ) ;
74+ if ( res . errors ) {
75+ return res . errors ;
76+ }
77+ context . commit ( 'VIEW_CUSTOMER' , res . data . customer ) ;
78+ } ,
3179 addCustomer : async ( context , customer ) => {
3280 const query = JSON . stringify ( {
3381 query : `
@@ -54,6 +102,22 @@ const store = new Vuex.Store({
54102 }
55103 context . commit ( 'ADD_CUSTOMER' , res . data . createCustomer ) ;
56104 } ,
105+ deleteCustomer : async ( context , payload ) => {
106+ const query = JSON . stringify ( {
107+ query : `
108+ mutation {
109+ deleteCustomer(_id: "${ payload } ") {
110+ email
111+ firstName
112+ }
113+ }
114+ ` ,
115+ } ) ;
116+ const res = await sendRequest ( query ) ;
117+ if ( res . errors ) {
118+ return res . errors ;
119+ }
120+ } ,
57121 setAlert : async ( context , payload ) => {
58122 context . commit ( 'SET_ALERT' , payload ) ;
59123 setTimeout ( ( ) => {
@@ -71,7 +135,22 @@ const store = new Vuex.Store({
71135 return ( state . alerts = null ) ;
72136 } ,
73137 ADD_CUSTOMER ( state , payload ) {
74- return ( state . customers = [ ...state . customers , ...payload ] ) ;
138+ return ( state . customers = [ ...state . customers , payload ] ) ;
139+ } ,
140+ SET_LOADING ( state , payload ) {
141+ return ( state . loading = payload ) ;
142+ } ,
143+ SET_CUSTOMERS ( state , payload ) {
144+ return ( state . customers = payload ) ;
145+ } ,
146+ SET_CUSTOMER_LOADING ( state , payload ) {
147+ return ( state . customerLoading = payload ) ;
148+ } ,
149+ VIEW_CUSTOMER ( state , payload ) {
150+ return ( state . currentCustomer = payload ) ;
151+ } ,
152+ CLEAR_CURRENT_CUSTOMER ( state ) {
153+ return ( state . currentCustomer = null ) ;
75154 } ,
76155 } ,
77156} ) ;
0 commit comments