diff --git a/src/actions/index.js b/src/actions/index.js
index 57a1aa4..70e321c 100644
--- a/src/actions/index.js
+++ b/src/actions/index.js
@@ -1,8 +1,10 @@
import axios from "axios";
-import * as toggles from "./toggles";
import { push } from "connected-react-router";
import { backend } from "../config.js";
import store from "../index.js";
+import * as restaurant from "../redux/slices/restaurantSlice";
+import * as toggle from "../redux/slices/togglesSlice";
+import * as notifications from "../redux/slices/notificationsSlice";
axios.defaults.withCredentials = true;
@@ -11,27 +13,37 @@ axios.interceptors.response.use(
return response;
},
(error) => {
- return new Promise((resolve) => {
- if (error.response && error.response.status === 401 && error.config && !error.config.__isRetryRequest) {
- const response = axios.post(backend + "user/refreshtoken", {withCredentials: true}).then((res) => {
- const jwt = res.headers['x-auth-token'];
- store.dispatch(setNewToken(jwt))
- error.config.__isRetryRequest = true
- error.config.headers['x-auth-token'] = jwt
- return axios(error.config);
- })
- resolve(response)
+ return new Promise((resolve) => {
+ if (
+ error.response &&
+ error.response.status === 401 &&
+ error.config &&
+ !error.config.__isRetryRequest
+ ) {
+ const response = axios
+ .post(backend + "user/refreshtoken", { withCredentials: true })
+ .then((res) => {
+ const jwt = res.headers["x-auth-token"];
+ store.dispatch(setNewToken(jwt));
+ error.config.__isRetryRequest = true;
+ error.config.headers["x-auth-token"] = jwt;
+ return axios(error.config);
+ });
+ resolve(response);
+ } else {
+ if (error.response && error.response.status === 403) {
+ store.dispatch(
+ logout("Podany użytkownik, lub hasło nie istnieje.", "error")
+ );
+ } else if (error.response && error.response.status === 400) {
+ store.dispatch(logout("Dane nieprawidłowe.", "error"));
} else {
- if(error.response && error.response.status === 403){
- store.dispatch(logout("Podany użytkownik, lub hasło nie istnieje.", "error"));
- } else if(error.response && error.response.status === 400) {
- store.dispatch(logout("Dane nieprawidłowe.", "error"));
- } else {
- store.dispatch(logout("Sesja wygasła", "info"));
- }
+ store.dispatch(logout("Sesja wygasła", "info"));
}
- })
- });
+ }
+ });
+ }
+);
const autocomplete = (input) => {
return {
@@ -49,9 +61,9 @@ export const clearAutocomplete = () => {
export const setNewToken = (token) => {
return {
type: "SET_NEW_TOKEN",
- payload: token
- }
-}
+ payload: token,
+ };
+};
export const fetchAutocomplete = (input) => {
return function (dispatch) {
@@ -100,20 +112,13 @@ export const setSearchQuery = (input) => {
};
};
-export const setRestaurant = (restaurant) => {
- return {
- type: "SET_RESTAURANT",
- payload: restaurant,
- };
-};
-
export const fetchRestaurant = (id) => {
return function (dispatch) {
axios
.get(backend + "restaurant?restaurantId=" + id)
.then((response) => {
- dispatch(setRestaurant(response.data));
- dispatch(toggles.hideDishes());
+ dispatch(restaurant.setRestaurant(response.data));
+ dispatch(toggle.hideDishes());
dispatch(fetchAllDishes(id));
})
.catch((err) =>
@@ -142,7 +147,7 @@ export const fetchAllDishes = (id) => {
.get(backend + "restaurant/dishes?restaurantId=" + id)
.then((response) => {
dispatch(setDishes(response.data));
- dispatch(toggles.showDishes());
+ dispatch(toggle.showDishes());
});
};
};
@@ -163,7 +168,7 @@ export const fetchDish = (id) => {
export const refreshUserData = () => {
return function (dispatch) {
- const state = store.getState()
+ const state = store.getState();
axios({
url: backend + "user/refresh",
method: "POST",
@@ -173,7 +178,7 @@ export const refreshUserData = () => {
})
.then((response) => {
dispatch(
- toggles.setLoggedIn(
+ toggle.setLoggedIn(
response.data.firstname,
response.data.lastname,
state.data.userData.jwt,
@@ -195,180 +200,10 @@ export const refreshUserData = () => {
};
};
-export const tryLogin = (username, password) => {
- const data = { email: username, password: password };
-
- return function (dispatch) {
- dispatch(toggles.showBackdrop());
- axios
- .post(backend + "user/login", data)
- .then((response) => {
- if(response.data.isRestaurant === true){
- const jwt = response.headers["x-auth-token"];
- dispatch(
- toggles.setLoggedIn(
- response.data.firstname,
- response.data.lastname,
- jwt,
- response.data.id,
- response.data.email,
- "",
- response.data.NIP,
- response.data.adress,
- response.data.companyName,
- response.data.restaurants,
- response.data.isRestaurant
- )
- );
- dispatch(toggles.hideBackdrop());
- dispatch(notification(`Witaj ${response.data.firstname}!`, "success"));
- dispatch(push("/"));
- } else {
- const jwt = response.headers["x-auth-token"];
- dispatch(
- toggles.setLoggedIn(
- "",
- "",
- jwt,
- response.data.id,
- response.data.email,
- response.data.login,
- "",
- "",
- "",
- [],
- response.data.isRestaurant
- )
- );
- dispatch(toggles.hideBackdrop());
- dispatch(notification(`Witaj ${response.data.login}!`, "success"));
- dispatch(push("/"));
- }
- })
- .catch((err) => {
- if (!err.response) {
- console.log(err);
- } else if (err.response.status === 404) {
- dispatch(toggles.hideBackdrop());
- dispatch(notification("Użytkownik nie istnieje :(", "error"));
- } else if (err.response.status === 403) {
- dispatch(toggles.hideBackdrop());
- dispatch(notification("Błędne dane logowania :(", "error"));
- } else {
- dispatch(toggles.hideBackdrop());
- dispatch(notification("Błąd serwera :(", "error"));
- }
- });
- };
-};
-
-export const remindPassword = (email) => {
- return function (dispatch) {
- const data = { email: email };
- dispatch(toggles.showBackdrop());
- axios
- .post(backend + "user/forgotpassword", data)
- .then((response) => {
- dispatch(toggles.hideBackdrop());
- dispatch(notification(response.data, "info"));
- })
- .catch((e) => {
- dispatch(toggles.hideBackdrop());
- dispatch(notification(e.response.data, "error"));
- });
- };
-};
-
-export const changePassword = (email, password, token) => {
- return function (dispatch) {
- const data = {
- token: token,
- email: email,
- newPass: password,
- };
- dispatch(toggles.showBackdrop());
- axios
- .post(backend + "user/resetpass", data)
- .then((response) => {
- dispatch(toggles.hideBackdrop());
- dispatch(notification(response.data, "info"));
- })
- .catch((e) => {
- dispatch(toggles.hideBackdrop());
- dispatch(notification(e.response.data, "error"));
- });
- };
-};
-
-export const logout = (message, type) => {
- return function (dispatch) {
- if(message && type){
- dispatch(notification(message, type));
- } else {
- dispatch(notification("Wylogowano.", "info"));
- }
- dispatch(toggles.hideBackdrop());
- dispatch(toggles.setLoggedOut());
- dispatch(push("/"));
- };
-};
-
-export const tryRegister = (data) => {
- return function (dispatch) {
- dispatch(toggles.showBackdrop());
- axios
- .post(backend + "user/register", data)
- .then((response) => {
- if (response.status === 201) {
- dispatch(
- notification(
- "Rejestracja przebiegła pomyślnie, możesz teraz zalogować się do swojego konta.",
- "success"
- )
- );
- dispatch(toggles.hideBackdrop());
- dispatch(push("/"))
- }
- })
- .catch((err) => {
- if (err.response.status === 500) {
- dispatch(
- notification(
- "Wystąpił nieoczekiwany błąd serwera, przepraszamy...",
- "error"
- )
- );
- dispatch(toggles.hideBackdrop());
- } else if (err.response.status === 409) {
- dispatch(
- notification(
- "Adres email jest już zajęty, proszę użyć innego.",
- "error"
- )
- );
- dispatch(toggles.hideBackdrop());
- }
- });
- };
-};
-
-export const setTempData = (data) => {
- return {
- type: "SET_TEMP_DATA",
- payload: data,
- };
-};
-
-export const clearTempData = () => {
- return {
- type: "CLEAR_TEMP_DATA",
- };
-};
-
export const notification = (message, type) => {
return function (dispatch) {
dispatch(
- toggles.enqueueSnackbar({
+ toggle.enqueueSnackbar({
message: message,
options: {
key: new Date().getTime() + Math.random(),
@@ -389,13 +224,13 @@ export const updateRestaurant = (restaurant) => {
export const setTags = (tags) => {
return {
type: "SET_TAGS",
- payload: tags
- }
-}
+ payload: tags,
+ };
+};
export const setTypes = (types) => {
return {
type: "SET_TYPES",
- payload: types
- }
-}
\ No newline at end of file
+ payload: types,
+ };
+};
diff --git a/src/actions/toggles.js b/src/actions/toggles.js
index 93ea2ad..66bb2d8 100644
--- a/src/actions/toggles.js
+++ b/src/actions/toggles.js
@@ -1,15 +1,3 @@
-export const showDishes = () => {
- return {
- type: "SET_DISHLIST_VISIBLE",
- };
-};
-
-export const hideDishes = () => {
- return {
- type: "SET_DISHLIST_HIDDEN",
- };
-};
-
export const setLoggedIn = (
firstname,
lastname,
@@ -36,7 +24,7 @@ export const setLoggedIn = (
adress: adress,
companyName: companyName,
restaurants: restaurants,
- isRestaurant: isRestaurant
+ isRestaurant: isRestaurant,
},
};
};
@@ -47,18 +35,6 @@ export const setLoggedOut = () => {
};
};
-export const hideRegulamin = () => {
- return {
- type: "DIALOG_REGULAMIN_HIDE",
- };
-};
-
-export const showRegulamin = () => {
- return {
- type: "DIALOG_REGULAMIN_SHOW",
- };
-};
-
export const enqueueSnackbar = (notification) => {
const key = notification.options && notification.options.key;
@@ -70,20 +46,3 @@ export const enqueueSnackbar = (notification) => {
},
};
};
-
-export const removeSnackbar = (key) => ({
- type: "REMOVE_SNACKBAR",
- key,
-});
-
-export const showBackdrop = () => {
- return {
- type: "SHOW_BACKDROP",
- };
-};
-
-export const hideBackdrop = () => {
- return {
- type: "HIDE_BACKDROP",
- };
-};
diff --git a/src/components/Output/Restaurant.js b/src/components/Output/Restaurant.js
index e2286ea..dc2272e 100644
--- a/src/components/Output/Restaurant.js
+++ b/src/components/Output/Restaurant.js
@@ -4,21 +4,20 @@ import DishList from "./DishList";
import CircularProgress from "@material-ui/core/CircularProgress";
import { useParams } from "react-router-dom";
import { extractTags } from "../../Services";
-import { useSelector, useDispatch } from "react-redux";
+import { useAppDispatch, useAppSelector } from "../../redux/hooks";
import { fetchRestaurant } from "../../actions";
import GoogleMapStatic from "./GoogleMapStatic";
import WorkingHours from "./WorkingHours";
import Social from "./Social";
-import LunchMenu from "./LunchMenu";
export default function Restaurant(props) {
- const restaurant = useSelector((state) => state.restaurant);
+ const restaurant = useAppSelector((state) => state.restaurant);
const { id } = useParams();
- const dispatch = useDispatch();
- const showDishList = useSelector((state) => state.data.showDishList);
+ const dispatch = useAppDispatch();
+ const showDishList = useAppSelector((state) => state.toggles.showDishList);
useEffect(() => {
- dispatch(fetchRestaurant(id));
+ dispatch(fetchRestaurant(id));
}, [dispatch, id]);
if (restaurant.name) document.title = restaurant.name;
@@ -61,7 +60,9 @@ export default function Restaurant(props) {
{restaurant.lunchHours}
++ {restaurant.lunchHours} +
)}