319 lines
7.6 KiB
JavaScript
319 lines
7.6 KiB
JavaScript
import axios from "axios";
|
|
import * as toggles from "./toggles";
|
|
import { push } from "connected-react-router";
|
|
import { backend } from "../config.js";
|
|
|
|
const autocomplete = (input) => {
|
|
return {
|
|
type: "AUTOCOMPLETE_ADD",
|
|
payload: input,
|
|
};
|
|
};
|
|
|
|
export const clearAutocomplete = () => {
|
|
return {
|
|
type: "AUTOCOMPLETE_CLEAR",
|
|
};
|
|
};
|
|
|
|
export const fetchAutocomplete = (input) => {
|
|
return function (dispatch) {
|
|
axios
|
|
.get(backend + "search/autocomplete?string=" + encodeURI(input))
|
|
.then((response) => {
|
|
const cities = Array.from(response.data.cities);
|
|
const restaurants = Array.from(response.data.restaurants);
|
|
const options = cities.concat(restaurants);
|
|
dispatch(autocomplete(options));
|
|
})
|
|
.catch((err) => {
|
|
console.log(err);
|
|
});
|
|
};
|
|
};
|
|
|
|
export const fetchSearch = (input) => {
|
|
return function (dispatch) {
|
|
axios
|
|
.get(backend + "search?string=" + encodeURI(input))
|
|
.then((response) => {
|
|
const data = response.data;
|
|
if (Object.keys(data).length > 0) {
|
|
dispatch(setSearchResults(data));
|
|
dispatch(push("/results"));
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
console.log(err);
|
|
});
|
|
};
|
|
};
|
|
|
|
const setSearchResults = (input) => {
|
|
return {
|
|
type: "SEARCH_RESULTS",
|
|
payload: input,
|
|
};
|
|
};
|
|
|
|
export const setSearchQuery = (input) => {
|
|
return {
|
|
type: "SEARCH_QUERY_SET",
|
|
payload: 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(fetchAllDishes(id));
|
|
})
|
|
.catch((err) =>
|
|
dispatch(notification("Nie udało się pobrać restauracji.", "error"))
|
|
);
|
|
};
|
|
};
|
|
|
|
const setDishes = (data) => {
|
|
return {
|
|
type: "SET_DISHES",
|
|
payload: data,
|
|
};
|
|
};
|
|
|
|
const setDish = (data) => {
|
|
return {
|
|
type: "SET_DISH",
|
|
payload: data,
|
|
};
|
|
};
|
|
|
|
export const fetchAllDishes = (id) => {
|
|
return function (dispatch) {
|
|
axios
|
|
.get(backend + "restaurant/dishes?restaurantId=" + id)
|
|
.then((response) => {
|
|
dispatch(setDishes(response.data));
|
|
dispatch(toggles.showDishes());
|
|
});
|
|
};
|
|
};
|
|
|
|
export const fetchDish = (id) => {
|
|
return function (dispatch) {
|
|
axios
|
|
.get(backend + "dish/?dishId=" + id)
|
|
.then((response) => {
|
|
dispatch(setDish(response.data));
|
|
})
|
|
.catch((err) => {
|
|
dispatch(notification("Nie udało się pobrać :(", "error"));
|
|
dispatch(push("/"));
|
|
});
|
|
};
|
|
};
|
|
|
|
export const refreshUserData = (token) => {
|
|
return function (dispatch) {
|
|
axios({
|
|
url: backend + "user/refresh",
|
|
method: "POST",
|
|
headers: {
|
|
"x-auth-token": token,
|
|
},
|
|
})
|
|
.then((response) => {
|
|
dispatch(
|
|
toggles.setLoggedIn(
|
|
response.data.firstname,
|
|
response.data.lastname,
|
|
token,
|
|
response.data.id,
|
|
response.data.email,
|
|
response.data.NIP,
|
|
response.data.adress,
|
|
response.data.companyName,
|
|
response.data.restaurants
|
|
)
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
if (err.status === 401) {
|
|
dispatch(logout());
|
|
}
|
|
console.log(err);
|
|
});
|
|
};
|
|
};
|
|
|
|
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) => {
|
|
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
|
|
)
|
|
);
|
|
dispatch(toggles.hideBackdrop());
|
|
dispatch(notification(`Witaj ${response.data.firstname}!`, "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 === 401) {
|
|
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 = () => {
|
|
return function (dispatch) {
|
|
dispatch(notification("Wylogowano.", "info"));
|
|
dispatch(toggles.setLoggedOut());
|
|
};
|
|
};
|
|
|
|
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());
|
|
}
|
|
})
|
|
.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({
|
|
message: message,
|
|
options: {
|
|
key: new Date().getTime() + Math.random(),
|
|
variant: type,
|
|
},
|
|
})
|
|
);
|
|
};
|
|
};
|
|
|
|
export const updateRestaurant = (restaurant) => {
|
|
return {
|
|
type: "UPDATE_RESTAURANT",
|
|
payload: restaurant,
|
|
};
|
|
};
|