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