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

Bug/WC-122: Prevent a user from being added in multiple roles on a project. #1452

Merged
merged 3 commits into from
Nov 15, 2024
Merged
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
10 changes: 7 additions & 3 deletions client/modules/datafiles/src/projects/forms/BaseProjectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,11 @@ export const BaseProjectForm: React.FC<{
]}
className="inner-form-item"
>
<UserSelect userRole="pi" maxCount={1} />
<UserSelect
userRole="pi"
maxCount={1}
existingUsers={watchedUsers}
/>
</Form.Item>
</Form.Item>
<Form.Item
Expand All @@ -292,7 +296,7 @@ export const BaseProjectForm: React.FC<{
<br />
<br />
<Form.Item name="coPis" initialValue={[]} className="inner-form-item">
<UserSelect userRole="co_pi" />
<UserSelect userRole="co_pi" existingUsers={watchedUsers} />
</Form.Item>
</Form.Item>
</div>
Expand All @@ -305,7 +309,7 @@ export const BaseProjectForm: React.FC<{
initialValue={[]}
className="inner-form-item"
>
<UserSelect userRole="team_member" />
<UserSelect userRole="team_member" existingUsers={watchedUsers} />
</Form.Item>
</Form.Item>

Expand Down
22 changes: 19 additions & 3 deletions client/modules/datafiles/src/projects/forms/CreateProjectForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button, Form, Input } from 'antd';
import React, { useEffect } from 'react';
import React, { useEffect, useMemo } from 'react';

import { UserSelect, GuestMembersInput } from './_fields';
import { TProjectUser } from './_fields/UserSelect';
Expand Down Expand Up @@ -34,6 +34,22 @@ export const BaseProjectCreateForm: React.FC<{
]);
}, [form, user]);

const watchedPi = Form.useWatch(['pi'], form);
const watchedCoPis = Form.useWatch(['coPis'], form);
const watchedMembers = Form.useWatch(['teamMembers'], form);
const watchedGuestMembers = Form.useWatch(['guestMembers'], form);
const watchedUsers = useMemo(
() => [
...(watchedPi ?? []),
...(watchedCoPis ?? []),
...(watchedMembers ?? []),
...(watchedGuestMembers?.filter(
(f: TProjectUser) => !!f && f.fname && f.lname && f.email && f.inst
) ?? []),
],
[watchedPi, watchedCoPis, watchedMembers, watchedGuestMembers]
);

if (!user) return null;
return (
<Form
Expand Down Expand Up @@ -94,7 +110,7 @@ export const BaseProjectCreateForm: React.FC<{
<br />
<br />
<Form.Item name="coPis" initialValue={[]} className="inner-form-item">
<UserSelect userRole="co_pi" />
<UserSelect userRole="co_pi" existingUsers={watchedUsers} />
</Form.Item>
</Form.Item>
</div>
Expand All @@ -106,7 +122,7 @@ export const BaseProjectCreateForm: React.FC<{
initialValue={[]}
className="inner-form-item"
>
<UserSelect userRole="team_member" />
<UserSelect userRole="team_member" existingUsers={watchedUsers} />
</Form.Item>
</Form.Item>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const AuthorSelect: React.FC<{
(u) =>
(u.email || '') === (a.email || '') &&
u.fname === a.fname &&
u.lname === a.lname
u.lname === a.lname &&
(u.role || '') === (a.role || '')
)
)
.filter((u) => !!u);
Expand All @@ -28,7 +29,8 @@ export const AuthorSelect: React.FC<{
(a) =>
(u.email || '') === (a.email || '') &&
u.fname === a.fname &&
u.lname === a.lname
u.lname === a.lname &&
(u.role || '') === (a.role || '')
)
)
.filter((u) => !!u);
Expand Down Expand Up @@ -60,7 +62,8 @@ export const AuthorSelect: React.FC<{
(v) =>
(user?.email || '') === (v.email || '') &&
user?.fname === v.fname &&
user?.lname === v.lname
user?.lname === v.lname &&
(user?.role || '') === (v?.role || '')
)
)
.map((v) => JSON.stringify(v) ?? [])}
Expand Down
19 changes: 13 additions & 6 deletions client/modules/datafiles/src/projects/forms/_fields/UserSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ export const UserSelect: React.FC<{
userRole?: string;
maxCount?: number;
disabled?: boolean;
}> = ({ value, onChange, id, userRole, maxCount, disabled }) => {
existingUsers?: TProjectUser[];
}> = ({ value, onChange, id, userRole, maxCount, disabled, existingUsers }) => {
const existingUsernames = useMemo(
() => (existingUsers ?? []).map((u) => u.username),
[existingUsers]
);
const initialOptions: SelectProps['options'] = useMemo(
() =>
value?.map((u) => ({
Expand Down Expand Up @@ -46,15 +51,17 @@ export const UserSelect: React.FC<{
signal: controller.signal,
})
.then((resp) =>
resp.data.result.map((u) => ({
label: `${u.fname} ${u.lname} (${u.email})`,
value: JSON.stringify({ ...u, role: userRole }),
}))
resp.data.result
.filter((u) => !existingUsernames.includes(u.username))
.map((u) => ({
label: `${u.fname} ${u.lname} (${u.email})`,
value: JSON.stringify({ ...u, role: userRole }),
}))
)
.then((opts) => setData(opts))
.catch((_) => setData([]));
return () => controller.abort();
}, [debouncedSearchTerm, setData, userRole]);
}, [debouncedSearchTerm, setData, userRole, existingUsernames]);

const changeCallback = (newValue: string[]) => {
onChange && onChange(newValue.map((v) => JSON.parse(v)));
Expand Down
Loading