-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPublication.js
312 lines (273 loc) · 10.7 KB
/
Publication.js
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
import React, { useState } from 'react';
import { withRouter } from 'react-router-dom';
import Select from 'react-select';
import ReactTooltip from 'react-tooltip';
import PropTypes from 'prop-types';
const Publication = ({ createMode, publication, history, refreshPublications, globalState }) => {
if (createMode) {
publication = {
owner: globalState.login,
title: '',
pageCount: null,
publicationYear: null,
attachments: [],
shareList: [],
links: []
};
}
const orginalPublication = publication;
const [editMode, setEditMode] = useState(createMode ? true : false);
const [currentPublication, setCurrentPublication] = useState(orginalPublication);
const [toAttachList, setToAttachList] = useState([]);
const [localIdList, setLocalIdList] = useState([]);
const inputClass = 'form-control' + (!editMode ? ' form-control-plaintext' : '');
const attachLink = currentPublication.links.find((l) => l.rel === "attachFile");
// console.log(currentPublication);
const validatePublication = (publication) => {
return (
publication.title.length > 0 &&
publication.pageCount > 0 &&
publication.publicationYear > 0
);
};
const saveChanges = async () => {
if (!validatePublication(currentPublication)) {
alert('Niepoprawne dane');
return;
}
const publication = { ...currentPublication };
publication.attachments = publication.attachments.map(a => { // Clear local id
if (!localIdList.includes(a.id)) return a;
else return {
userName: a.userName,
fileName: a.fileName
};
});
let selfLink = currentPublication.links.find((l) => l.rel === 'self');
const response = await fetch(selfLink.href, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(publication),
});
if (!response.ok) {
alert('Nie udało się zapisać zmian');
return;
}
// Send SSE
let url = globalState.urls.clientBase + '/api/publicationMessage';
url = new URL(url);
url.searchParams.set('publication', publication.title);
url.searchParams.set('action', 'changed');
fetch(url);
refreshPublications();
history.push('/publications');
};
const createPublication = async () => {
if (!validatePublication(currentPublication)) {
alert('Niepoprawne dane');
return;
}
const baseUrl = globalState.urls.publicationsApi;
let url = baseUrl + globalState.actions['publication.create'].href;
url = url.replace('{user}', globalState.login);
const publication = { ...currentPublication };
if (editMode) // Clear attachment id
publication.attachments = publication.attachments.map(a => ({
userName: a.userName,
fileName: a.fileName
}));
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(publication),
});
if (!response.ok) {
alert('Nie udało się utworzyć publikacji');
return;
}
// Send SSE
url = globalState.urls.clientBase + '/api/publicationMessage';
url = new URL(url);
url.searchParams.set('publication', publication.title);
url.searchParams.set('action', 'created');
fetch(url);
refreshPublications();
history.push('/publications');
};
const detachFile = async (id) => {
setCurrentPublication({
...currentPublication,
attachments: currentPublication.attachments.filter((a) => a.id !== id)
});
};
const attachFiles = async () => {
const attachments = [];
const curretnlyTakenId = [...localIdList]; // Cant use state because it updates async
for (const fileName of toAttachList) {
const id = Math.max(...currentPublication.attachments.map((a) => a.id), ...curretnlyTakenId) + 1;
curretnlyTakenId.push(id);
attachments.push({
id: id,
userName: globalState.login,
fileName: fileName,
links: [{ rel: 'detach', href: 'NotYetSaved' }]
});
}
setLocalIdList(curretnlyTakenId);
setCurrentPublication({
...currentPublication,
attachments: currentPublication.attachments.concat(attachments)
});
setToAttachList([]);
};
const prepareAttachmentsOptions = () => {
const attachedFileNames = currentPublication.attachments.map((a) => a.fileName);
let userFileNames = globalState.userFiles.map((f) => f.fileName);
userFileNames = userFileNames.filter((f) => (!attachedFileNames.includes(f)));
return userFileNames.map((f) => ({ value: f, label: f }));
};
return (
<section className="container mt-3">
<form>
<div className="form-group row">
<label className="col-sm-2 col-form-label" htmlFor="title">Tytuł</label>
<div className="col-sm-10">
<input className={inputClass} name="title" type="text" readOnly={!editMode}
value={currentPublication.title}
onChange={
(e) => { setCurrentPublication({ ...currentPublication, title: e.target.value }); }
}
/>
</div>
</div>
<div className="form-group row">
<label className="col-sm-2 col-form-label" htmlFor="pageCount">Liczba stron</label>
<div className="col-sm-10">
<input className={inputClass} readOnly={!editMode} name="pageCount" type="number" min={0}
value={currentPublication.pageCount}
onChange={
(e) => { setCurrentPublication({ ...currentPublication, pageCount: e.target.value }); }
}
/>
</div>
</div>
<div className="form-group row">
<label className="col-sm-2 col-form-label" htmlFor="publicationYear">Rok wydania</label>
<div className="col-sm-10">
<input className={inputClass} readOnly={!editMode} name="publicationYer" type="number"
min={1200} max={2200} value={currentPublication.publicationYear}
onChange={
(e) => { setCurrentPublication({ ...currentPublication, publicationYear: e.target.value }); }
}
/>
</div>
</div>
<div className="form-group">
<label className="font-weight-bolder">Załączone pliki</label>
<ul className="list-group">
{currentPublication.attachments.map((attachment) => {
const downloadLink = attachment.links.find((l) => l.rel === 'download');
const detachLink = attachment.links.find((l) => l.rel === 'detach');
if (downloadLink) { // Fill in template url
downloadLink.href = downloadLink.href
.replace('{appBaseUrl}', globalState.urls.clientBase)
.replace('{fileName}', attachment.fileName);
}
return (
<li key={attachment.id} className="list-group-item d-inline-flex align-items-center">
<span className="flex-grow-1">{attachment.fileName}</span>
<div className="buttons">
{downloadLink &&
<a href={downloadLink.href} className="btn btn-primary mr-2" type="button">
<i className="fas fa-cloud-download-alt"></i>
<span className="d-none d-sm-inline ml-1">Pobierz</span>
</a>
}
{detachLink && editMode &&
<button onClick={() => detachFile(attachment.id)} className="btn btn-warning mr-2" type="button">
<i className="fas fa-unlink"></i>
<span className="d-none d-sm-inline ml-1">Odłącz</span>
</button>
}
</div>
</li>
);
})}
</ul>
</div>
{editMode && (attachLink || createMode) &&
<div className="form-group ">
<label htmlFor="attachments">Dołącz pliki</label>
<div className="d-flex">
<Select
isMulti
value={toAttachList.map((a) => ({ value: a, label: a }))}
name="attachments"
onChange={(selectedOptions) => setToAttachList(selectedOptions.map((o) => o.value))}
options={prepareAttachmentsOptions()}
className="basic-multi-select flex-grow-1 mr-2"
classNamePrefix="select"
/>
<button onClick={attachFiles} disabled={toAttachList.length === 0} className="btn btn-success" type="button">
<i className="fas fa-link"></i>
<span className="d-none d-sm-inline ml-1">Dołącz</span>
</button>
</div>
</div>
}
<hr />
<div className="form-group d-flex justify-content-end">
{editMode && !createMode &&
<>
<span className="d-inline-block" data-tip data-for="save-button">
<button className="btn btn-success" type="button" disabled={!validatePublication(currentPublication)} onClick={saveChanges}>
Zapisz
</button>
{!validatePublication(currentPublication) &&
<ReactTooltip id='save-button' type='error'>
<span>Nieprawidłowe dane publikacji</span>
</ReactTooltip>
}
</span>
<button className="btn btn-danger ml-2" type="button"
onClick={() => { setCurrentPublication(orginalPublication); setToAttachList([]); setEditMode(false); }}>
Anuluj zmiany
</button>
</>
}
{!editMode &&
<button className="btn btn-info" type="button" onClick={() => setEditMode(true)}>
Edytuj
</button>
}
{createMode &&
<>
<span className="d-inline-block" data-tip data-for="create-button">
<button className="btn btn-success" type="button" disabled={!validatePublication(currentPublication)} onClick={createPublication}>
Utwórz pulikację
</button>
</span>
{!validatePublication(currentPublication) &&
<ReactTooltip id='create-button' type='error'>
<span>Nieprawidłowe dane publikacji</span>
</ReactTooltip>
}
</>
}
</div>
</form>
</section >
);
};
Publication.propTypes = {
createMode: PropTypes.bool,
publication: PropTypes.object,
history: PropTypes.object.isRequired,
refreshPublications: PropTypes.func.isRequired,
globalState: PropTypes.object
};
export default withRouter(Publication);