Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/redux/actions/favorite.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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}` },
Expand All @@ -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}` },
Expand All @@ -72,8 +71,8 @@ export const fetchAddFavorites = (stock) => async (dispatch) => {
<NotificationComponent
success={true}
text={`You added ${data.symbol[0].name
.split("^")
.join("")} to your favorites successfully.`}
.split('^')
.join('')} to your favorites successfully.`}
/>
),
{ duration: 1500 }
Expand All @@ -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}` },
Expand All @@ -111,8 +110,8 @@ export const fetchDeleteFavorite = (id) => async (dispatch) => {
<NotificationComponent
success={true}
text={`You deleted ${data.symbol[0].name
.split("^")
.join("")} from your favorites.`}
.split('^')
.join('')} from your favorites.`}
/>
),
{ duration: 1500 }
Expand Down
95 changes: 47 additions & 48 deletions src/redux/actions/user.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
() => (
<NotificationComponent
Expand All @@ -119,7 +118,7 @@ export const fetchRegisterUser = (user) => async (dispatch) => {
toaster.notify(
() => (
<NotificationComponent
text={"Oops! Something went wrong!"}
text={'Oops! Something went wrong!'}
success={false}
/>
),
Expand All @@ -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}` },
Expand All @@ -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(
() => <NotificationComponent text={"Buh-Bye!"} success={true} />,
() => <NotificationComponent text={'Buh-Bye!'} success={true} />,
{ duration: 1500 }
),
];
Expand All @@ -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(
() => (
<NotificationComponent
Expand All @@ -183,7 +182,7 @@ export const fetchLoginUser = (user) => async (dispatch) => {
toaster.notify(
() => (
<NotificationComponent
text={"Oops! Something went wrong!"}
text={'Oops! Something went wrong!'}
success={false}
/>
),
Expand All @@ -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}` },
Expand All @@ -212,16 +211,16 @@ 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())];
}
};

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}` },
Expand All @@ -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(
() => (
<NotificationComponent
Expand All @@ -249,7 +248,7 @@ export const fetchUpdateUser = (user) => async (dispatch) => {
toaster.notify(
() => (
<NotificationComponent
text={"Oops! Something went wrong!"}
text={'Oops! Something went wrong!'}
success={false}
/>
),
Expand All @@ -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}` },
Expand All @@ -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(
() => <NotificationComponent success={true} text={`Buh-bye!`} />,
Expand All @@ -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}` },
Expand All @@ -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(
() => (
<NotificationComponent
text={"Avatar is changed! Wow, that looks amazing! ;)"}
text={'Avatar is changed! Wow, that looks amazing! ;)'}
success={true}
/>
),
Expand All @@ -321,7 +320,7 @@ export const fetchUploadAvatar = (input) => async (dispatch) => {
toaster.notify(
() => (
<NotificationComponent
text={"Oops! Something went wrong!"}
text={'Oops! Something went wrong!'}
success={false}
/>
),
Expand Down