Skip to content

Commit

Permalink
update types
Browse files Browse the repository at this point in the history
Signed-off-by: shanghaikid <[email protected]>
  • Loading branch information
shanghaikid committed Jul 31, 2023
1 parent aaff654 commit 70e1e21
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions client/src/pages/user/CreateUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const useStyles = makeStyles((theme: Theme) => ({
const CreateUser: FC<CreateUserProps> = ({
handleCreate,
handleClose,
roles,
roleOptions,
}) => {
const { t: commonTrans } = useTranslation();
const { t: userTrans } = useTranslation('user');
Expand Down Expand Up @@ -127,7 +127,7 @@ const CreateUser: FC<CreateUserProps> = ({
</Typography>

<FormGroup row>
{roles.map((r: RoleOption, index: number) => (
{roleOptions.map((r: RoleOption, index: number) => (
<FormControlLabel
control={
<Checkbox
Expand Down
3 changes: 2 additions & 1 deletion client/src/pages/user/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface CreateUserParams {
}

export interface UpdateUserRoleParams {
username: string;
roles: string[];
}

Expand All @@ -26,7 +27,7 @@ export interface UpdateUserRoleProps {
export interface CreateUserProps {
handleCreate: (data: CreateUserParams) => void;
handleClose: () => void;
roles: RoleOption[];
roleOptions: RoleOption[];
}

export interface UpdateUserProps {
Expand Down
22 changes: 10 additions & 12 deletions client/src/pages/user/UpdateUserRole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,24 @@ const UpdateUserRole: FC<UpdateUserRoleProps> = ({
}) => {
const { t: userTrans } = useTranslation('user');
const { t: btnTrans } = useTranslation('btn');
const [allRoles, setAllRoles] = useState([]);
const [roleOptions, setRoleOptions] = useState([]);

const [form, setForm] = useState<UpdateUserRoleParams>({
username: username,
roles: roles,
});

const classes = useStyles();

const handleUpdate = async () => {
await UserHttp.updateUserRole({
username: username,
roles: form.roles,
});
await UserHttp.updateUserRole(form);
onUpdate(form);
};

const fetchAllRoles = async () => {
const roles = await UserHttp.getRoles();

setAllRoles(roles.results.map((r: any) => r.role.name));
setRoleOptions(roles.results.map((r: any) => r.role.name));
};

useEffect(() => {
Expand All @@ -68,7 +66,7 @@ const UpdateUserRole: FC<UpdateUserRoleProps> = ({
>
<>
<FormGroup row>
{allRoles.map((r: any, index: number) => (
{roleOptions.map((roleOption: string, index: number) => (
<FormControlLabel
control={
<Checkbox
Expand All @@ -80,20 +78,20 @@ const UpdateUserRole: FC<UpdateUserRoleProps> = ({

if (!checked) {
newRoles = newRoles.filter(
(n: string | number) => n !== r
(n: string | number) => n !== roleOption
);
} else {
newRoles.push(r);
newRoles.push(roleOption);
}

setForm(v => ({ ...v, roles: [...newRoles] }));
}}
/>
}
key={index}
label={r}
value={r}
checked={form.roles.indexOf(r) !== -1}
label={roleOption}
value={roleOption}
checked={form.roles.indexOf(roleOption) !== -1}
/>
))}
</FormGroup>
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/user/User.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const Users = () => {
<CreateUser
handleCreate={handleCreate}
handleClose={handleCloseDialog}
roles={roles.results.map((r: any) => {
roleOptions={roles.results.map((r: any) => {
return { label: r.role.name, value: r.role.name };
})}
/>
Expand Down

0 comments on commit 70e1e21

Please sign in to comment.