Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontend/#062 user dashboard #115

Merged
merged 11 commits into from
Apr 6, 2021
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"class-methods-use-this": "off",
"import/extensions": "off",
"object-curly-newline": "warn",
"consistent-return": "warn"
"consistent-return": "warn",
"no-underscore-dangle": "warn"
},
"env": {
"browser": true,
Expand Down
2 changes: 1 addition & 1 deletion api/order/Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ orderSchema.methods.createOrdersDependencies = async function createOrdersDepend
};

orderSchema.post(
'findOneAndDelete',
'remove',
{ query: true, document: true },
async (doc) => {
try {
Expand Down
75 changes: 72 additions & 3 deletions api/user/userMeRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ router.put(
}

try {
const user = await User.findOneAndUpdate(
const user = await User.findByIdAndUpdate(
req.user.id,
{ phone: req.body.phone },
{ new: true },
Expand Down Expand Up @@ -103,11 +103,14 @@ router.put(
}

try {
const user = await User.findOneAndUpdate(
const user = await User.findByIdAndUpdate(
req.user.id,
{ name: req.body.name },
{ new: true },
).select('-password');
// const user = await User.findByIdAndUpdate(req.user.id)
console.log(req.user.id);
console.log(user);
if (!user) res.status(404).send('User not found');
await user.save();
res.status(200).json({ user: user, isAuthenticated: true });
Expand Down Expand Up @@ -138,7 +141,7 @@ router.put(
try {
const salt = await bcryptjs.genSalt(10);
const password = await bcryptjs.hash(req.body.password, salt);
const user = await User.findOneAndUpdate(
const user = await User.findByIdAndUpdate(
req.user.id,
{ password: password },
{ new: true },
Expand All @@ -153,4 +156,70 @@ router.put(
},
);

// @route PUT api/users/me/email
// @desc Update profile
// @access Private
router.put(
'/email',
authMiddleware,
check('email', 'Insert correct email address')
.notEmpty()
.isEmail()
.normalizeEmail()
.trim(),
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}

try {
const user = await User.findByIdAndUpdate(
req.user.id,
{ email: req.body.email },
{ new: true },
).select('-password');
if (!user) res.status(404).send('User not found');
await user.save();
res.status(200).json({ user: user, isAuthenticated: true });
} catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
},
);

// @route PUT api/users/me/surname
// @desc Update surname
// @access Private
router.put(
'/name',
authMiddleware,
check('surname', 'Surame is required')
.notEmpty()
.isString()
.trim()
.isLength({ min: 5, max: 255 }),
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}

try {
const user = await User.findByIdAndUpdate(
req.user.id,
{ surname: req.body.surname },
{ new: true },
).select('-password');
if (!user) res.status(404).send('User not found');
await user.save();
res.status(200).json({ user: user, isAuthenticated: true });
} catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
},
);

export default router;
24 changes: 24 additions & 0 deletions api/user/userOrdersRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ router.get(
{
path: 'tickets',
model: 'Ticket',
populate: [
{
path: 'screening',
model: 'Screening',
select: 'startDate cinemaHallId movieId',
populate: [
{
path: 'cinemaHallId',
model: 'CinemaHall',
select: 'name',
},
{
path: 'movieId',
model: 'Movie',
select: 'title',
},
],
},
{
path: 'seat',
model: 'Seat',
select: 'row column',
},
],
},
);
const orderIds = orders.map((orderData) => orderData.id);
Expand Down
31 changes: 31 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.7.1",
"axios": "^0.21.1",
"formik": "^2.2.6",
"node-sass": "^5.0.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
Expand Down
73 changes: 56 additions & 17 deletions client/src/actions/Auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {

// Register User
export const register = async (formData, dispatch) => {
console.log('registering');
try {
const source = CancelToken.source();
const res = await api.post('/users/signup', formData, {
Expand All @@ -27,9 +26,16 @@ export const register = async (formData, dispatch) => {
source.cancel();
return true;
} catch (err) {
const { errors } = err.response.data;
if (errors) {
errors.forEach((error) => alert(error.msg, 'Something went wrong'));
if (err.response) {
const { errors } = err.response.data;
if (errors) {
errors.forEach((error) => alert(error.msg, 'Something went wrong'));
}
}
if (err.request) {
console.log(err.request);
} else {
console.log('Error', err.message);
}
dispatch({
type: REGISTER_FAIL,
Expand All @@ -51,12 +57,18 @@ export const logout = async (dispatch) => {
source.cancel();
return true;
} catch (err) {
const { errors } = err.response.data;
if (err.response) {
const { errors } = err.response.data;

if (errors) {
errors.forEach((error) => dispatch(alert(error.msg, 'danger')));
if (errors) {
errors.forEach((error) => alert(error.msg, 'danger'));
}
}
if (err.request) {
console.log(err.request);
} else {
console.log('Error', err.message);
}

dispatch({
type: LOGOUT_FAIL,
});
Expand All @@ -83,8 +95,17 @@ export const checkIfIsAuthenticated = async (dispatch) => {
type: AUTH_ERROR,
});
return false;
} catch {
console.log('Cannot fetch at the moment');
} catch (err) {
if (err.response) {
console.log(err.response.data);
console.log(err.response.status);
console.log(err.response.headers);
} else if (err.request) {
console.log(err.request);
} else {
console.log('Error', err.message);
}
console.log(err.config);
}
};

Expand All @@ -105,15 +126,23 @@ export const login = async (formData, dispatch) => {
});
source.cancel();
} catch (err) {
const { errors } = err.response.data;
if (err.response) {
const { errors } = err.response.data;

if (errors) {
errors.forEach((error) => dispatch(alert(error.msg, 'danger')));
if (errors) {
errors.forEach((error) => alert(error.msg, 'danger'));
}
} else if (err.request) {
console.log(err.request);
} else {
console.log('Error', err.message);
}

console.log(err.config);
dispatch({
type: LOGIN_FAIL,
payload: null,
});
alert('Login failed');
}
};

Expand All @@ -131,8 +160,18 @@ export const loadUser = async (dispatch) => {
});
source.cancel();
} catch (err) {
dispatch({
type: AUTH_ERROR,
});
if (err.response) {
console.log(err.response.data);
console.log(err.response.status);
console.log(err.response.headers);
} else if (err.request) {
console.log(err.request);
} else {
console.log('Error', err.message);
}
console.log(err.config);
// dispatch({
// type: AUTH_ERROR,
// });
}
};
42 changes: 42 additions & 0 deletions client/src/actions/Orders/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable import/prefer-default-export */
import api from '../../services/Api';
import { ORDER_ERROR, GET_ORDER, DELETE_ORDER } from '../types';

// Get order
export const getUsersOrder = async (id) => {
try {
const res = await api.get(`users/me/orders/${id}`);
return res;
// TODO: define if there is a need for reducer
} catch (error) {
if (error.response) {
console.log(error.response.data);
} else if (error.request) {
console.log(error.request);
} else {
console.log('Error', error.message);
}
console.log(error.config);
return false;
}
};

// Delete order
export const deleteUsersOrder = async (id, dispatch) => {
try {
await api.delete(`users/me/orders/${id}`);
const user = api.get(`users/me/`);
return user;
// TODO: define if there is a need for reducer
} catch (error) {
if (error.response) {
console.log(error.response.data);
} else if (error.request) {
console.log(error.request);
} else {
console.log('Error', error.message);
}
console.log(error.config);
return false;
}
};
Loading