1- import axios from "axios" ;
2- import toaster from "toasted-notes" ;
3- import { push } from "connected-react-router" ;
4-
5- import NotificationComponent from "../../components/notification-component/NotificationComponent" ;
6-
7- const url = "https://urldev-mern-react-times-api.herokuapp.com" ;
8- // const url = `http://localhost:3000`;
9-
10- export const REGISTER_USER = "REGISTER_USER" ;
11- export const LOGOUT_USER = "LOGOUT_USER" ;
12- export const LOGIN_USER = "LOGIN_USER" ;
13- export const DELETE_USER = "DELETE_USER" ;
14- export const UPDATE_USER = "UPDATE_USER" ;
15- export const GET_USER = "GET_USER" ;
16- export const SET_TOKEN = "SET_TOKEN" ;
17- export const USER_FETCH_ERROR = "USER_FETCH_ERROR" ;
18- export const USER_LOADING = "USER_LOADING" ;
19- export const USER_LOADING_END = "USER_LOADING_END" ;
20- export const USER_MODAL_OPEN = "USER_MODAL_OPEN" ;
21- export const USER_MODAL_CLOSE = "USER_MODAL_CLOSE" ;
22- export const DELETE_MODAL_OPEN = "DELETE_MODAL_OPEN" ;
23- export const DELETE_MODAL_CLOSE = "DELETE_MODAL_CLOSE" ;
24- export const UPLOAD_AVATAR = "UPLOAD_AVATAR" ;
1+ import axios from 'axios' ;
2+ import toaster from 'toasted-notes' ;
3+ import { push } from 'connected-react-router' ;
4+
5+ import NotificationComponent from '../../components/notification-component/NotificationComponent' ;
6+
7+ const url = process . env . REACT_APP_API_URL ;
8+
9+ export const REGISTER_USER = 'REGISTER_USER' ;
10+ export const LOGOUT_USER = 'LOGOUT_USER' ;
11+ export const LOGIN_USER = 'LOGIN_USER' ;
12+ export const DELETE_USER = 'DELETE_USER' ;
13+ export const UPDATE_USER = 'UPDATE_USER' ;
14+ export const GET_USER = 'GET_USER' ;
15+ export const SET_TOKEN = 'SET_TOKEN' ;
16+ export const USER_FETCH_ERROR = 'USER_FETCH_ERROR' ;
17+ export const USER_LOADING = 'USER_LOADING' ;
18+ export const USER_LOADING_END = 'USER_LOADING_END' ;
19+ export const USER_MODAL_OPEN = 'USER_MODAL_OPEN' ;
20+ export const USER_MODAL_CLOSE = 'USER_MODAL_CLOSE' ;
21+ export const DELETE_MODAL_OPEN = 'DELETE_MODAL_OPEN' ;
22+ export const DELETE_MODAL_CLOSE = 'DELETE_MODAL_CLOSE' ;
23+ export const UPLOAD_AVATAR = 'UPLOAD_AVATAR' ;
2524
2625export const registerUser = ( user ) => ( {
2726 type : REGISTER_USER ,
@@ -99,9 +98,9 @@ export const fetchRegisterUser = (user) => async (dispatch) => {
9998 dispatch ( registerUser ( data . user ) ) ,
10099 dispatch ( userLoadingEnd ( ) ) ,
101100 dispatch ( setToken ( data . token ) ) ,
102- localStorage . setItem ( " token" , JSON . stringify ( data . token ) ) ,
103- localStorage . setItem ( " user" , JSON . stringify ( data . user ) ) ,
104- dispatch ( push ( "/" ) ) ,
101+ localStorage . setItem ( ' token' , JSON . stringify ( data . token ) ) ,
102+ localStorage . setItem ( ' user' , JSON . stringify ( data . user ) ) ,
103+ dispatch ( push ( '/' ) ) ,
105104 toaster . notify (
106105 ( ) => (
107106 < NotificationComponent
@@ -119,7 +118,7 @@ export const fetchRegisterUser = (user) => async (dispatch) => {
119118 toaster . notify (
120119 ( ) => (
121120 < NotificationComponent
122- text = { " Oops! Something went wrong!" }
121+ text = { ' Oops! Something went wrong!' }
123122 success = { false }
124123 />
125124 ) ,
@@ -130,7 +129,7 @@ export const fetchRegisterUser = (user) => async (dispatch) => {
130129} ;
131130
132131export const fetchLogoutUser = ( ) => async ( dispatch ) => {
133- const token = JSON . parse ( localStorage . getItem ( " token" ) ) ;
132+ const token = JSON . parse ( localStorage . getItem ( ' token' ) ) ;
134133
135134 const config = {
136135 headers : { Authorization : `Bearer ${ token } ` } ,
@@ -142,10 +141,10 @@ export const fetchLogoutUser = () => async (dispatch) => {
142141 await axios . post ( `${ url } /profile/logout` , null , config ) ;
143142
144143 return [
145- path . includes ( " profile" ) && dispatch ( push ( " /home" ) ) ,
144+ path . includes ( ' profile' ) && dispatch ( push ( ' /home' ) ) ,
146145 dispatch ( logOutUser ( ) ) ,
147146 toaster . notify (
148- ( ) => < NotificationComponent text = { " Buh-Bye!" } success = { true } /> ,
147+ ( ) => < NotificationComponent text = { ' Buh-Bye!' } success = { true } /> ,
149148 { duration : 1500 }
150149 ) ,
151150 ] ;
@@ -163,9 +162,9 @@ export const fetchLoginUser = (user) => async (dispatch) => {
163162 dispatch ( loginUser ( data . user ) ) ,
164163 dispatch ( userLoadingEnd ( ) ) ,
165164 dispatch ( setToken ( data . token ) ) ,
166- localStorage . setItem ( " token" , JSON . stringify ( data . token ) ) ,
167- localStorage . setItem ( " user" , JSON . stringify ( data . user ) ) ,
168- dispatch ( push ( " /home" ) ) ,
165+ localStorage . setItem ( ' token' , JSON . stringify ( data . token ) ) ,
166+ localStorage . setItem ( ' user' , JSON . stringify ( data . user ) ) ,
167+ dispatch ( push ( ' /home' ) ) ,
169168 toaster . notify (
170169 ( ) => (
171170 < NotificationComponent
@@ -183,7 +182,7 @@ export const fetchLoginUser = (user) => async (dispatch) => {
183182 toaster . notify (
184183 ( ) => (
185184 < NotificationComponent
186- text = { " Oops! Something went wrong!" }
185+ text = { ' Oops! Something went wrong!' }
187186 success = { false }
188187 />
189188 ) ,
@@ -199,7 +198,7 @@ export const fetchLoginUser = (user) => async (dispatch) => {
199198// so there will be no need for this.
200199// Adding just in case.
201200export const fetchUser = ( ) => async ( dispatch ) => {
202- const token = JSON . parse ( localStorage . getItem ( " token" ) ) ;
201+ const token = JSON . parse ( localStorage . getItem ( ' token' ) ) ;
203202
204203 const config = {
205204 headers : { Authorization : `Bearer ${ token } ` } ,
@@ -212,16 +211,16 @@ export const fetchUser = () => async (dispatch) => {
212211 dispatch ( registerUser ( data . user ) ) ,
213212 dispatch ( userLoadingEnd ( ) ) ,
214213 dispatch ( setToken ( data . token ) ) ,
215- localStorage . setItem ( " token" , JSON . stringify ( data . token ) ) ,
216- localStorage . setItem ( " user" , JSON . stringify ( data . user ) ) ,
214+ localStorage . setItem ( ' token' , JSON . stringify ( data . token ) ) ,
215+ localStorage . setItem ( ' user' , JSON . stringify ( data . user ) ) ,
217216 ] ;
218217 } catch ( error ) {
219218 return [ dispatch ( userFetchError ( error ) ) , dispatch ( userLoadingEnd ( ) ) ] ;
220219 }
221220} ;
222221
223222export const fetchUpdateUser = ( user ) => async ( dispatch ) => {
224- const token = JSON . parse ( localStorage . getItem ( " token" ) ) ;
223+ const token = JSON . parse ( localStorage . getItem ( ' token' ) ) ;
225224
226225 const config = {
227226 headers : { Authorization : `Bearer ${ token } ` } ,
@@ -231,8 +230,8 @@ export const fetchUpdateUser = (user) => async (dispatch) => {
231230 const data = await response . data ;
232231 return [
233232 dispatch ( updateUser ( data ) ) ,
234- localStorage . setItem ( " user" , JSON . stringify ( data ) ) ,
235- dispatch ( push ( "/" ) ) ,
233+ localStorage . setItem ( ' user' , JSON . stringify ( data ) ) ,
234+ dispatch ( push ( '/' ) ) ,
236235 toaster . notify (
237236 ( ) => (
238237 < NotificationComponent
@@ -249,7 +248,7 @@ export const fetchUpdateUser = (user) => async (dispatch) => {
249248 toaster . notify (
250249 ( ) => (
251250 < NotificationComponent
252- text = { " Oops! Something went wrong!" }
251+ text = { ' Oops! Something went wrong!' }
253252 success = { false }
254253 />
255254 ) ,
@@ -260,7 +259,7 @@ export const fetchUpdateUser = (user) => async (dispatch) => {
260259} ;
261260
262261export const fetchDeleteUser = ( ) => async ( dispatch ) => {
263- const token = JSON . parse ( localStorage . getItem ( " token" ) ) ;
262+ const token = JSON . parse ( localStorage . getItem ( ' token' ) ) ;
264263
265264 const config = {
266265 headers : { Authorization : `Bearer ${ token } ` } ,
@@ -269,7 +268,7 @@ export const fetchDeleteUser = () => async (dispatch) => {
269268 await axios . delete ( `${ url } /profile` , config ) ;
270269 return [
271270 localStorage . clear ( ) ,
272- dispatch ( push ( "/" ) ) ,
271+ dispatch ( push ( '/' ) ) ,
273272 dispatch ( deleteUser ( ) ) ,
274273 toaster . notify (
275274 ( ) => < NotificationComponent success = { true } text = { `Buh-bye!` } /> ,
@@ -293,7 +292,7 @@ export const fetchDeleteUser = () => async (dispatch) => {
293292} ;
294293
295294export const fetchUploadAvatar = ( input ) => async ( dispatch ) => {
296- const token = JSON . parse ( localStorage . getItem ( " token" ) ) ;
295+ const token = JSON . parse ( localStorage . getItem ( ' token' ) ) ;
297296
298297 const config = {
299298 headers : { Authorization : `Bearer ${ token } ` } ,
@@ -304,11 +303,11 @@ export const fetchUploadAvatar = (input) => async (dispatch) => {
304303 const data = await response . data ;
305304 return [
306305 dispatch ( uploadAvatar ( data ) ) ,
307- localStorage . setItem ( " user" , JSON . stringify ( data ) ) ,
306+ localStorage . setItem ( ' user' , JSON . stringify ( data ) ) ,
308307 toaster . notify (
309308 ( ) => (
310309 < NotificationComponent
311- text = { " Avatar is changed! Wow, that looks amazing! ;)" }
310+ text = { ' Avatar is changed! Wow, that looks amazing! ;)' }
312311 success = { true }
313312 />
314313 ) ,
@@ -321,7 +320,7 @@ export const fetchUploadAvatar = (input) => async (dispatch) => {
321320 toaster . notify (
322321 ( ) => (
323322 < NotificationComponent
324- text = { " Oops! Something went wrong!" }
323+ text = { ' Oops! Something went wrong!' }
325324 success = { false }
326325 />
327326 ) ,
0 commit comments