import { Button, CircularProgress, FormControlLabel, makeStyles, Switch, Theme, Typography } from '@material-ui/core'; import gql from 'graphql-tag'; import { without } from 'lodash'; import * as React from 'react'; import { Mutation } from 'react-apollo'; import { useQuery } from 'react-apollo-hooks'; import { Field } from 'react-final-form'; import { RouteComponentProps } from 'react-router'; import { UserContext } from '../../App'; import Form from '../../components/Form'; import ChecklistField from '../../components/form/ChecklistField'; import { DropzoneField } from '../../components/form/DropzoneField'; import Input from '../../components/form/TextField'; import { AttachmentInput, UpdateUser, UpdateUserVariables, UserDetails, UserDetailsVariables, UserRole, } from '../../graphql'; import { minLength, required } from '../../util/validation'; const userDetailsQuery = gql` query UserDetails($userId: ID!) { userById(id: $userId) { id name roles credentials { type username } profile { id filename } enabled } } `; const mutation = gql` mutation UpdateUser( $userId: ID! $name: String! $roles: [UserRole!]! $profile: [AttachmentInput!] $enabled: Boolean ) { updateUser(id: $userId, name: $name, roles: $roles, profile: $profile, enabled: $enabled) { id name roles profile { id filename } enabled } } `; interface RouteProps { userId: string; } interface FormProps { name: string; roles: UserRole[]; profile?: AttachmentInput[]; enabled: boolean; } const useStyles = makeStyles((theme: Theme) => ({ container: { maxWidth: 300, }, form: { display: 'flex', flexDirection: 'column', }, subtitle: { color: 'gray', }, enabledContainer: { marginBottom: theme.spacing(4), }, })); interface Props extends RouteComponentProps {} const UpdateUserPage: React.FC = ({ match, history }) => { const classes = useStyles(); const { data, loading } = useQuery(userDetailsQuery, { variables: { userId: match.params.userId }, }); if (loading && (!data || !data.userById)) { return ; } if (!data || !data.userById) { return User not found; } const user = data.userById; return ( {me => { return (
{user.name}
{user.credentials && user.credentials.type}
{' '}
{user.credentials && user.credentials.username}
mutation={mutation}> {updateUser => ( onSubmit={({ name, roles, profile, enabled }) => { return updateUser({ variables: { userId: match.params.userId, name, roles, profile, enabled: !!enabled, }, }); }} initialValues={{ name: user.name, roles: user.roles, enabled: user.enabled, }} successMessage="Updated user" onSuccess={() => history.push(`/users`)} > {({ handleSubmit, values }) => { return (
{props => { return ( } label="Active" /> ); }} {!values.enabled && ( *Note, saving the user as inactive will result in them not being able to login )}
); }} )}
); }}
); }; export default UpdateUserPage;