From 2143efbe8715cfff4c5a18f83db2f0c6adf397c4 Mon Sep 17 00:00:00 2001 From: Can Ural Date: Wed, 23 Nov 2022 19:19:21 +0200 Subject: [PATCH] Update favorite and user redux store API URLs --- src/redux/actions/favorite.js | 33 ++++++------ src/redux/actions/user.js | 95 +++++++++++++++++------------------ 2 files changed, 63 insertions(+), 65 deletions(-) diff --git a/src/redux/actions/favorite.js b/src/redux/actions/favorite.js index 98b685c..4128da5 100644 --- a/src/redux/actions/favorite.js +++ b/src/redux/actions/favorite.js @@ -1,16 +1,15 @@ -import axios from "axios"; -import toaster from "toasted-notes"; +import axios from 'axios'; +import toaster from 'toasted-notes'; -import NotificationComponent from "../../components/notification-component/NotificationComponent"; +import NotificationComponent from '../../components/notification-component/NotificationComponent'; -const url = "https://urldev-mern-react-times-api.herokuapp.com"; -// const url = `http://localhost:3000`; +const url = process.env.REACT_APP_API_URL; -export const GET_FAVORITE = "GET_FAVORITE"; -export const ADD_FAVORITE = "ADD_FAVORITE"; -export const DELETE_FAVORITE = "DELETE_FAVORITE"; -export const FETCH_FAVORITE_ERROR = "FETCH_FAVORITE_ERROR"; -export const CLEAN_FAVORITE_STATE = "CLEAN_FAVORITE_STATE"; +export const GET_FAVORITE = 'GET_FAVORITE'; +export const ADD_FAVORITE = 'ADD_FAVORITE'; +export const DELETE_FAVORITE = 'DELETE_FAVORITE'; +export const FETCH_FAVORITE_ERROR = 'FETCH_FAVORITE_ERROR'; +export const CLEAN_FAVORITE_STATE = 'CLEAN_FAVORITE_STATE'; export const getFavorite = (stock) => ({ type: GET_FAVORITE, @@ -37,7 +36,7 @@ export const cleanFavoriteState = () => ({ }); export const fetchGetFavorites = () => async (dispatch) => { - const token = JSON.parse(localStorage.getItem("token")); + const token = JSON.parse(localStorage.getItem('token')); const config = { headers: { Authorization: `Bearer ${token}` }, @@ -56,7 +55,7 @@ export const fetchAddFavorites = (stock) => async (dispatch) => { symbol: stock, }; - const token = JSON.parse(localStorage.getItem("token")); + const token = JSON.parse(localStorage.getItem('token')); const config = { headers: { Authorization: `Bearer ${token}` }, @@ -72,8 +71,8 @@ export const fetchAddFavorites = (stock) => async (dispatch) => { ), { duration: 1500 } @@ -96,7 +95,7 @@ export const fetchAddFavorites = (stock) => async (dispatch) => { }; export const fetchDeleteFavorite = (id) => async (dispatch) => { - const token = JSON.parse(localStorage.getItem("token")); + const token = JSON.parse(localStorage.getItem('token')); const config = { headers: { Authorization: `Bearer ${token}` }, @@ -111,8 +110,8 @@ export const fetchDeleteFavorite = (id) => async (dispatch) => { ), { duration: 1500 } diff --git a/src/redux/actions/user.js b/src/redux/actions/user.js index c6c468c..9901730 100644 --- a/src/redux/actions/user.js +++ b/src/redux/actions/user.js @@ -1,27 +1,26 @@ -import axios from "axios"; -import toaster from "toasted-notes"; -import { push } from "connected-react-router"; - -import NotificationComponent from "../../components/notification-component/NotificationComponent"; - -const url = "https://urldev-mern-react-times-api.herokuapp.com"; -// const url = `http://localhost:3000`; - -export const REGISTER_USER = "REGISTER_USER"; -export const LOGOUT_USER = "LOGOUT_USER"; -export const LOGIN_USER = "LOGIN_USER"; -export const DELETE_USER = "DELETE_USER"; -export const UPDATE_USER = "UPDATE_USER"; -export const GET_USER = "GET_USER"; -export const SET_TOKEN = "SET_TOKEN"; -export const USER_FETCH_ERROR = "USER_FETCH_ERROR"; -export const USER_LOADING = "USER_LOADING"; -export const USER_LOADING_END = "USER_LOADING_END"; -export const USER_MODAL_OPEN = "USER_MODAL_OPEN"; -export const USER_MODAL_CLOSE = "USER_MODAL_CLOSE"; -export const DELETE_MODAL_OPEN = "DELETE_MODAL_OPEN"; -export const DELETE_MODAL_CLOSE = "DELETE_MODAL_CLOSE"; -export const UPLOAD_AVATAR = "UPLOAD_AVATAR"; +import axios from 'axios'; +import toaster from 'toasted-notes'; +import { push } from 'connected-react-router'; + +import NotificationComponent from '../../components/notification-component/NotificationComponent'; + +const url = process.env.REACT_APP_API_URL; + +export const REGISTER_USER = 'REGISTER_USER'; +export const LOGOUT_USER = 'LOGOUT_USER'; +export const LOGIN_USER = 'LOGIN_USER'; +export const DELETE_USER = 'DELETE_USER'; +export const UPDATE_USER = 'UPDATE_USER'; +export const GET_USER = 'GET_USER'; +export const SET_TOKEN = 'SET_TOKEN'; +export const USER_FETCH_ERROR = 'USER_FETCH_ERROR'; +export const USER_LOADING = 'USER_LOADING'; +export const USER_LOADING_END = 'USER_LOADING_END'; +export const USER_MODAL_OPEN = 'USER_MODAL_OPEN'; +export const USER_MODAL_CLOSE = 'USER_MODAL_CLOSE'; +export const DELETE_MODAL_OPEN = 'DELETE_MODAL_OPEN'; +export const DELETE_MODAL_CLOSE = 'DELETE_MODAL_CLOSE'; +export const UPLOAD_AVATAR = 'UPLOAD_AVATAR'; export const registerUser = (user) => ({ type: REGISTER_USER, @@ -99,9 +98,9 @@ export const fetchRegisterUser = (user) => async (dispatch) => { dispatch(registerUser(data.user)), dispatch(userLoadingEnd()), dispatch(setToken(data.token)), - localStorage.setItem("token", JSON.stringify(data.token)), - localStorage.setItem("user", JSON.stringify(data.user)), - dispatch(push("/")), + localStorage.setItem('token', JSON.stringify(data.token)), + localStorage.setItem('user', JSON.stringify(data.user)), + dispatch(push('/')), toaster.notify( () => ( async (dispatch) => { toaster.notify( () => ( ), @@ -130,7 +129,7 @@ export const fetchRegisterUser = (user) => async (dispatch) => { }; export const fetchLogoutUser = () => async (dispatch) => { - const token = JSON.parse(localStorage.getItem("token")); + const token = JSON.parse(localStorage.getItem('token')); const config = { headers: { Authorization: `Bearer ${token}` }, @@ -142,10 +141,10 @@ export const fetchLogoutUser = () => async (dispatch) => { await axios.post(`${url}/profile/logout`, null, config); return [ - path.includes("profile") && dispatch(push("/home")), + path.includes('profile') && dispatch(push('/home')), dispatch(logOutUser()), toaster.notify( - () => , + () => , { duration: 1500 } ), ]; @@ -163,9 +162,9 @@ export const fetchLoginUser = (user) => async (dispatch) => { dispatch(loginUser(data.user)), dispatch(userLoadingEnd()), dispatch(setToken(data.token)), - localStorage.setItem("token", JSON.stringify(data.token)), - localStorage.setItem("user", JSON.stringify(data.user)), - dispatch(push("/home")), + localStorage.setItem('token', JSON.stringify(data.token)), + localStorage.setItem('user', JSON.stringify(data.user)), + dispatch(push('/home')), toaster.notify( () => ( async (dispatch) => { toaster.notify( () => ( ), @@ -199,7 +198,7 @@ export const fetchLoginUser = (user) => async (dispatch) => { // so there will be no need for this. // Adding just in case. export const fetchUser = () => async (dispatch) => { - const token = JSON.parse(localStorage.getItem("token")); + const token = JSON.parse(localStorage.getItem('token')); const config = { headers: { Authorization: `Bearer ${token}` }, @@ -212,8 +211,8 @@ export const fetchUser = () => async (dispatch) => { dispatch(registerUser(data.user)), dispatch(userLoadingEnd()), dispatch(setToken(data.token)), - localStorage.setItem("token", JSON.stringify(data.token)), - localStorage.setItem("user", JSON.stringify(data.user)), + localStorage.setItem('token', JSON.stringify(data.token)), + localStorage.setItem('user', JSON.stringify(data.user)), ]; } catch (error) { return [dispatch(userFetchError(error)), dispatch(userLoadingEnd())]; @@ -221,7 +220,7 @@ export const fetchUser = () => async (dispatch) => { }; export const fetchUpdateUser = (user) => async (dispatch) => { - const token = JSON.parse(localStorage.getItem("token")); + const token = JSON.parse(localStorage.getItem('token')); const config = { headers: { Authorization: `Bearer ${token}` }, @@ -231,8 +230,8 @@ export const fetchUpdateUser = (user) => async (dispatch) => { const data = await response.data; return [ dispatch(updateUser(data)), - localStorage.setItem("user", JSON.stringify(data)), - dispatch(push("/")), + localStorage.setItem('user', JSON.stringify(data)), + dispatch(push('/')), toaster.notify( () => ( async (dispatch) => { toaster.notify( () => ( ), @@ -260,7 +259,7 @@ export const fetchUpdateUser = (user) => async (dispatch) => { }; export const fetchDeleteUser = () => async (dispatch) => { - const token = JSON.parse(localStorage.getItem("token")); + const token = JSON.parse(localStorage.getItem('token')); const config = { headers: { Authorization: `Bearer ${token}` }, @@ -269,7 +268,7 @@ export const fetchDeleteUser = () => async (dispatch) => { await axios.delete(`${url}/profile`, config); return [ localStorage.clear(), - dispatch(push("/")), + dispatch(push('/')), dispatch(deleteUser()), toaster.notify( () => , @@ -293,7 +292,7 @@ export const fetchDeleteUser = () => async (dispatch) => { }; export const fetchUploadAvatar = (input) => async (dispatch) => { - const token = JSON.parse(localStorage.getItem("token")); + const token = JSON.parse(localStorage.getItem('token')); const config = { headers: { Authorization: `Bearer ${token}` }, @@ -304,11 +303,11 @@ export const fetchUploadAvatar = (input) => async (dispatch) => { const data = await response.data; return [ dispatch(uploadAvatar(data)), - localStorage.setItem("user", JSON.stringify(data)), + localStorage.setItem('user', JSON.stringify(data)), toaster.notify( () => ( ), @@ -321,7 +320,7 @@ export const fetchUploadAvatar = (input) => async (dispatch) => { toaster.notify( () => ( ),