Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/sakJH/GymNest
Browse files Browse the repository at this point in the history
  • Loading branch information
sakJH committed Apr 23, 2024
2 parents 1893ddd + 1c10336 commit ce0ce5d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const ActivityListItem = ({ schedule, onSelect, onEdit, onDelete, onReserve }) =
</IconButton>
</Tooltip>
)}
{user && onDelete && (
{user && (user.roleId === 4) && onDelete && (
<Tooltip title="Smazat">
<IconButton onClick={(e) => { e.stopPropagation(); onDelete(schedule.id); }} color="error">
<DeleteIcon />
Expand Down
21 changes: 17 additions & 4 deletions GymNest-IS/frontend/src/components/user/UserSettingsForm.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React, { useState } from 'react';
import React, { useState, useContext } from 'react';
import axios from 'axios';
import { Button, TextField, Select, MenuItem, FormControl, InputLabel, Dialog, DialogActions, DialogContent, DialogTitle, DialogContentText } from '@mui/material';
import { AuthContext } from '../AuthContext';

const UserSettingsForm = ({ user, handleClose, logout }) => {
const UserSettingsForm = ({ user, handleClose }) => {
const { setCreditsUser, token, logout } = useContext(AuthContext);
const [formData, setFormData] = useState({
username: user.username || '',
firstName: user.firstName || '',
Expand All @@ -28,7 +30,16 @@ const UserSettingsForm = ({ user, handleClose, logout }) => {
preferredCurrency: formData.preferredCurrency,
colorScheme: formData.colorScheme
};
await axios.put(`http://localhost:3001/api/users/${user.username}`, updateData);

const response = await axios.put(`http://localhost:3001/api/users/${user.username}`, updateData, {
headers: { Authorization: `Bearer ${token}` }
});

if (response.data) {
// Zde předpokládáme, že server vrací aktualizované údaje uživatele
setCreditsUser({ ...user, ...updateData });
alert('Uživatelské údaje byly úspěšně aktualizovány.');
}
} catch (error) {
console.error('Error updating user:', error);
}
Expand All @@ -46,7 +57,9 @@ const UserSettingsForm = ({ user, handleClose, logout }) => {

const confirmDelete = async () => {
try {
await axios.delete(`http://localhost:3001/api/users/delete/${user.username}`);
const response = await axios.delete(`http://localhost:3001/api/users/${user.username}`, {
headers: { Authorization: `Bearer ${token}` }
});
logout(); // Odhlášení uživatele a smazání JWT tokenu
handleClose();
} catch (error) {
Expand Down
10 changes: 5 additions & 5 deletions GymNest-IS/user-service/src/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class User extends Model {

static async deleteUserByUsername(username) {
try {
const result = await this.destroy({
where: { username }
});
if (result === 0) {
// Žádný záznam nebyl odstraněn, uživatel s tímto uživatelským jménem nebyl nalezen
const user = await this.findOne({ where: { username } });
if (!user) {
throw new Error(`Uživatel s uživatelským jménem "${username}" nebyl nalezen.`);
}

await user.destroy();

console.log(`Uživatel s uživatelským jménem "${username}" byl úspěšně odstraněn.`);
return true;
} catch (error) {
Expand Down
3 changes: 0 additions & 3 deletions GymNest-IS/user-service/src/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,4 @@ router.post('/users/:userId/credits/add', UserController.addUserCredits);
*/
router.post('/users/:userId/credits/remove', UserController.removeUserCredits);

// Route pro smazání uživatele podle uživatelského jména
router.delete('users/delete/:username', UserController.deleteUserByUsername)

module.exports = router;

0 comments on commit ce0ce5d

Please sign in to comment.