Login / Register / SideMenu / Fixes
This commit is contained in:
@@ -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());
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user