-
Notifications
You must be signed in to change notification settings - Fork 0
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 #62 from cevi/feature/#24_Staff_add_users_to_Abtei…
…lung Feature/#24 staff add users to abteilung
- Loading branch information
Showing
3 changed files
with
179 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import {Button, Form, message, Select, Spin} from "antd"; | ||
import {firestore} from "../../config/firebase/firebase"; | ||
import {abteilungenCollection, abteilungenMembersCollection} from "../../config/firebase/collections"; | ||
import React, {useContext, useState} from "react"; | ||
import {AbteilungenContext} from "../navigation/NavigationMenu"; | ||
import {roles} from "../abteilung/members/MemberTable"; | ||
import {useForm} from "antd/es/form/Form"; | ||
import Modal from "antd/lib/modal/Modal"; | ||
import { AbteilungMember } from 'types/user.type'; | ||
|
||
export interface EditAbteilungMemberProps { | ||
uid: string | ||
onSuccess?: () => void | ||
} | ||
|
||
export const AddUserToAbteilung = (props: EditAbteilungMemberProps) => { | ||
|
||
const {uid, onSuccess} = props; | ||
|
||
const [form] = useForm<AbteilungMember>(); | ||
|
||
const abteilungenContext = useContext(AbteilungenContext); | ||
const abteilungen = abteilungenContext.abteilungen; | ||
const abtielungenLoading = abteilungenContext.loading; | ||
|
||
const addUserToAbteilung = async () => { | ||
const userRef = firestore().collection(abteilungenCollection).doc(form.getFieldValue('abteilung')).collection(abteilungenMembersCollection).doc(uid) | ||
const memberDoc = await userRef.get(); | ||
if (memberDoc.exists) { | ||
message.error('Dieser Benutzer ist bereits mitglied dieser Abteilung'); | ||
} else { | ||
const member: AbteilungMember = { | ||
userId: uid, | ||
role: form.getFieldValue('role'), | ||
approved: true, | ||
} | ||
await firestore().collection(abteilungenCollection).doc(form.getFieldValue('abteilung')).collection(abteilungenMembersCollection).doc(uid).set(member) | ||
.then(() => { | ||
if (onSuccess) { | ||
onSuccess(); | ||
} | ||
}) | ||
.catch(() => { | ||
message.error('Der Benutzer konnte nicht zur Abteilung hinzugefügt werden!') | ||
}) | ||
} | ||
} | ||
|
||
return <> | ||
{ | ||
abtielungenLoading ? <Spin/> : <> | ||
<Form | ||
form={form} | ||
initialValues={{ | ||
role: 'member' | ||
} as Partial<AbteilungMember>} | ||
onFinish={addUserToAbteilung}> | ||
<Form.Item | ||
label='Abteilung' | ||
name='abteilung' | ||
rules={[ | ||
{required: true}, | ||
{type: 'string', min: 1}, | ||
]} | ||
> | ||
<Select | ||
showSearch | ||
placeholder='Abteilung' | ||
optionFilterProp='children' | ||
options={[{label: 'Keine Abteilung', value: null} as any, ...abteilungen.map((a) => { | ||
return { | ||
value: a.id, | ||
label: a.name, | ||
} | ||
}).sort((a, b) => a.label.localeCompare(b.label))]} | ||
> | ||
</Select> | ||
</Form.Item> | ||
<Form.Item | ||
label='Rolle' | ||
name='role' | ||
rules={[ | ||
{required: true} | ||
]} | ||
> | ||
<Select key='role'> | ||
{ | ||
roles.map(role => <Select.Option key={role.key} | ||
value={role.key}>{role.name}</Select.Option>) | ||
} | ||
</Select> | ||
</Form.Item> | ||
<Form.Item wrapperCol={{offset: 8, span: 16}}> | ||
<Button type='primary' htmlType='submit'> | ||
Zu Abteilung hinzufügen | ||
</Button> | ||
</Form.Item> | ||
</Form> | ||
</> | ||
} | ||
</> | ||
} | ||
|
||
export const AddUserToAbteilungButton = (props: EditAbteilungMemberProps) => { | ||
const {uid} = props; | ||
|
||
const [isModalVisible, setIsModalVisible] = useState(false); | ||
|
||
return <> | ||
<Button type='primary' onClick={() => setIsModalVisible(!isModalVisible)}> | ||
Benutzer zu Abteilung hinzufügen | ||
</Button> | ||
<Modal | ||
title='Benutzer zu Abteilung hinzufügen' | ||
visible={isModalVisible} | ||
onCancel={() => setIsModalVisible(false)} | ||
footer={[ | ||
<Button key='back' onClick={() => setIsModalVisible(false)}> | ||
Abbrechen | ||
</Button>, | ||
]} | ||
> | ||
<AddUserToAbteilung uid={uid} onSuccess={() => { | ||
message.success('Der Benutzer wurde erfolgreich zur Abteilung hinzugefügt') | ||
setIsModalVisible(false) | ||
}}/> | ||
</Modal> | ||
</> | ||
|
||
} |
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 |
---|---|---|
@@ -1,29 +1,35 @@ | ||
export interface UserData { | ||
__caslSubjectType__ : 'UserData' | ||
id: string; | ||
email: string | ||
displayName: string | ||
photoURL: string | ||
given_name: string, | ||
family_name: string, | ||
nickname: string, | ||
name: string, | ||
email_verified?: boolean | ||
user_metadata?: any | ||
staff?: boolean; | ||
defaultAbteilung?: string; | ||
roles: { [abteilungId: string]: role } | ||
__caslSubjectType__: "UserData"; | ||
id: string; | ||
email: string; | ||
displayName: string; | ||
photoURL: string; | ||
given_name: string; | ||
family_name: string; | ||
nickname: string; | ||
name: string; | ||
email_verified?: boolean; | ||
user_metadata?: any; | ||
staff?: boolean; | ||
defaultAbteilung?: string; | ||
roles: { [abteilungId: string]: role }; | ||
} | ||
|
||
export interface UserDataUpdate { | ||
email: string | ||
displayName: string | ||
photoURL: string | ||
given_name: string, | ||
family_name: string, | ||
nickname: string, | ||
name: string, | ||
defaultAbteilung?: string; | ||
email: string; | ||
displayName: string; | ||
photoURL: string; | ||
given_name: string; | ||
family_name: string; | ||
nickname: string; | ||
name: string; | ||
defaultAbteilung?: string; | ||
} | ||
|
||
export type role = 'pending' | 'guest' | 'member' | 'matchef' | 'admin'; | ||
export type role = "pending" | "guest" | "member" | "matchef" | "admin"; | ||
|
||
export interface AbteilungMember { | ||
userId: string; | ||
role: role; | ||
approved: boolean; | ||
} |