Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31b3f6c
commit 7a3967f
Showing
25 changed files
with
751 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { PickType } from "@nestjs/mapped-types"; | ||
import { IsString } from "class-validator"; | ||
import { UserDTO } from "src/user/dto/user.dto"; | ||
|
||
export class UpdatePasswordDTO extends PickType(UserDTO, ["password"]) { | ||
@IsString() | ||
oldPassword: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Expose, plainToClass } from "class-transformer"; | ||
import { Allow } from "class-validator"; | ||
import { UserDTO } from "./user.dto"; | ||
|
||
export class CreateUserDTO extends UserDTO{ | ||
|
||
@Expose() | ||
@Allow() | ||
isAdmin: boolean; | ||
|
||
from(partial: Partial<CreateUserDTO>) { | ||
return plainToClass(CreateUserDTO, partial, { excludeExtraneousValues: true }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { ActionIcon, Box, Group, Skeleton, Table } from "@mantine/core"; | ||
import { useModals } from "@mantine/modals"; | ||
import { TbCheck, TbEdit, TbTrash } from "react-icons/tb"; | ||
import User from "../../types/user.type"; | ||
import showUpdateUserModal from "./showUpdateUserModal"; | ||
|
||
const ManageUserTable = ({ | ||
users, | ||
getUsers, | ||
deleteUser, | ||
isLoading, | ||
}: { | ||
users: User[]; | ||
getUsers: () => void; | ||
deleteUser: (user: User) => void; | ||
isLoading: boolean; | ||
}) => { | ||
const modals = useModals(); | ||
|
||
return ( | ||
<Box sx={{ display: "block", overflowX: "auto", whiteSpace: "nowrap" }}> | ||
<Table verticalSpacing="sm" highlightOnHover> | ||
<thead> | ||
<tr> | ||
<th>Username</th> | ||
<th>Email</th> | ||
<th>Admin</th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{isLoading | ||
? skeletonRows | ||
: users.map((user) => ( | ||
<tr key={user.id}> | ||
<td>{user.username}</td> | ||
<td>{user.email}</td> | ||
<td>{user.isAdmin && <TbCheck />}</td> | ||
<td> | ||
<Group position="right"> | ||
<ActionIcon | ||
variant="light" | ||
color="primary" | ||
size="sm" | ||
onClick={() => | ||
showUpdateUserModal(modals, user, getUsers) | ||
} | ||
> | ||
<TbEdit /> | ||
</ActionIcon> | ||
<ActionIcon | ||
variant="light" | ||
color="red" | ||
size="sm" | ||
onClick={() => deleteUser(user)} | ||
> | ||
<TbTrash /> | ||
</ActionIcon> | ||
</Group> | ||
</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</Table> | ||
</Box> | ||
); | ||
}; | ||
|
||
const skeletonRows = [...Array(10)].map((v, i) => ( | ||
<tr key={i}> | ||
<td> | ||
<Skeleton key={i} height={20} /> | ||
</td> | ||
<td> | ||
<Skeleton key={i} height={20} /> | ||
</td> | ||
<td> | ||
<Skeleton key={i} height={20} /> | ||
</td> | ||
<td> | ||
<Skeleton key={i} height={20} /> | ||
</td> | ||
</tr> | ||
)); | ||
|
||
export default ManageUserTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { | ||
Button, | ||
Group, | ||
Input, | ||
PasswordInput, | ||
Stack, | ||
Switch, | ||
TextInput, | ||
Title, | ||
} from "@mantine/core"; | ||
import { useForm, yupResolver } from "@mantine/form"; | ||
import { ModalsContextProps } from "@mantine/modals/lib/context"; | ||
import * as yup from "yup"; | ||
import userService from "../../services/user.service"; | ||
import toast from "../../utils/toast.util"; | ||
|
||
const showCreateUserModal = ( | ||
modals: ModalsContextProps, | ||
getUsers: () => void | ||
) => { | ||
return modals.openModal({ | ||
title: <Title order={5}>Create user</Title>, | ||
children: <Body modals={modals} getUsers={getUsers} />, | ||
}); | ||
}; | ||
|
||
const Body = ({ | ||
modals, | ||
getUsers, | ||
}: { | ||
modals: ModalsContextProps; | ||
getUsers: () => void; | ||
}) => { | ||
const form = useForm({ | ||
initialValues: { | ||
username: "", | ||
email: "", | ||
password: "", | ||
isAdmin: false, | ||
}, | ||
validate: yupResolver( | ||
yup.object().shape({ | ||
email: yup.string().email(), | ||
username: yup.string().min(3), | ||
password: yup.string().min(8), | ||
}) | ||
), | ||
}); | ||
|
||
return ( | ||
<Stack> | ||
<form | ||
onSubmit={form.onSubmit(async (values) => { | ||
console.log(values) | ||
userService | ||
.create(values) | ||
.then(() => { | ||
getUsers(); | ||
modals.closeAll(); | ||
}) | ||
.catch(toast.axiosError); | ||
})} | ||
> | ||
<Stack> | ||
<TextInput label="Username" {...form.getInputProps("username")} /> | ||
<TextInput | ||
type="email" | ||
label="Email" | ||
{...form.getInputProps("email")} | ||
/> | ||
<PasswordInput | ||
label="New password" | ||
{...form.getInputProps("password")} | ||
/> | ||
|
||
|
||
<Switch labelPosition="left" label="Admin privileges" {...form.getInputProps("isAdmin")} /> | ||
|
||
<Group position="right"> | ||
<Button type="submit">Create</Button> | ||
</Group> | ||
</Stack> | ||
</form> | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default showCreateUserModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.