1
1
import axios from 'axios' ;
2
- import { toast } from " react-hot-toast" ;
2
+ import { toast } from ' react-hot-toast' ;
3
3
4
- const currentURL = window . location . protocol + "//" + window . location . hostname + "/api/" ;
4
+ const currentURL =
5
+ window . location . protocol + '//' + window . location . hostname + '/api/' ;
5
6
6
7
axios . defaults . baseURL = currentURL ;
7
8
@@ -13,19 +14,19 @@ const clearAuthHeader = () => {
13
14
axios . defaults . headers . common . Authorization = '' ;
14
15
} ;
15
16
16
- export const logIn = async ( credentials ) => {
17
- try {
18
- const resp = await axios . post ( '/admin/login' , credentials ) ;
19
- setAuthHeader ( resp . data ) ;
20
- localStorage . setItem ( 'token' , resp . data ) ;
21
- } catch ( error ) {
22
- toast . error ( " The email address or password is incorrect. Please retry" , {
23
- duration : 3000 ,
24
- } ) ;
25
- }
26
- } ;
17
+ export const logIn = async credentials => {
18
+ try {
19
+ const resp = await axios . post ( '/admin/login' , credentials ) ;
20
+ setAuthHeader ( resp . data ) ;
21
+ localStorage . setItem ( 'token' , resp . data ) ;
22
+ } catch ( error ) {
23
+ toast . error ( ' The email address or password is incorrect. Please retry' , {
24
+ duration : 3000 ,
25
+ } ) ;
26
+ }
27
+ } ;
27
28
28
- export const logOut = async ( ) => {
29
+ export const logOut = async ( ) => {
29
30
try {
30
31
await axios . post ( '/admin/logout' ) ;
31
32
clearAuthHeader ( ) ;
@@ -43,10 +44,10 @@ export const refreshAdmin = async () => {
43
44
const response = await axios . get ( '/admin/check' ) ;
44
45
return response ;
45
46
} catch ( error ) {
46
- if ( error . response && error . response . status === 401 ) {
47
+ if ( error . response && error . response . status === 401 ) {
47
48
localStorage . clear ( ) ;
48
49
} else {
49
- toast . error ( " Something went wrong. Please retry" , {
50
+ toast . error ( ' Something went wrong. Please retry' , {
50
51
duration : 3000 ,
51
52
} ) ;
52
53
}
@@ -55,69 +56,66 @@ export const refreshAdmin = async () => {
55
56
} ;
56
57
57
58
export const getAppointments = async ( ) => {
58
- try {
59
- const resp = await axios . get ( '/appointments' ) ;
60
- return resp . data ;
59
+ try {
60
+ const resp = await axios . get ( '/appointments' ) ;
61
+ return resp . data ;
62
+ } catch ( error ) {
63
+ toast . error ( 'Oops! Something went wrong. Please retry later' , {
64
+ duration : 3000 ,
65
+ } ) ;
66
+ }
67
+ } ;
61
68
62
- } catch ( error ) {
63
- toast . error ( "Oops! Something went wrong. Please retry later" , {
64
- duration : 3000 ,
65
- } ) ;
69
+ export const getAppointmentsByDate = async date => {
70
+ try {
71
+ if ( ! axios . defaults . headers . common . Authorization ) {
72
+ return ;
66
73
}
67
- } ;
68
-
69
- export const getAppointmentsByDate = async ( date ) => {
70
- try {
71
- if ( ! axios . defaults . headers . common . Authorization ) {
72
- return ;
73
- }
74
- const resp = await axios . get ( `/appointments?date=${ date } ` ) ;
75
- return resp . data ;
76
-
77
- } catch ( error ) {
78
- toast . error ( "Oops! Something went wrong. Please retry later" , {
79
- duration : 3000 ,
80
- } ) ;
74
+ const resp = await axios . get ( `/appointments?date=${ date } ` ) ;
75
+ return resp . data ;
76
+ } catch ( error ) {
77
+ toast . error ( 'Oops! Something went wrong. Please retry later' , {
78
+ duration : 3000 ,
79
+ } ) ;
80
+ }
81
+ } ;
82
+
83
+ export const getAppointmentsByMonth = async month => {
84
+ try {
85
+ if ( ! axios . defaults . headers . common . Authorization ) {
86
+ return ;
81
87
}
88
+ const resp = await axios . get ( `/appointments?month=${ month } ` ) ;
89
+ return resp . data ;
90
+ } catch ( error ) {
91
+ toast . error ( 'Oops! Something went wrong. Please retry later' , {
92
+ duration : 3000 ,
93
+ } ) ;
94
+ }
82
95
} ;
83
96
84
- export const getAppointmentsByMonth = async ( month ) => {
85
- try {
86
- if ( ! axios . defaults . headers . common . Authorization ) {
87
- return ;
88
- }
89
- const resp = await axios . get ( `/appointments?month=${ month } ` ) ;
90
- return resp . data ;
97
+ export const updateAppointment = async ( id , data ) => {
98
+ try {
99
+ const response = await axios . put ( `/appointments/${ id } ` , data ) ;
100
+ return response ;
101
+ } catch ( error ) {
102
+ toast . error ( `${ error . message } ` , {
103
+ duration : 3000 ,
104
+ } ) ;
105
+ }
106
+ } ;
91
107
92
- } catch ( error ) {
93
- toast . error ( "Oops! Something went wrong. Please retry later" , {
94
- duration : 3000 ,
95
- } ) ;
96
- }
97
- } ;
98
-
99
- export const updateAppointment = async ( id , data ) => {
100
- try {
101
- const response = await axios . put ( `/appointments/${ id } ` , data ) ;
102
- return response ;
103
- } catch ( error ) {
104
- toast . error ( `${ error . message } ` , {
105
- duration : 3000 ,
106
- } ) ;
107
- }
108
- } ;
109
-
110
- export const deleteAppointment = async ( id ) => {
111
- try {
112
- await axios . delete ( `/appointments/${ id } ` ) ;
113
- } catch ( error ) {
114
- toast . error ( `${ error . message } ` , {
115
- duration : 3000 ,
116
- } ) ;
117
- }
118
- } ;
108
+ export const deleteAppointment = async id => {
109
+ try {
110
+ await axios . delete ( `/appointments/${ id } ` ) ;
111
+ } catch ( error ) {
112
+ toast . error ( `${ error . message } ` , {
113
+ duration : 3000 ,
114
+ } ) ;
115
+ }
116
+ } ;
119
117
120
- export const sendFileToBackend = async ( data ) => {
118
+ export const sendFileToBackend = async data => {
121
119
try {
122
120
const formData = new FormData ( ) ;
123
121
Object . entries ( data ) . forEach ( ( [ key , value ] ) => {
@@ -133,12 +131,12 @@ export const sendFileToBackend = async (data) => {
133
131
}
134
132
} ;
135
133
136
- export const bookAppointment = async ( data ) => {
137
- try {
134
+ export const bookAppointment = async data => {
135
+ try {
138
136
const formData = new FormData ( ) ;
139
137
Object . entries ( data ) . forEach ( ( [ key , value ] ) => {
140
138
formData . append ( key , value ) ;
141
- } ) ;
139
+ } ) ;
142
140
const response = await axios . post ( '/appointments' , formData ) ;
143
141
return response ;
144
142
} catch ( error ) {
@@ -148,11 +146,13 @@ export const bookAppointment = async (data) => {
148
146
}
149
147
} ;
150
148
151
- export const getAvailableSlots = async ( date , duration ) => {
149
+ export const getAvailableSlots = async ( date , duration ) => {
152
150
try {
153
- const response = await axios . get ( `/appointments/slots?date=${ date } &duration=${ duration } ` ) ;
151
+ const response = await axios . get (
152
+ `/appointments/slots?date=${ date } &duration=${ duration } `
153
+ ) ;
154
154
155
- return response . data ;
155
+ return response . data ;
156
156
} catch ( error ) {
157
157
toast . error ( `${ error . message } ` , {
158
158
duration : 3000 ,
@@ -161,19 +161,22 @@ export const getAvailableSlots = async(date,duration) => {
161
161
} ;
162
162
163
163
export const getPublishableKey = async ( ) => {
164
- try {
165
- const response = await axios . get ( '/stripe/config' ) ;
166
- return response . data . publishableKey ;
167
- } catch ( error ) {
168
- console . error ( error )
169
- }
164
+ try {
165
+ const response = await axios . get ( '/stripe/config' ) ;
166
+ return response . data . publishableKey ;
167
+ } catch ( error ) {
168
+ console . error ( error ) ;
169
+ }
170
170
} ;
171
171
172
172
export const createPaymentIntent = async ( service , email ) => {
173
173
try {
174
- const response = await axios . post ( `/stripe/create-payment-intent/${ service } ` , { email} ) ;
174
+ const response = await axios . post (
175
+ `/stripe/create-payment-intent/${ service } ` ,
176
+ { email }
177
+ ) ;
175
178
return response . data . clientSecret ;
176
179
} catch ( error ) {
177
- console . error ( error )
180
+ console . error ( error ) ;
178
181
}
179
- } ;
182
+ } ;
0 commit comments