Login / Register / SideMenu / Fixes

This commit is contained in:
2020-08-09 17:50:53 +02:00
parent e5a68f4b85
commit 769be397e5
19 changed files with 960 additions and 160 deletions

View File

@@ -1,5 +1,6 @@
import axios from "axios";
import * as toggles from "./toggles";
const backend = "http://localhost:4000/";
const autocomplete = (input) => {
return {
@@ -17,9 +18,7 @@ export const clearAutocomplete = () => {
export const fetchAutocomplete = (input) => {
return function (dispatch) {
axios
.get(
"http://localhost:4000/search/autocomplete?string=" + encodeURI(input)
)
.get(backend + "search/autocomplete?string=" + encodeURI(input))
.then((response) => {
const cities = Array.from(response.data.cities);
const restaurants = Array.from(response.data.restaurants);
@@ -36,7 +35,7 @@ export const fetchAutocomplete = (input) => {
export const fetchSearch = (input) => {
return function (dispatch) {
axios
.get("http://localhost:4000/search?string=" + encodeURI(input))
.get(backend + "search?string=" + encodeURI(input))
.then((response) => {
const data = response.data;
if (Object.keys(data).length > 0) {
@@ -80,7 +79,7 @@ export const setRestaurant = (restaurant) => {
export const fetchRestaurant = (id) => {
return function (dispatch) {
axios
.get("http://localhost:4000/restaurant?restaurantId=" + id)
.get(backend + "restaurant?restaurantId=" + id)
.then((response) => {
dispatch(setRestaurant(response.data));
dispatch(toggles.hideDishes());
@@ -101,7 +100,7 @@ const setDishes = (data) => {
export const fetchAllDishes = (id) => {
return function (dispatch) {
axios
.get("http://localhost:4000/restaurant/dishes?restaurantId=" + id)
.get(backend + "restaurant/dishes?restaurantId=" + id)
.then((response) => {
dispatch(setDishes(response.data));
dispatch(toggles.showDishes());
@@ -109,10 +108,38 @@ export const fetchAllDishes = (id) => {
};
};
export const tryLogin = (username) => {
export const tryLogin = (username, password) => {
const data = { email: username, password: password };
return function (dispatch) {
dispatch(toggles.setLoggedIn(username));
dispatch(toggles.hideLoginDialog());
axios
.post(backend + "user/login", data)
.then((response) => {
const jwt = response.headers["x-auth-token"];
console.log(response.headers);
dispatch(
toggles.setLoggedIn(
response.data.firstname,
jwt,
response.data.id,
response.data.email
)
);
dispatch(toggles.hideLoginDialog());
})
.catch((err) => {
if (err.response.status === 404) {
dispatch(
toggles.setLoginResult(
"Użytkownik o podanym adresie email nie istnieje."
)
);
} else if (err.response.status === 401) {
dispatch(toggles.setLoginResult("Podane hasło jest nieprawidłowe"));
} else {
dispatch(toggles.setLoginResult("Błąd serwera..."));
}
});
};
};
@@ -121,3 +148,39 @@ export const logout = () => {
dispatch(toggles.setLoggedOut());
};
};
export const tryRegister = (data) => {
return function (dispatch) {
dispatch(toggles.showRegisterCircle());
axios
.post(backend + "user/register", data)
.then((response) => {
if (response.status === 201) {
dispatch(
toggles.setRegisterResult(
"Dziękujemy za rejestrację. Teraz możesz zalogować się do swojego konta."
)
);
dispatch(toggles.hideRegisterCircle());
dispatch(toggles.hideRegisterForm());
}
})
.catch((err) => {
if (err.response.status === 500) {
dispatch(
toggles.setRegisterResult(
"Wystąpił nieoczekiwany błąd serwera, przepraszamy..."
)
);
dispatch(toggles.hideRegisterCircle());
} else if (err.response.status === 409) {
dispatch(
toggles.setRegisterResult(
"Adres email jest już zajęty, proszę użyć innego."
)
);
dispatch(toggles.hideRegisterCircle());
}
});
};
};

View File

@@ -22,10 +22,10 @@ export const hideLoginDialog = () => {
};
};
export const setLoggedIn = (username) => {
export const setLoggedIn = (username, jwt, id, email) => {
return {
type: "SET_LOGGEDIN",
payload: username,
payload: { username: username, jwt: jwt, id: id, email: email },
};
};
@@ -34,3 +34,59 @@ export const setLoggedOut = () => {
type: "SET_LOGGEDOUT",
};
};
export const showRegisterDialog = () => {
return {
type: "DIALOG_REGISTER_VISIBLE",
};
};
export const hideRegisterDialog = () => {
return {
type: "DIALOG_REGISTER_HIDDEN",
};
};
export const showRegulaminDialog = () => {
return {
type: "DIALOG_REGULAMIN_VISIBLE",
};
};
export const hideRegulaminDialog = () => {
return {
type: "DIALOG_REGULAMIN_HIDDEN",
};
};
export const showRegisterCircle = () => {
return {
type: "DIALOG_REGISTER_CIRCLE_SHOW",
};
};
export const hideRegisterCircle = () => {
return {
type: "DIALOG_REGISTER_CIRCLE_HIDE",
};
};
export const hideRegisterForm = () => {
return {
type: "DIALOG_REGISTER_FORM_HIDE",
};
};
export const setRegisterResult = (text) => {
return {
type: "DIALOG_REGISTER_SET_RESULT",
payload: text,
};
};
export const setLoginResult = (text) => {
return {
type: "DIALOG_LOGIN_SET_RESULT",
payload: text,
};
};