Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend] add the ability to edit user_confidence_level (#4304) #5323

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class ListLinesContent extends Component {
registerChild(ref);
}}
autoHeight={true}
height={height}
height={height || 0}
labo-flg marked this conversation as resolved.
Show resolved Hide resolved
onRowsRendered={onRowsRendered}
isScrolling={isScrolling}
onScroll={onChildScroll}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ const useStyles = makeStyles(() => ({
}));

interface ConfidenceFieldProps {
name?: string;
label?: string;
labo-flg marked this conversation as resolved.
Show resolved Hide resolved
variant?: string;
onSubmit?: (name: string, value: string | number | number[]) => void;
onSubmit?: (name: string, value: string) => void;
labo-flg marked this conversation as resolved.
Show resolved Hide resolved
onFocus?: (name: string, value: string) => void;
editContext?:
| readonly ({
Expand All @@ -34,6 +36,8 @@ interface ConfidenceFieldProps {
}

const ConfidenceField: FunctionComponent<ConfidenceFieldProps> = ({
name = 'confidence',
label,
variant,
onFocus,
onSubmit,
Expand All @@ -43,6 +47,8 @@ const ConfidenceField: FunctionComponent<ConfidenceFieldProps> = ({
disabled,
}) => {
const { t } = useFormatter();
const finalLabel = label || t('Confidence level');

const classes = useStyles();
return (
<Alert
Expand All @@ -58,9 +64,9 @@ const ConfidenceField: FunctionComponent<ConfidenceFieldProps> = ({
containerstyle={containerStyle}
fullWidth={true}
entityType={entityType}
attributeName="confidence"
name={'confidence'}
label={t('Confidence level')}
attributeName={name}
name={name}
label={finalLabel}
onFocus={onFocus}
onSubmit={onSubmit}
editContext={editContext}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import React, { FunctionComponent } from 'react';
import { createFragmentContainer, graphql, useMutation } from 'react-relay';
import { Field, Form, Formik } from 'formik';
import * as R from 'ramda';
import { pick } from 'ramda';
import * as Yup from 'yup';
import MenuItem from '@mui/material/MenuItem';
import ConfidenceField from '@components/common/form/ConfidenceField';
import TextField from '../../../../components/TextField';
import SelectField from '../../../../components/SelectField';
import { SubscriptionFocus } from '../../../../components/Subscription';
Expand All @@ -16,7 +16,7 @@ import { UserEditionOverview_user$data } from './__generated__/UserEditionOvervi
import DateTimePickerField from '../../../../components/DateTimePickerField';
import { fieldSpacingContainerStyle } from '../../../../utils/field';
import useAuth from '../../../../utils/hooks/useAuth';
import { isOnlyOrganizationAdmin } from '../../../../utils/hooks/useGranted';
import useGranted, { isOnlyOrganizationAdmin, SETTINGS_SETACCESSES } from '../../../../utils/hooks/useGranted';

const userMutationFieldPatch = graphql`
mutation UserEditionOverviewFieldPatchMutation(
Expand Down Expand Up @@ -76,6 +76,10 @@ const userValidation = (t: (value: string) => string, userIsOnlyOrganizationAdmi
account_status: Yup.string(),
account_lock_after_date: Yup.date().nullable(),
objectOrganization: userIsOnlyOrganizationAdmin ? Yup.array().min(1, t('Minimum one organization')).required(t('This field is required')) : Yup.array(),
max_confidence: Yup.number()
.required(t('This field is required'))
.min(0, t('The value must be greater than or equal to 0'))
.max(100, t('The value must be less than or equal to 100')),
});

interface UserEditionOverviewComponentProps {
Expand All @@ -93,6 +97,8 @@ UserEditionOverviewComponentProps
> = ({ user, context }) => {
const { t } = useFormatter();
const { me, settings } = useAuth();
const hasSetAccess = useGranted([SETTINGS_SETACCESSES]);

const [commitFocus] = useMutation(userEditionOverviewFocus);
const [commitFieldPatch] = useMutation(userMutationFieldPatch);
const [commitGroupAdd] = useMutation(userMutationGroupAdd);
Expand All @@ -102,24 +108,19 @@ UserEditionOverviewComponentProps
const external = user.external === true;
const objectOrganization = convertOrganizations(user);

const initialValues = pick(
[
'name',
'user_email',
'firstname',
'lastname',
'language',
'api_token',
'objectOrganization',
'description',
'account_status',
'account_lock_after_date',
],
{
...user,
objectOrganization,
},
);
const initialValues = {
name: user.name,
user_email: user.user_email,
firstname: user.firstname,
lastname: user.lastname,
language: user.language,
api_token: user.api_token,
description: user.description,
account_status: user.account_status,
account_lock_after_date: user.account_lock_after_date,
max_confidence: user.user_confidence_level.max_confidence,
objectOrganization,
};

const handleChangeFocus = (name: string) => {
commitFocus({
Expand All @@ -132,16 +133,31 @@ UserEditionOverviewComponentProps
});
};

const handleSubmitField = (name: string, value: string | Date) => {
const handleSubmitField = (name: string, value: string) => {
userValidation(t, userIsOnlyOrganizationAdmin)
.validateAt(name, { [name]: value })
.then(() => {
commitFieldPatch({
variables: {
id: user.id,
input: { key: name, value: value || '' },
},
});
// specific case for user confidence level that must be updated as a full object (we cannot field patch non-multiple objects for now)
// We pass the existing overrides for this user so they are unchanged
labo-flg marked this conversation as resolved.
Show resolved Hide resolved
if (name === 'max_confidence') {
commitFieldPatch({
variables: {
id: user.id,
input: {
key: 'user_confidence_level',
value: { max_confidence: parseInt(value, 10), overrides: user.user_confidence_level.overrides },
},
},
});
} else {
// simple case for all flat attributes
commitFieldPatch({
variables: {
id: user.id,
input: { key: name, value: value || '' },
},
});
}
})
.catch(() => false);
};
Expand Down Expand Up @@ -320,6 +336,20 @@ UserEditionOverviewComponentProps
onFocus={handleChangeFocus}
onChange={handleSubmitField}
/>
{
hasSetAccess && (
<ConfidenceField
name="max_confidence"
label={t('Max Confidence Level')}
onFocus={handleChangeFocus}
onSubmit={handleSubmitField}
entityType="User"
containerStyle={fieldSpacingContainerStyle}
editContext={context}
variant="edit"
/>
)
}
</Form>
)}
</Formik>
Expand Down Expand Up @@ -351,6 +381,13 @@ const UserEditionOverview = createFragmentContainer(
otp_qr
account_status
account_lock_after_date
user_confidence_level {
max_confidence
overrides {
entity_type
max_confidence
}
}
roles(orderBy: $rolesOrderBy, orderMode: $rolesOrderMode) {
id
name
Expand Down