Bug 32030: Make the ERMProviders syspref multivaluated
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / vue / components / ERM / EHoldingsLocalTitlesFormAdd.vue
1 <template>
2     <div v-if="!initialized">{{ $t("Loading") }}</div>
3     <div v-else id="titles_add">
4         <h2 v-if="title.title_id">
5             {{ $t("Edit title .id", { id: title.title_id }) }}
6         </h2>
7         <h2 v-else>{{ $t("New title") }}</h2>
8         <div>
9             <form @submit="onSubmit($event)">
10                 <fieldset class="rows">
11                     <ol>
12                         <li v-if="title.title_id">
13                             <label for="title_title_id"
14                                 >{{ $t("Title identifier") }}:</label
15                             >
16                             <span>
17                                 {{ title.title_id }}
18                             </span>
19                         </li>
20
21                         <li>
22                             <label class="required" for="title_name"
23                                 >{{ $t("Publication title") }}:</label
24                             >
25                             <input
26                                 id="title_publication_title"
27                                 v-model="title.publication_title"
28                                 :placeholder="$t('Publication title')"
29                                 required
30                             />
31                             <span class="required">{{ $t("Required") }}</span>
32                         </li>
33
34                         <li>
35                             <label for="title_print_identifier"
36                                 >{{ $t("Print-format identifier") }}:</label
37                             >
38                             <input
39                                 id="title_print_identifier"
40                                 v-model="title.print_identifier"
41                                 :placeholder="$t('Print-format identifier')"
42                             />
43                         </li>
44
45                         <li>
46                             <label for="title_online_identifier"
47                                 >{{ $t("Online-format identifier") }}:</label
48                             >
49                             <input
50                                 id="title_online_identifier"
51                                 v-model="title.online_identifier"
52                                 :placeholder="$t('Online-format identifier')"
53                             />
54                         </li>
55
56                         <li>
57                             <label for="title_date_first_issue_online"
58                                 >{{
59                                     $t(
60                                         "Date of first serial issue available online"
61                                     )
62                                 }}:</label
63                             >
64                             <input
65                                 id="title_date_first_issue_online"
66                                 v-model="title.date_first_issue_online"
67                                 :placeholder="
68                                     $t(
69                                         'Date of first serial issue available online'
70                                     )
71                                 "
72                             />
73                         </li>
74
75                         <li>
76                             <label for="title_num_first_vol_online"
77                                 >{{
78                                     $t(
79                                         "Number of first volume available online"
80                                     )
81                                 }}:</label
82                             >
83                             <input
84                                 id="title_num_first_vol_online"
85                                 v-model="title.num_first_vol_online"
86                                 :placeholder="
87                                     $t(
88                                         'Number of first volume available online'
89                                     )
90                                 "
91                             />
92                         </li>
93
94                         <li>
95                             <label for="title_num_first_issue_online"
96                                 >{{
97                                     $t(
98                                         "Number of first issue available online"
99                                     )
100                                 }}:</label
101                             >
102                             <input
103                                 id="title_num_first_issue_online"
104                                 v-model="title.num_first_issue_online"
105                                 :placeholder="
106                                     $t('Number of first issue available online')
107                                 "
108                             />
109                         </li>
110
111                         <li>
112                             <label for="title_date_last_issue_online"
113                                 >{{
114                                     $t("Date of last issue available online")
115                                 }}:</label
116                             >
117                             <input
118                                 id="title_date_last_issue_online"
119                                 v-model="title.date_last_issue_online"
120                                 :placeholder="
121                                     $t('Date of last issue available online')
122                                 "
123                             />
124                         </li>
125
126                         <li>
127                             <label for="title_num_last_vol_online"
128                                 >{{
129                                     $t(
130                                         "Number of last volume available online"
131                                     )
132                                 }}:</label
133                             >
134                             <input
135                                 id="title_num_last_vol_online"
136                                 v-model="title.num_last_vol_online"
137                                 :placeholder="
138                                     $t('Number of last volume available online')
139                                 "
140                             />
141                         </li>
142
143                         <li>
144                             <label for="title_num_last_issue_online"
145                                 >{{
146                                     $t("Number of last issue available online")
147                                 }}:</label
148                             >
149                             <input
150                                 id="title_num_last_issue_online"
151                                 v-model="title.num_last_issue_online"
152                                 :placeholder="
153                                     $t('Number of last issue available online')
154                                 "
155                             />
156                         </li>
157
158                         <li>
159                             <label for="title_title_url"
160                                 >{{ $t("Title-level URL") }}:</label
161                             >
162                             <input
163                                 id="title_title_url"
164                                 v-model="title.title_url"
165                                 :placeholder="$t('Title-level URL')"
166                             />
167                         </li>
168
169                         <li>
170                             <label for="title_first_author"
171                                 >{{ $t("First author") }}:</label
172                             >
173                             <input
174                                 id="title_first_author"
175                                 v-model="title.first_author"
176                                 :placeholder="$t('First author')"
177                             />
178                         </li>
179
180                         <li>
181                             <label for="title_embargo_info"
182                                 >{{ $t("Embargo information") }}:</label
183                             >
184                             <input
185                                 id="title_embargo_info"
186                                 v-model="title.embargo_info"
187                                 :placeholder="$t('Embargo information')"
188                             />
189                         </li>
190
191                         <li>
192                             <label for="title_coverage_depth"
193                                 >{{ $t("Coverage depth") }}:</label
194                             >
195                             <input
196                                 id="title_coverage_depth"
197                                 v-model="title.coverage_depth"
198                                 :placeholder="$t('Coverage depth')"
199                             />
200                         </li>
201
202                         <li>
203                             <label for="title_notes">{{ $t("Notes") }}:</label>
204                             <input
205                                 id="title_notes"
206                                 v-model="title.notes"
207                                 :placeholder="$t('Notes')"
208                             />
209                         </li>
210
211                         <li>
212                             <label for="title_publisher_name"
213                                 >{{ $t("Publisher name") }}:</label
214                             >
215                             <input
216                                 id="title_publisher_name"
217                                 v-model="title.publisher_name"
218                                 :placeholder="$t('Publisher name')"
219                             />
220                         </li>
221
222                         <li>
223                             <label for="title_publication_type"
224                                 >{{ $t("Publication type") }}:</label
225                             >
226                             <select
227                                 id="title_publication_type"
228                                 v-model="title.publication_type"
229                             >
230                                 <option value=""></option>
231                                 <option
232                                     v-for="type in av_title_publication_types"
233                                     :key="type.authorised_values"
234                                     :value="type.authorised_value"
235                                     :selected="
236                                         type.authorised_value ==
237                                         title.publication_type
238                                             ? true
239                                             : false
240                                     "
241                                 >
242                                     {{ type.lib }}
243                                 </option>
244                             </select>
245                         </li>
246
247                         <li>
248                             <label for="title_date_monograph_published_print"
249                                 >{{
250                                     $t(
251                                         "Date the monograph is first published in print"
252                                     )
253                                 }}:</label
254                             >
255                             <input
256                                 id="title_date_monograph_published_print"
257                                 v-model="title.date_monograph_published_print"
258                                 :placeholder="
259                                     $t(
260                                         'Date the monograph is first published in print'
261                                     )
262                                 "
263                             />
264                         </li>
265
266                         <li>
267                             <label for="title_date_monograph_published_online"
268                                 >{{
269                                     $t(
270                                         "Date the monograph is first published online"
271                                     )
272                                 }}:</label
273                             >
274                             <input
275                                 id="title_date_monograph_published_online"
276                                 v-model="title.date_monograph_published_online"
277                                 :placeholder="
278                                     $t(
279                                         'Date the monograph is first published online'
280                                     )
281                                 "
282                             />
283                         </li>
284
285                         <li>
286                             <label for="title_monograph_volume"
287                                 >{{
288                                     $t("Number of volume for monograph")
289                                 }}:</label
290                             >
291                             <input
292                                 id="title_monograph_volume"
293                                 v-model="title.monograph_volume"
294                                 :placeholder="
295                                     $t('Number of volume for monograph')
296                                 "
297                             />
298                         </li>
299
300                         <li>
301                             <label for="title_monograph_edition"
302                                 >{{ $t("Edition of the monograph") }}:</label
303                             >
304                             <input
305                                 id="title_monograph_edition"
306                                 v-model="title.monograph_edition"
307                                 :placeholder="$t('Edition of the monograph')"
308                             />
309                         </li>
310
311                         <li>
312                             <label for="title_first_editor"
313                                 >{{ $t("First editor") }}:</label
314                             >
315                             <input
316                                 id="title_first_editor"
317                                 v-model="title.first_editor"
318                                 :placeholder="$t('First editor')"
319                             />
320                         </li>
321
322                         <li>
323                             <label for="title_parent_publication_title_id"
324                                 >{{
325                                     $t(
326                                         "Title identifier of the parent publication"
327                                     )
328                                 }}:</label
329                             >
330                             <input
331                                 id="title_parent_publication_title_id"
332                                 v-model="title.parent_publication_title_id"
333                                 :placeholder="
334                                     $t(
335                                         'Title identifier of the parent publication'
336                                     )
337                                 "
338                             />
339                         </li>
340
341                         <li>
342                             <label for="title_preceeding_publication_title_id"
343                                 >{{
344                                     $t(
345                                         "Title identifier of any preceding publication title"
346                                     )
347                                 }}:</label
348                             >
349                             <input
350                                 id="title_preceeding_publication_title_id"
351                                 v-model="title.preceeding_publication_title_id"
352                                 :placeholder="
353                                     $t(
354                                         'Title identifier of any preceding publication title'
355                                     )
356                                 "
357                             />
358                         </li>
359
360                         <li>
361                             <label for="title_access_type"
362                                 >{{
363                                     // FIXME May be fee-based (P) or Open Access (F).
364                                     $t("Access type")
365                                 }}:</label
366                             >
367                             <input
368                                 id="title_access_type"
369                                 v-model="title.access_type"
370                                 :placeholder="$t('Access type')"
371                             />
372                         </li>
373
374                         <EHoldingsTitlesFormAddResources
375                             :resources="title.resources"
376                         />
377                     </ol>
378                 </fieldset>
379                 <fieldset class="action">
380                     <input type="submit" value="Submit" />
381                     <router-link
382                         to="/cgi-bin/koha/erm/eholdings/local/titles"
383                         role="button"
384                         class="cancel"
385                         >{{ $t("Cancel") }}</router-link
386                     >
387                 </fieldset>
388             </form>
389         </div>
390     </div>
391 </template>
392
393 <script>
394 import { useVendorStore } from "../../stores/vendors"
395 import { useAVStore } from "../../stores/authorised_values"
396 import EHoldingsTitlesFormAddResources from "./EHoldingsLocalTitlesFormAddResources.vue"
397 import { setMessage, setError } from "../../messages"
398 import { fetchLocalTitle } from '../../fetch'
399 import { storeToRefs } from "pinia"
400
401 export default {
402     setup() {
403         const vendorStore = useVendorStore()
404         const { vendors } = storeToRefs(vendorStore)
405
406         const AVStore = useAVStore()
407         const { av_title_publication_types } = storeToRefs(AVStore)
408         const { get_lib_from_av } = AVStore
409
410         return {
411             vendors,
412             av_title_publication_types,
413             get_lib_from_av,
414         }
415     },
416     data() {
417         return {
418             title: {
419                 title_id: null,
420                 publication_title: '',
421                 external_id: '',
422                 print_identifier: '',
423                 online_identifier: '',
424                 date_first_issue_online: '',
425                 num_first_vol_online: '',
426                 num_first_issue_online: '',
427                 date_last_issue_online: '',
428                 num_last_vol_online: '',
429                 num_last_issue_online: '',
430                 title_url: '',
431                 first_author: '',
432                 embargo_info: '',
433                 coverage_depth: '',
434                 notes: '',
435                 publisher_name: '',
436                 publication_type: '',
437                 date_monograph_published_print: '',
438                 date_monograph_published_online: '',
439                 monograph_volume: '',
440                 monograph_edition: '',
441                 first_editor: '',
442                 parent_publication_title_id: '',
443                 preceeding_publication_title_id: '',
444                 access_type: '',
445                 resources: [],
446             },
447             initialized: false,
448         }
449     },
450     beforeRouteEnter(to, from, next) {
451         next(vm => {
452             if (to.params.title_id) {
453                 vm.title = vm.getTitle(to.params.title_id)
454             } else {
455                 vm.initialized = true
456             }
457         })
458     },
459     methods: {
460         async getTitle(title_id) {
461             const title = await fetchLocalTitle(title_id)
462             this.title = title
463             this.initialized = true
464         },
465         onSubmit(e) {
466             e.preventDefault()
467
468             let title = JSON.parse(JSON.stringify(this.title)) // copy
469             let apiUrl = '/api/v1/erm/eholdings/local/titles'
470
471             let method = 'POST'
472             if (title.title_id) {
473                 method = 'PUT'
474                 apiUrl += '/' + title.title_id
475             }
476             delete title.title_id
477             delete title.biblio_id
478
479             title.resources.forEach(r => {
480                 r.started_on = r.started_on ? $date_to_rfc3339(r.started_on) : null
481                 r.ended_on = r.ended_on ? $date_to_rfc3339(r.ended_on) : null
482             })
483
484             // Cannot use the map/keepAttrs because of the reserved keywork 'package'
485             title.resources.forEach(function (e) { delete e.package; delete e.resource_id })
486
487             const options = {
488                 method: method,
489                 body: JSON.stringify(title),
490                 headers: {
491                     'Content-Type': 'application/json;charset=utf-8'
492                 },
493             }
494
495             fetch(apiUrl, options)
496                 .then(response => {
497                     if (response.status == 200) {
498                         this.$router.push("/cgi-bin/koha/erm/eholdings/local/titles")
499                         setMessage(this.$t("Title updated"))
500                     } else if (response.status == 201) {
501                         this.$router.push("/cgi-bin/koha/erm/eholdings/local/titles")
502                         setMessage(this.$t("Title created"))
503                     } else {
504                         setError(response.message || response.statusText)
505                     }
506                 }, (error) => {
507                     setError(error)
508                 }).catch(e => { console.log(e) })
509         },
510     },
511     components: { EHoldingsTitlesFormAddResources },
512     name: "EHoldingsLocalTitlesFormAdd",
513 }
514 </script>
515 <style scoped>
516 fieldset.rows label {
517     width: 25rem;
518 }
519 </style>