-
-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathteamSelect.js
More file actions
208 lines (190 loc) · 6.95 KB
/
teamSelect.js
File metadata and controls
208 lines (190 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { useState, useContext, useCallback, useMemo } from 'react';
import Select from 'react-select';
import { FormattedMessage, useIntl } from 'react-intl';
import messages from './messages';
import commonMessages from '../messages';
import { Button } from '../button';
import { StateContext } from '../../views/projectEdit';
import { PencilIcon, WasteIcon, ExternalLinkIcon } from '../svgIcons';
import { useFetchWithAbort } from '../../hooks/UseFetch';
import { useTeamsQuery } from '../../api/teams';
export const TeamSelect = () => {
const intl = useIntl();
const nullState = {
team: { name: null, teamId: null },
role: { value: null, label: null },
edit: false,
};
const { projectInfo, setProjectInfo } = useContext(StateContext);
const [teamSelect, setTeamSelect] = useState(nullState);
const [org, setOrg] = useState(null);
const [, isOrganisationsLoading, organisationsData] = useFetchWithAbort(
'organisations/?omitManagerList=true',
);
const { data: teamsData, isFetching: isTeamsLoading } = useTeamsQuery({ omitMemberList: true });
const teamRoles = useMemo(
() => [
{ value: 'MAPPER', label: 'Mapper' },
{ value: 'VALIDATOR', label: 'Validator' },
{ value: 'PROJECT_MANAGER', label: 'Project Manager' },
],
[],
);
const getLabel = (value) => {
return teamRoles.filter((r) => r.value === value)[0].label;
};
const editTeam = (teamId, roleValue) => {
const team = projectInfo.teams.filter((t) => t.teamId === teamId && t.role === roleValue)[0];
const role = teamRoles.filter((r) => team.role === r.value)[0];
setTeamSelect((t) => {
return { ...t, edit: true, team: team, role: role };
});
};
const removeTeam = (teamId, roleValue) => {
const teams = projectInfo.teams.filter((t) => !(t.teamId === teamId && t.role === roleValue));
setProjectInfo({ ...projectInfo, teams: teams });
};
const newTeam = () => {
return {
teamId: teamSelect.team.teamId,
name: teamSelect.team.name,
role: teamSelect.role.value,
};
};
const addTeam = () => {
const teams = projectInfo.teams;
teams.push(newTeam());
setProjectInfo({ ...projectInfo, teams: teams });
setTeamSelect(nullState);
};
const updateTeam = () => {
const teams = projectInfo.teams.map((t) => {
let item = t;
if (t.teamId === teamSelect.team.teamId && t.role === teamSelect.team.role) {
item = newTeam();
}
return item;
});
setProjectInfo({ ...projectInfo, teams: teams });
setTeamSelect(nullState);
};
const handleSelect = (value, field) => {
setTeamSelect((v) => {
return { ...v, [field]: value };
});
};
let teamList = teamsData?.teams;
if (org !== null) {
teamList = [
{
label: org.name,
options: teamList?.filter((t) => t.organisationId === org.organisationId),
},
{
label: 'Others',
options: teamList?.filter((t) => t.organisationId !== org.organisationId),
},
];
}
// Filter out assigned roles and display only remaining roles in options
const roleList = useCallback(() => {
const existingRolesOfSelectdTeam = projectInfo.teams.reduce(
(prev, curr) => (curr.teamId === teamSelect.team?.teamId ? [...prev, curr.role] : prev),
[],
);
return teamRoles.filter((role) => !existingRolesOfSelectdTeam.includes(role.value));
}, [projectInfo.teams, teamRoles, teamSelect]);
return (
<div className="w-80">
<div className="mb4">
{projectInfo.teams.map((t) => (
<div key={t.teamId} className="w-100 cf pa2 bg-white blue-dark mb2">
<div className="w-50 fl fw5">
<span className="pr1">{t.name}</span>
<a
className="link blue-light"
href={`/teams/${t.teamId}/membership/`}
target="_blank"
rel="noreferrer noopener"
>
<ExternalLinkIcon />
</a>
</div>
<div className="w-30 fl">{getLabel(t.role)}</div>
<div className="w-20 fl pl3 tr">
<span
className="pa2 br-100 pointer bg-grey-light"
onClick={() => editTeam(t.teamId, t.role)}
>
<PencilIcon className="h1 w1 blue-dark" />
</span>
<span
className=" ml1 pa2 br-100 pointer bg-grey-light red"
onClick={() => removeTeam(t.teamId, t.role)}
>
<WasteIcon className="h1 w1" />
</span>
</div>
</div>
))}
</div>
<Select
classNamePrefix="react-select"
isClearable={true}
getOptionLabel={(option) => option.name}
getOptionValue={(option) => option.organisationId}
placeholder={<FormattedMessage {...messages.filterByOrg} />}
options={isOrganisationsLoading ? [] : organisationsData.organisations}
value={org}
onChange={(value) => setOrg(value)}
className="mb2 z-4"
isLoading={isOrganisationsLoading}
loadingMessage={() => intl.formatMessage(commonMessages['loading'])}
/>
<div className="cf pb3 flex justify-between">
<Select
classNamePrefix="react-select"
getOptionLabel={(option) => option.name}
getOptionValue={(option) => option.teamId}
options={isTeamsLoading ? [] : teamList}
onChange={(value) => handleSelect(value, 'team')}
className="w-40 fl pr2 z-3"
value={teamSelect.team.name !== null ? teamSelect.team : null}
placeholder={<FormattedMessage {...messages.selectTeam} />}
isDisabled={teamSelect.edit}
isLoading={isTeamsLoading}
loadingMessage={() => intl.formatMessage(commonMessages['loading'])}
/>
<Select
classNamePrefix="react-select"
getOptionLabel={(option) => option.label}
getOptionValue={(option) => option.value}
options={roleList()}
onChange={(value) => handleSelect(value, 'role')}
className="w-40 fl mr2 z-3"
isDisabled={teamSelect.team.name === null ? true : false}
value={teamSelect.role.value !== null ? teamSelect.role : null}
placeholder={<FormattedMessage {...messages.selectRole} />}
/>
<Button
onClick={teamSelect.edit === false ? addTeam : updateTeam}
className="bg-blue-dark white fl mr2 f6"
disabled={teamSelect.team.name === null || teamSelect.role.value === null}
>
{teamSelect.edit === false ? (
<FormattedMessage {...messages.add} />
) : (
<FormattedMessage {...messages.update} />
)}
</Button>
<Button
onClick={() => setTeamSelect(nullState)}
className="bg-red white fl mr2 f6"
disabled={!teamSelect.edit}
>
<FormattedMessage {...messages.cancel} />
</Button>
</div>
</div>
);
};