-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathAddProject.jsx
More file actions
352 lines (322 loc) · 12.3 KB
/
AddProject.jsx
File metadata and controls
352 lines (322 loc) · 12.3 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import React, { useState } from 'react';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
import { connect } from 'react-redux';
import '../../Header/DarkMode.css';
import { addNewWBS } from './../../../actions/wbs';
import { postNewProject } from './../../../actions/projects';
import { findUserProfiles, assignProject } from './../../../actions/projectMembers';
const AddProject = (props) => {
const [modal, setModal] = useState(false);
const [newName, setNewName] = useState('');
const [newCategory, setNewCategory] = useState('Unspecified');
const [showAddButton, setShowAddButton] = useState(false);
const [wbsName, setWbsName] = useState('');
const [wbsList, setWbsList] = useState([]);
const [memberName, setMemberName] = useState('');
const [membersList, setMembersList] = useState([]);
const [showFoundUserList, setShowFoundUserList] = useState(false);
const [loading, setLoading] = useState(false);
const [successMessage, setSuccessMessage] = useState('');
const [lastTimeoutId, setLastTimeoutId] = useState(null);
const { darkMode } = props.state.theme;
const canAssignProjectToUsers = props.hasPermission('assignProjectToUsers');
const canPostWBS = props.hasPermission('postWbs');
const resetForm = () => {
setNewName('');
setNewCategory('Unspecified');
setWbsList([]);
setMembersList([]);
setShowAddButton(false);
setLoading(false);
setWbsName('');
setMemberName('');
setShowFoundUserList(false);
};
const toggleModal = () => {
if (!loading) {
setModal(!modal);
if (!modal) {
resetForm();
}
}
};
const closeModalAndShowNotification = (message) => {
setModal(false);
setSuccessMessage(message);
resetForm();
setLoading(false);
setTimeout(() => {
setSuccessMessage('');
}, 2000);
};
const changeNewName = (name) => {
setNewName(name);
setShowAddButton(name.length > 0);
};
const addWBS = () => {
if (wbsName && !wbsList.includes(wbsName)) {
setWbsList([...wbsList, wbsName]);
setWbsName('');
} else if (wbsList.includes(wbsName)) {
// eslint-disable-next-line no-console
console.log('This WBS already exists');
}
};
const removeWBS = (indexToRemove) => {
setWbsList(wbsList.filter((_, index) => index !== indexToRemove));
};
const addMember = (user) => {
const newMember = {
_id: user._id,
firstName: user.firstName,
lastName: user.lastName
};
const isDuplicate = membersList.some(member => member._id === newMember._id);
if (!isDuplicate) {
setMembersList([...membersList, newMember]);
} else {
// eslint-disable-next-line no-console
console.log('This member is already in the list');
}
setMemberName('');
setShowFoundUserList(false);
};
const removeMember = (indexToRemove) => {
setMembersList(membersList.filter((_, index) => index !== indexToRemove));
};
const handleInputChange = event => {
const currentValue = event.target.value;
setMemberName(currentValue);
if (lastTimeoutId !== null) {
clearTimeout(lastTimeoutId);
}
const timeoutId = setTimeout(() => {
if (currentValue.trim()) {
props.findUserProfiles(currentValue);
setShowFoundUserList(true);
} else {
setShowFoundUserList(false);
}
}, 300);
setLastTimeoutId(timeoutId);
};
const handleAddProject = async () => {
if (newName && newCategory && !loading) {
setLoading(true);
try {
const projectId = await props.postNewProject(newName, newCategory);
// Add WBS
if (wbsList.length > 0) {
await Promise.all(wbsList.map(wbs => props.addNewWBS(wbs, projectId)));
}
// Assign project to members
if (membersList.length > 0) {
await Promise.all(membersList.map(member =>
props.assignProject(projectId, member._id, 'Assign', member.firstName, member.lastName)
));
}
closeModalAndShowNotification('Project added successfully!');
} catch (error) {
// eslint-disable-next-line no-console
console.error("Error adding project:", error);
setLoading(false);
closeModalAndShowNotification('Error adding project. Please try again.');
} finally {
setLoading(false);
}
}
};
return (
<div>
<button
type="button"
className="btn btn-outline-success m-2"
onClick={toggleModal}
style={{
borderColor: '#8ec233',
color: '#8ec233',
borderWidth: '1px',
marginBottom: '10px'
}}
>
<i className="fa fa-plus" aria-hidden="true"></i> Add New Project
</button>
{successMessage && (
<div
style={{
position: 'fixed',
top: '20px',
left: '50%',
transform: 'translateX(-50%)',
zIndex: 1000,
padding: '10px',
backgroundColor: successMessage.includes('Error') ? '#f44336' : '#4caf50',
color: 'white',
borderRadius: '5px',
textAlign: 'center',
width: '300px',
}}
>
{successMessage}
</div>
)}
<Modal isOpen={modal} toggle={toggleModal} className={`modal-dialog modal-lg ${darkMode ? 'text-light dark-mode' : ''}`}>
<ModalHeader
toggle={toggleModal}
className={darkMode ? 'bg-space-cadet' : ''}
cssModule={{ 'modal-title': 'w-100 text-center my-auto pl-2' }}>
Add New Project
</ModalHeader>
<ModalBody className={darkMode ? 'bg-yinmn-blue' : ''}>
<div className="form-group">
<label htmlFor="projectName" className={darkMode ? "text-light":" "}>Project Name</label>
<span className="red-asterisk">* </span>
<input
type="text"
className={`form-control ${darkMode ? 'bg-darkmode-liblack border-0 text-light' : ''}`}
id="projectName"
placeholder="Enter project name"
value={newName}
onChange={(e) => changeNewName(e.target.value)}
disabled={loading}
/>
</div>
<div className="form-group">
<label htmlFor="category" className={darkMode ? "text-light":" "}>Select Category</label>
<select
className={`form-control ${darkMode ? 'bg-darkmode-liblack border-0 text-light' : ''}`}
id="category"
value={newCategory}
onChange={(e) => setNewCategory(e.target.value)}
disabled={loading}
>
<option value="Unspecified">Unspecified</option>
<option value="Food">Food</option>
<option value="Energy">Energy</option>
<option value="Housing">Housing</option>
<option value="Education">Education</option>
<option value="Society">Society</option>
<option value="Economics">Economics</option>
<option value="Stewardship">Stewardship</option>
<option value="Other">Other</option>
</select>
</div>
{canPostWBS && (
<div className="form-group">
<label htmlFor="WBS" className={darkMode ? "text-light":" "}>Add WBS (optional)</label>
<div className="input-group">
<input
type="text"
className={`form-control ${darkMode ? 'bg-darkmode-liblack border-0 text-light' : ''}`}
id="wbsName"
placeholder="Enter WBS name"
value={wbsName}
onChange={(e) => setWbsName(e.target.value)}
disabled={loading}
/>
<div className="input-group-append">
<Button color="primary" onClick={addWBS} disabled={loading}>
Add WBS
</Button>
</div>
</div>
{wbsList.length > 0 && (
<ul className="list-group mt-2" style={{ maxHeight: '15vh', overflowY: 'auto' }}>
{wbsList.map((wbs, index) => (
<li key={index} className={`list-group-item d-flex justify-content-between align-items-center ${darkMode ? 'bg-darkmode-liblack text-light border-0' : ''}`} style={{color:"#403e3e"}}>
{wbs}
<Button color="danger" size="sm" onClick={() => removeWBS(index)} disabled={loading}>
Delete
</Button>
</li>
))}
</ul>
)}
</div>
)}
{canAssignProjectToUsers && (
<div className="form-group">
<label htmlFor="members" className={darkMode ? "text-light" : ""}>Add Members (optional)</label>
<div className="input-group">
<input
type="text"
className={`form-control ${darkMode ? 'bg-yinmn-blue border-0 text-light' : ''}`}
id="memberName"
placeholder="Search for members"
value={memberName}
onChange={handleInputChange}
disabled={loading}
/>
</div>
{showFoundUserList && props.state.projectMembers?.foundUsers?.length > 0 && (
<ul className="list-group mt-2" style={{ maxHeight: '15vh', overflowY: 'auto' }}>
{props.state.projectMembers.foundUsers.map((member) => (
<li key={member._id} className="list-group-item d-flex justify-content-between align-items-center" style={{color: darkMode ? '#fff' : '#403e3e'}}>
{props.hasPermission('getProjectMembers') ? (
<a href={`/userprofile/${member._id}`} className={darkMode ? 'text-azure' : ''} target='_blank' rel="noreferrer">
{member.firstName} {member.lastName}
</a>
) : (
<span>{member.firstName} {member.lastName}</span>
)}
<Button color="primary" size="sm" onClick={() => addMember(member)} disabled={loading}>
Add
</Button>
</li>
))}
</ul>
)}
{membersList.length > 0 && (
<ul className="list-group mt-2" style={{ maxHeight: '15vh', overflowY: 'auto' }}>
{membersList.map((member, index) => (
<li key={index} className="list-group-item d-flex justify-content-between align-items-center" style={{color: darkMode ? '#fff' : '#403e3e'}}>
{props.hasPermission('getProjectMembers') ? (
<a href={`/userprofile/${member._id}`} className={darkMode ? 'text-azure' : ''} target="_blank" rel="noreferrer">
{member.firstName} {member.lastName}
</a>
) : (
<span>{member.firstName} {member.lastName}</span>
)}
<Button color="danger" size="sm" onClick={() => removeMember(index)} disabled={loading}>
Delete
</Button>
</li>
))}
</ul>
)}
</div>
)}
</ModalBody>
<ModalFooter className={darkMode ? 'bg-yinmn-blue' : ''}>
{showAddButton && (
<Button
color="primary"
onClick={handleAddProject}
disabled={loading}
>
{loading ? (
<span>
<i className="fa fa-spinner fa-spin" /> Adding...
</span>
) : (
'Add Project'
)}
</Button>
)}
<Button color="secondary" onClick={toggleModal} disabled={loading}>
Cancel
</Button>
</ModalFooter>
</Modal>
</div>
);
};
const mapStateToProps = state => {
return { state };
};
export default connect(mapStateToProps, {
addNewWBS,
postNewProject,
findUserProfiles,
assignProject,
})(AddProject);