-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #671 from digitalfabrik/665-create-admin-from-admi…
…nistration 665: Create admin from administration
- Loading branch information
Showing
16 changed files
with
474 additions
and
101 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
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
157 changes: 157 additions & 0 deletions
157
administration/src/components/users/CreateUserDialog.tsx
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,157 @@ | ||
import { Button, Checkbox, Classes, Dialog, FormGroup, HTMLSelect, InputGroup } from '@blueprintjs/core' | ||
import { useContext, useState } from 'react' | ||
import styled from 'styled-components' | ||
import { Role, useCreateAdministratorMutation } from '../../generated/graphql' | ||
import { useAppToaster } from '../AppToaster' | ||
import RoleHelpButton from './RoleHelpButton' | ||
import { roleToText } from './UsersTable' | ||
import { ProjectConfigContext } from '../../project-configs/ProjectConfigContext' | ||
import RegionSelector from '../RegionSelector' | ||
import { ApolloError } from '@apollo/client' | ||
|
||
const RoleFormGroupLabel = styled.span` | ||
& span { | ||
display: inline-block !important; | ||
} | ||
` | ||
|
||
const RoleSelector = ({ | ||
role, | ||
onChange, | ||
hideProjectAdmin, | ||
}: { | ||
role: Role | null | ||
onChange: (role: Role | null) => void | ||
hideProjectAdmin: boolean | ||
}) => { | ||
return ( | ||
<HTMLSelect fill onChange={e => onChange((e.target.value as Role | null) ?? null)} value={role ?? ''} required> | ||
<option value='' disabled> | ||
Auswählen... | ||
</option> | ||
{hideProjectAdmin ? null : <option value={Role.ProjectAdmin}>{roleToText(Role.ProjectAdmin)}</option>} | ||
<option value={Role.RegionAdmin}>{roleToText(Role.RegionAdmin)}</option> | ||
<option value={Role.RegionManager}>{roleToText(Role.RegionManager)}</option> | ||
</HTMLSelect> | ||
) | ||
} | ||
|
||
const getMessageFromApolloError = (error: ApolloError): string => { | ||
const defaultMessage = 'Etwas ist schief gelaufen.' | ||
if (error.graphQLErrors.length !== 1) { | ||
return defaultMessage | ||
} | ||
const graphQLError = error.graphQLErrors[0] | ||
if ('code' in graphQLError.extensions) { | ||
switch (graphQLError.extensions['code']) { | ||
case 'EMAIL_ALREADY_EXISTS': | ||
return 'Die Email-Adresse wird bereits verwendet.' | ||
} | ||
} | ||
return defaultMessage | ||
} | ||
|
||
const CreateUserDialog = ({ | ||
isOpen, | ||
onClose, | ||
onSuccess, | ||
regionIdOverride, | ||
}: { | ||
isOpen: boolean | ||
onClose: () => void | ||
onSuccess: () => void | ||
// If regionIdOverride is set, the region selector will be hidden, and only RegionAdministrator and RegionManager | ||
// roles are selectable. | ||
regionIdOverride: number | null | ||
}) => { | ||
const appToaster = useAppToaster() | ||
const [email, setEmail] = useState('') | ||
const [role, setRole] = useState<Role | null>(null) | ||
const [regionId, setRegionId] = useState<number | null>(null) | ||
const [sendWelcomeMail, setSendWelcomeMail] = useState(true) | ||
const project = useContext(ProjectConfigContext).projectId | ||
const rolesWithRegion = [Role.RegionManager, Role.RegionAdmin] | ||
|
||
const [createAdministrator, { loading }] = useCreateAdministratorMutation({ | ||
onError: error => { | ||
console.error(error) | ||
appToaster?.show({ intent: 'danger', message: 'Fehler: ' + getMessageFromApolloError(error) }) | ||
}, | ||
onCompleted: () => { | ||
appToaster?.show({ intent: 'success', message: 'Benutzer erfolgreich erstellt.' }) | ||
onClose() | ||
onSuccess() | ||
// Reset State | ||
setEmail('') | ||
setRole(null) | ||
setRegionId(null) | ||
setSendWelcomeMail(true) | ||
}, | ||
}) | ||
|
||
const getRegionId = () => { | ||
if (regionIdOverride !== null) { | ||
return regionIdOverride | ||
} else { | ||
return role !== null && rolesWithRegion.includes(role) ? regionId : null | ||
} | ||
} | ||
|
||
return ( | ||
<Dialog title='Benutzer hinzufügen' isOpen={isOpen} onClose={onClose}> | ||
<form | ||
onSubmit={e => { | ||
e.preventDefault() | ||
createAdministrator({ | ||
variables: { | ||
project, | ||
email, | ||
role: role as Role, | ||
regionId: getRegionId(), | ||
sendWelcomeMail, | ||
}, | ||
}) | ||
}}> | ||
<div className={Classes.DIALOG_BODY}> | ||
<FormGroup label='Email-Adresse'> | ||
<InputGroup | ||
value={email} | ||
required | ||
onChange={e => setEmail(e.target.value)} | ||
type='email' | ||
placeholder='[email protected]' | ||
/> | ||
</FormGroup> | ||
<FormGroup | ||
label={ | ||
<RoleFormGroupLabel> | ||
Rolle <RoleHelpButton /> | ||
</RoleFormGroupLabel> | ||
}> | ||
<RoleSelector role={role} onChange={setRole} hideProjectAdmin={regionIdOverride !== null} /> | ||
</FormGroup> | ||
{regionIdOverride !== null || role === null || !rolesWithRegion.includes(role) ? null : ( | ||
<FormGroup label='Region'> | ||
<RegionSelector onSelect={region => setRegionId(region.id)} selectedId={regionId} /> | ||
</FormGroup> | ||
)} | ||
<FormGroup> | ||
<Checkbox checked={sendWelcomeMail} onChange={e => setSendWelcomeMail(e.currentTarget.checked)}> | ||
<b>Sende eine Willkommens-Email.</b> | ||
<br /> | ||
Diese Email enthält einen Link, mit dem das Passwort des Accounts gesetzt werden kann. Der Link ist 24 | ||
Stunden gültig. | ||
</Checkbox> | ||
</FormGroup> | ||
</div> | ||
<div className={Classes.DIALOG_FOOTER}> | ||
<div className={Classes.DIALOG_FOOTER_ACTIONS}> | ||
<Button type='submit' intent='success' text='Benutzer hinzufügen' icon='add' loading={loading} /> | ||
</div> | ||
</div> | ||
</form> | ||
</Dialog> | ||
) | ||
} | ||
|
||
export default CreateUserDialog |
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,42 @@ | ||
import { Button, H4 } from '@blueprintjs/core' | ||
import { Popover2 } from '@blueprintjs/popover2' | ||
import { Role } from '../../generated/graphql' | ||
import { roleToText } from './UsersTable' | ||
|
||
const RoleHelpButton = () => { | ||
return ( | ||
<Popover2 | ||
content={ | ||
<div style={{ padding: '10px' }}> | ||
<H4 style={{ textAlign: 'center' }}>Welche Rollen haben welche Berechtigungen?</H4> | ||
<ul> | ||
<li> | ||
<b>{roleToText(Role.ProjectAdmin)}:</b> | ||
<ul> | ||
<li>Kann verwaltende Benutzer in allen Regionen verwalten.</li> | ||
</ul> | ||
</li> | ||
<li> | ||
<b>{roleToText(Role.RegionAdmin)}:</b> | ||
<ul> | ||
<li>Kann verwaltende Benutzer in seiner Region verwalten.</li> | ||
<li>Kann digitale Karten in seiner Region erstellen.</li> | ||
<li>Kann Anträge in seiner Region verwalten.</li> | ||
</ul> | ||
</li> | ||
<li> | ||
<b>{roleToText(Role.RegionManager)}:</b> | ||
<ul> | ||
<li>Kann digitale Karten in seiner Region erstellen.</li> | ||
<li>Kann Anträge in seiner Region verwalten.</li> | ||
</ul> | ||
</li> | ||
</ul> | ||
</div> | ||
}> | ||
<Button icon='help' minimal /> | ||
</Popover2> | ||
) | ||
} | ||
|
||
export default RoleHelpButton |
Oops, something went wrong.