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