Skip to content

Commit

Permalink
fix: 🐛 fix updateUserById api
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Jan 3, 2023
1 parent 4986866 commit d143a19
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 59 deletions.
3 changes: 0 additions & 3 deletions apps/api/src/user/dto/updateUserById.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { UserRole } from '@prisma/client';

export class UpdateUserByIdDto {
name: string;
email: string;
userRole: UserRole;
}
8 changes: 1 addition & 7 deletions apps/api/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,8 @@ export class UserController {

const name = updateUserByIdDto.name;
const email = updateUserByIdDto.email;
const userRole = updateUserByIdDto.userRole;

const user = await this.userService.updateUserById(
id,
name,
email,
userRole
);
const user = await this.userService.updateUserById(id, name, email);
if (user) {
response = {
message: 'update user by id',
Expand Down
9 changes: 2 additions & 7 deletions apps/api/src/user/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,15 @@ export class UserRepository {
return user;
}

async updateUserById(
id: string,
name: string,
email: string,
userRole: UserRole
) {
async updateUserById(id: string, name: string, email: string) {
const user = await this.prisma.user.update({
where: {
id: id,
},
data: {
name: name,
email: email,
userRoles: [userRole],
updated_at: new Date(),
},
});
return user;
Expand Down
10 changes: 2 additions & 8 deletions apps/api/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,13 @@ export class UserService {
return user;
}

async updateUserById(
id: string,
name: string,
email: string,
userRole: UserRole
) {
async updateUserById(id: string, name: string, email: string) {
let user = null;

const userFromDB = await this.userRepository.updateUserById(
id,
name,
email,
userRole
email
);
if (userFromDB) {
user = {
Expand Down
34 changes: 3 additions & 31 deletions apps/web/src/components/profile/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import TextField from '@mui/material/TextField';
import Container from '@mui/material/Container';
import Typography from '@mui/material/Typography';
import Button from '@mui/material/Button';
import InputLabel from '@mui/material/InputLabel';
import MenuItem from '@mui/material/MenuItem';
import FormControl from '@mui/material/FormControl';
import Select, { SelectChangeEvent } from '@mui/material/Select';
import { User, UserRole } from '@prisma/client';
import { User } from '@prisma/client';
import CustomBreadcrumbs from '../customBreadcrumbs/CustomBreadcrumbs';
import CustomSnackBar from '../customSnackBar/CustomSnackBar';
import * as userService from '../../services/userService';
Expand All @@ -19,7 +15,6 @@ function Profile() {

const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [userRole, setUserRole] = useState<UserRole>(UserRole.NORMAL_USER);

const [snackbarText, setSnackbarText] = useState('');
const [snackbarOpen, setSnackbarOpen] = useState(false);
Expand All @@ -41,7 +36,6 @@ function Profile() {
setUser(responseData.user);
setName(responseData.user.name);
setEmail(responseData.user.email);
setUserRole(responseData.user.userRoles[0]);
}
}
}
Expand All @@ -59,19 +53,14 @@ function Profile() {
setEmail(e.target.value);
};

const handleUserRolesChange = (event: SelectChangeEvent) => {
setUserRole(event.target.value as UserRole);
};

const handleUpdateUserClick = async () => {
const token = localStorage.getItem('token');
if (token && user && name && email && userRole) {
if (token && user && name && email) {
const response = await userService.updateUserById(
token,
user.id,
name,
email,
userRole
email
);
console.log('response = ', response);

Expand Down Expand Up @@ -121,23 +110,6 @@ function Profile() {
value={email}
onChange={(e) => handleEmailChange(e)}
/>
<div className="my-5">
<FormControl className="w-full">
<InputLabel id="demo-simple-select-helper-label">
User Roles
</InputLabel>
<Select
labelId="demo-simple-select-helper-label"
id="demo-simple-select-helper"
value={userRole}
label="User Roles"
onChange={handleUserRolesChange}
>
<MenuItem value={UserRole.NORMAL_USER}>Normal User</MenuItem>
<MenuItem value={UserRole.ADMIN}>Admin</MenuItem>
</Select>
</FormControl>
</div>
<Button
fullWidth
color="primary"
Expand Down
4 changes: 1 addition & 3 deletions apps/web/src/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ export const updateUserById = async (
token: string,
id: string,
name: string,
email: string,
userRole: UserRole
email: string
) => {
const data = {
name: name,
email: email,
userRole: userRole,
};
const response = await axios.patch(`${rootUrl}/users/${id}`, data, {
headers: {
Expand Down

0 comments on commit d143a19

Please sign in to comment.