401 lines
9.9 KiB
JavaScript
401 lines
9.9 KiB
JavaScript
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";
|
|
|
|
axios.defaults.withCredentials = true;
|
|
|
|
axios.interceptors.response.use(
|
|
(response) => {
|
|
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)
|
|
} 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"));
|
|
}
|
|
}
|
|
})
|
|
});
|
|
|
|
const autocomplete = (input) => {
|
|
return {
|
|
type: "AUTOCOMPLETE_ADD",
|
|
payload: input,
|
|
};
|
|
};
|
|
|
|
export const clearAutocomplete = () => {
|
|
return {
|
|
type: "AUTOCOMPLETE_CLEAR",
|
|
};
|
|
};
|
|
|
|
export const setNewToken = (token) => {
|
|
return {
|
|
type: "SET_NEW_TOKEN",
|
|
payload: token
|
|
}
|
|
}
|
|
|
|
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 = () => {
|
|
return function (dispatch) {
|
|
const state = store.getState()
|
|
axios({
|
|
url: backend + "user/refresh",
|
|
method: "POST",
|
|
headers: {
|
|
"x-auth-token": state.data.userData.jwt,
|
|
},
|
|
})
|
|
.then((response) => {
|
|
dispatch(
|
|
toggles.setLoggedIn(
|
|
response.data.firstname,
|
|
response.data.lastname,
|
|
state.data.userData.jwt,
|
|
response.data.id,
|
|
response.data.email,
|
|
response.data.NIP,
|
|
response.data.adress,
|
|
response.data.companyName,
|
|
response.data.restaurants
|
|
)
|
|
);
|
|
})
|
|
.catch((err) => {
|
|
if (err.status === 401 || err.status === 500) {
|
|
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) => {
|
|
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({
|
|
message: message,
|
|
options: {
|
|
key: new Date().getTime() + Math.random(),
|
|
variant: type,
|
|
},
|
|
})
|
|
);
|
|
};
|
|
};
|
|
|
|
export const updateRestaurant = (restaurant) => {
|
|
return {
|
|
type: "UPDATE_RESTAURANT",
|
|
payload: restaurant,
|
|
};
|
|
};
|
|
|
|
export const setTags = (tags) => {
|
|
return {
|
|
type: "SET_TAGS",
|
|
payload: tags
|
|
}
|
|
}
|
|
|
|
export const setTypes = (types) => {
|
|
return {
|
|
type: "SET_TYPES",
|
|
payload: types
|
|
}
|
|
} |