@@ -15,77 +15,100 @@ import {
15
15
customerAccessTokenDelete ,
16
16
customerCreate
17
17
} from '@/api/customer/mutations'
18
+ import {
19
+ getCustomer
20
+ } from '@/api/customer/queries'
18
21
19
22
type CustomerMutTokenCreate = {
20
23
customerAccessTokenCreate : CustomerAccessTokenCreatePayload
21
24
}
22
25
23
- export const useCustomer = defineStore ( 'customer' , {
24
- state : ( ) => ( {
25
- customer : null as Customer | null ,
26
- customerAccessToken : null as CustomerAccessToken | null ,
27
- loading : false as boolean ,
28
- customerUserErrors : null as CustomerUserError [ ] ,
29
- } ) ,
30
- actions : {
31
- async customerCreate ( input : CustomerCreateInput ) {
32
- if ( input ) {
33
- const { data } = await useClient ( ) . mutate ( {
34
- mutation : customerCreate ,
35
- variables : { input }
36
- } )
37
- if ( data . customerCreate . customer ) {
38
- useRouter ( ) . push ( { name : 'account-login' , params : { email : input . email } } )
39
- }
40
- if ( data . customerCreate . customerUserErrors ) {
41
- this . customerUserErrors = data . customerCreate . customerUserErrors
42
- }
43
- }
44
- } ,
45
- async getCustomer ( accessToken : string ) {
46
- console . log ( 'getCustomer' , accessToken )
47
- } ,
48
- async login ( email : string , password : string ) {
49
- try {
50
- this . loading = true
51
- if ( ! this . customerAccessToken ) {
52
- const tokenCreateInput = { email, password }
53
- this . customerAccessTokenCreate ( tokenCreateInput )
26
+ type CustomerMutTokenDelete = { }
27
+
28
+ type CustomerGet = {
29
+ customer : Customer
30
+ }
31
+
32
+ export const useCustomer = defineStore (
33
+ 'customer' ,
34
+ {
35
+ state : ( ) => ( {
36
+ customer : null as Customer | null ,
37
+ customerAccessToken : null as CustomerAccessToken | null ,
38
+ loading : false as boolean ,
39
+ customerUserErrors : null as CustomerUserError [ ] ,
40
+ } ) ,
41
+ actions : {
42
+ async customerCreate ( input : CustomerCreateInput ) {
43
+ if ( input ) {
44
+ const { data } = await useClient ( ) . mutate ( {
45
+ mutation : customerCreate ,
46
+ variables : { input }
47
+ } )
48
+ if ( data . customerCreate . customer ) {
49
+ useRouter ( ) . push ( { name : 'account-login' , params : { email : input . email } } )
50
+ }
51
+ if ( data . customerCreate . customerUserErrors ) {
52
+ this . customerUserErrors = data . customerCreate . customerUserErrors
53
+ }
54
54
}
55
- if ( this . customerAccessToken . accessToken ) {
56
- this . getCustomer ( this . customerAccessToken . accessToken )
55
+ } ,
56
+ async login ( email : string , password : string ) {
57
+ try {
58
+ this . loading = true
59
+ if ( ! this . customerAccessToken ) {
60
+ await this . customerAccessTokenCreate ( { email, password } )
61
+ }
62
+ if ( ! this . customer ) {
63
+ await this . getCustomer ( this . customerAccessToken . accessToken )
64
+ }
65
+ } catch ( e ) {
66
+ return e
67
+ } finally {
68
+ this . loading = false
57
69
}
58
- } catch ( e ) {
59
- return e
60
- } finally {
61
- this . loading = false
62
- }
63
- } ,
64
- async logout ( ) { } ,
65
- async customerAccessTokenCreate ( input : CustomerAccessTokenCreateInput ) {
66
- try {
67
- const { data } = await useClient ( ) . mutate < CustomerMutTokenCreate > ( {
68
- mutation : customerAccessTokenCreate ,
69
- variables : {
70
- input ,
70
+ } ,
71
+ async logout ( ) { } ,
72
+ async customerAccessTokenCreate ( input : CustomerAccessTokenCreateInput ) {
73
+ try {
74
+ const { data } = await useClient ( ) . mutate < CustomerMutTokenCreate > ( {
75
+ mutation : customerAccessTokenCreate ,
76
+ variables : {
77
+ input ,
78
+ }
79
+ } )
80
+
81
+ if ( data . customerAccessTokenCreate . customerAccessToken ) {
82
+ this . customerAccessToken = data . customerAccessTokenCreate . customerAccessToken
71
83
}
72
- } )
73
- if ( data . customerAccessTokenCreate . customerAccessToken ) {
74
- this . customerAccessToken = data . customerAccessTokenCreate . customerAccessToken
84
+ if ( data . customerAccessTokenCreate . customerUserErrors ) {
85
+ this . customerUserErrors = data . customerAccessTokenCreate . customerUserErrors
86
+ }
87
+ } catch ( e ) {
88
+ return e
75
89
}
76
- if ( data . customerAccessTokenCreate . customerUserErrors ) {
77
- this . customerUserErrors = data . customerAccessTokenCreate . customerUserErrors
90
+ } ,
91
+ async getCustomer ( accessToken : string ) {
92
+ if ( accessToken ) {
93
+ const { data } = await useClient ( ) . query ( {
94
+ query : getCustomer ,
95
+ variables : { customerAccessToken : accessToken }
96
+ } )
97
+ if ( data . customer ) {
98
+ this . customer = data . customer
99
+ } else {
100
+ this . customer = null
101
+ throw new Error ( 'No customer found' )
102
+ }
78
103
}
79
- } catch ( e ) {
80
- return e
81
- }
104
+ } ,
105
+ async customerAccessTokenDelete ( ) { } ,
106
+ } ,
107
+ persist : {
108
+ paths : [ 'customer' , 'customerAccessToken' ] ,
109
+ } ,
110
+ getters : {
111
+ isSignedIn : ( state ) => ! ! state . customerAccessToken ,
82
112
} ,
83
- async customerAccessTokenDelete ( ) { } ,
84
- } ,
85
- persist : {
86
- paths : [ 'customer' , 'customerAccessToken' ] ,
87
- } ,
88
- getters : {
89
- isSignedIn : ( state ) => ! ! state . customerAccessToken ,
90
- } ,
91
- } )
113
+ }
114
+ )
0 commit comments