Skip to content

Commit

Permalink
role selection ui wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nl0 committed May 30, 2024
1 parent 197100b commit 048c251
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions catalog/app/containers/Admin/Users/Users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ interface RoleSelectValue {

const ROLE_SELECT_VALUE_EMPTY: RoleSelectValue = { selected: [], active: undefined }

Check warning on line 58 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L58

Added line #L58 was not covered by tests

const validateRoleSelect: FF.FieldValidator<RoleSelectValue> = (v) =>

Check warning on line 60 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L60

Added line #L60 was not covered by tests
v.active ? undefined : 'required'

const ROLE_NAME_ASC = R.ascend((r: Role) => r.name)

Check warning on line 63 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L63

Added line #L63 was not covered by tests

const useRoleSelectStyles = M.makeStyles((t) => ({

Check warning on line 65 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L65

Added line #L65 was not covered by tests
Expand All @@ -78,20 +81,14 @@ const useRoleSelectStyles = M.makeStyles((t) => ({
interface RoleSelectProps extends RF.FieldRenderProps<RoleSelectValue> {
roles: readonly Role[]
label?: React.ReactNode
// value?: RoleSelectValue
// input: {
// onChange: (value: RoleSelectValue) => void
// }
}

// TODO:
// - [ ] validation
function RoleSelect({
roles,
input: { value, onChange /*...input*/ },
label, // ...props
}: RoleSelectProps) {
function RoleSelect({ roles, input: { value, onChange }, meta, label }: RoleSelectProps) {
const classes = useRoleSelectStyles()

Check warning on line 87 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L86-L87

Added lines #L86 - L87 were not covered by tests

const error = meta.submitFailed && meta.error
const disabled = meta.submitting || meta.submitSucceeded

const { active, selected } = value ?? ROLE_SELECT_VALUE_EMPTY

const available = React.useMemo(
Expand Down Expand Up @@ -127,7 +124,7 @@ function RoleSelect({
const activate = (r: Role) => onChange({ selected, active: r })

Check warning on line 124 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L124

Added line #L124 was not covered by tests

return (

Check warning on line 126 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L126

Added line #L126 was not covered by tests
<M.FormControl className={classes.root} margin="normal">
<M.FormControl className={classes.root} margin="normal" error={!!error}>
{!!label && <M.InputLabel shrink>{label}</M.InputLabel>}
<div className={classes.chips}>
{selected.map((r) =>

Check warning on line 130 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L130

Added line #L130 was not covered by tests
Expand All @@ -139,6 +136,7 @@ function RoleSelect({
color="secondary"
className={classes.chip}
onDelete={() => remove(r)}

Check warning on line 138 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L138

Added line #L138 was not covered by tests
disabled={disabled}
/>
) : (
<M.Chip

Check warning on line 142 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L142

Added line #L142 was not covered by tests
Expand All @@ -150,6 +148,7 @@ function RoleSelect({
onDelete={() => remove(r)}

Check warning on line 148 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L148

Added line #L148 was not covered by tests
clickable
onClick={() => activate(r)}

Check warning on line 150 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L150

Added line #L150 was not covered by tests
disabled={disabled}
/>
),
)}
Expand All @@ -164,6 +163,7 @@ function RoleSelect({
clickable
onDelete={openAddMenu}
onClick={openAddMenu}
disabled={disabled}
/>
)}
</div>
Expand All @@ -180,7 +180,11 @@ function RoleSelect({
</M.MenuItem>
))}
</M.Menu>
<M.FormHelperText error>Assign a role please</M.FormHelperText>
{!!error && (
<M.FormHelperText error>

Check warning on line 184 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L184

Added line #L184 was not covered by tests
{error === 'required' ? 'Assign a role please' : error}
</M.FormHelperText>
)}
</M.FormControl>
)
}
Expand Down Expand Up @@ -298,6 +302,7 @@ function Invite({ close, roles, defaultRole }: InviteProps) {
name="username"
validate={validators.required as FF.FieldValidator<any>}
label="Username"
placeholder="Enter a username"
fullWidth
margin="normal"
errors={{
Expand All @@ -322,6 +327,7 @@ function Invite({ close, roles, defaultRole }: InviteProps) {
name="email"
validate={validators.required as FF.FieldValidator<any>}
label="Email"
placeholder="Enter an email"
fullWidth
margin="normal"
errors={{
Expand All @@ -331,7 +337,7 @@ function Invite({ close, roles, defaultRole }: InviteProps) {
}}
autoComplete="off"
/>
<RF.Field<RoleSelectValue> name="roles">
<RF.Field<RoleSelectValue> name="roles" validate={validateRoleSelect}>
{(props) => <RoleSelect label="Roles" roles={roles} {...props} />}

Check warning on line 341 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L341

Added line #L341 was not covered by tests
</RF.Field>
{(!!error || !!submitError) && (
Expand Down Expand Up @@ -712,10 +718,10 @@ function EditRoles({ close, roles, user }: EditRolesProps) {
push('Changes saved')
return
case 'OperationError':

Check warning on line 720 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L714-L720

Added lines #L714 - L720 were not covered by tests
// TODO
// should not happend
throw new Error(`Unexpected operation error: [${r.name}] ${r.message}`)
case 'InvalidInput':

Check warning on line 723 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L722-L723

Added lines #L722 - L723 were not covered by tests
// TODO
// should not happend
const [e] = r.errors
throw new Error(

Check warning on line 726 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L725-L726

Added lines #L725 - L726 were not covered by tests
`Unexpected input error at '${e.path}': [${e.name}] ${e.message}`,
Expand Down Expand Up @@ -759,7 +765,7 @@ function EditRoles({ close, roles, user }: EditRolesProps) {
<M.DialogTitle>Configure roles for {user.name}</M.DialogTitle>
<M.DialogContent>
<form onSubmit={handleSubmit}>
<RF.Field<RoleSelectValue> name="roles">
<RF.Field<RoleSelectValue> name="roles" validate={validateRoleSelect}>
{(props) => <RoleSelect roles={roles} {...props} />}

Check warning on line 769 in catalog/app/containers/Admin/Users/Users.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Users/Users.tsx#L769

Added line #L769 was not covered by tests
</RF.Field>
{(!!error || !!submitError) && (
Expand Down

0 comments on commit 048c251

Please sign in to comment.