Bug 32939: Use APIClient to fetch local eHoldings
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / vue / components / ERM / EHoldingsLocalTitlesFormAdd.vue
1 <template>
2     <div v-if="!initialized">{{ $__("Loading") }}</div>
3     <div v-else id="titles_add">
4         <h2 v-if="title.title_id">
5             {{ $__("Edit title #%s").format(title.title_id) }}
6         </h2>
7         <h2 v-else>{{ $__("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                                 >{{ $__("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                                 >{{ $__("Publication title") }}:</label
24                             >
25                             <input
26                                 id="title_publication_title"
27                                 v-model="title.publication_title"
28                                 :placeholder="$__('Publication title')"
29                                 required
30                             />
31                             <span class="required">{{ $__("Required") }}</span>
32                         </li>
33
34                         <li>
35                             <label for="title_print_identifier"
36                                 >{{ $__("Print-format identifier") }}:</label
37                             >
38                             <input
39                                 id="title_print_identifier"
40                                 v-model="title.print_identifier"
41                                 :placeholder="$__('Print-format identifier')"
42                             />
43                         </li>
44
45                         <li>
46                             <label for="title_online_identifier"
47                                 >{{ $__("Online-format identifier") }}:</label
48                             >
49                             <input
50                                 id="title_online_identifier"
51                                 v-model="title.online_identifier"
52                                 :placeholder="$__('Online-format identifier')"
53                             />
54                         </li>
55
56                         <li>
57                             <label for="title_date_first_issue_online"
58                                 >{{
59                                     $__(
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                                     $__(
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                                     $__(
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                                     $__(
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                                     $__(
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                                     $__(
107                                         'Number of first issue available online'
108                                     )
109                                 "
110                             />
111                         </li>
112
113                         <li>
114                             <label for="title_date_last_issue_online"
115                                 >{{
116                                     $__("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                                     $__('Date of last issue available online')
124                                 "
125                             />
126                         </li>
127
128                         <li>
129                             <label for="title_num_last_vol_online"
130                                 >{{
131                                     $__(
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                                     $__(
141                                         'Number of last volume available online'
142                                     )
143                                 "
144                             />
145                         </li>
146
147                         <li>
148                             <label for="title_num_last_issue_online"
149                                 >{{
150                                     $__(
151                                         "Number of last issue available online"
152                                     )
153                                 }}:</label
154                             >
155                             <input
156                                 id="title_num_last_issue_online"
157                                 v-model="title.num_last_issue_online"
158                                 :placeholder="
159                                     $__('Number of last issue available online')
160                                 "
161                             />
162                         </li>
163
164                         <li>
165                             <label for="title_title_url"
166                                 >{{ $__("Title-level URL") }}:</label
167                             >
168                             <input
169                                 id="title_title_url"
170                                 v-model="title.title_url"
171                                 :placeholder="$__('Title-level URL')"
172                             />
173                         </li>
174
175                         <li>
176                             <label for="title_first_author"
177                                 >{{ $__("First author") }}:</label
178                             >
179                             <input
180                                 id="title_first_author"
181                                 v-model="title.first_author"
182                                 :placeholder="$__('First author')"
183                             />
184                         </li>
185
186                         <li>
187                             <label for="title_embargo_info"
188                                 >{{ $__("Embargo information") }}:</label
189                             >
190                             <input
191                                 id="title_embargo_info"
192                                 v-model="title.embargo_info"
193                                 :placeholder="$__('Embargo information')"
194                             />
195                         </li>
196
197                         <li>
198                             <label for="title_coverage_depth"
199                                 >{{ $__("Coverage depth") }}:</label
200                             >
201                             <input
202                                 id="title_coverage_depth"
203                                 v-model="title.coverage_depth"
204                                 :placeholder="$__('Coverage depth')"
205                             />
206                         </li>
207
208                         <li>
209                             <label for="title_notes">{{ $__("Notes") }}:</label>
210                             <input
211                                 id="title_notes"
212                                 v-model="title.notes"
213                                 :placeholder="$__('Notes')"
214                             />
215                         </li>
216
217                         <li>
218                             <label for="title_publisher_name"
219                                 >{{ $__("Publisher name") }}:</label
220                             >
221                             <input
222                                 id="title_publisher_name"
223                                 v-model="title.publisher_name"
224                                 :placeholder="$__('Publisher name')"
225                             />
226                         </li>
227
228                         <li>
229                             <label for="title_publication_type"
230                                 >{{ $__("Publication type") }}:</label
231                             >
232                             <v-select
233                                 id="title_publication_type"
234                                 v-model="title.publication_type"
235                                 label="lib"
236                                 :reduce="av => av.authorised_value"
237                                 :options="av_title_publication_types"
238                             />
239                         </li>
240
241                         <li>
242                             <label for="title_date_monograph_published_print"
243                                 >{{
244                                     $__(
245                                         "Date the monograph is first published in print"
246                                     )
247                                 }}:</label
248                             >
249                             <input
250                                 id="title_date_monograph_published_print"
251                                 v-model="title.date_monograph_published_print"
252                                 :placeholder="
253                                     $__(
254                                         'Date the monograph is first published in print'
255                                     )
256                                 "
257                             />
258                         </li>
259
260                         <li>
261                             <label for="title_date_monograph_published_online"
262                                 >{{
263                                     $__(
264                                         "Date the monograph is first published online"
265                                     )
266                                 }}:</label
267                             >
268                             <input
269                                 id="title_date_monograph_published_online"
270                                 v-model="title.date_monograph_published_online"
271                                 :placeholder="
272                                     $__(
273                                         'Date the monograph is first published online'
274                                     )
275                                 "
276                             />
277                         </li>
278
279                         <li>
280                             <label for="title_monograph_volume"
281                                 >{{
282                                     $__("Number of volume for monograph")
283                                 }}:</label
284                             >
285                             <input
286                                 id="title_monograph_volume"
287                                 v-model="title.monograph_volume"
288                                 :placeholder="
289                                     $__('Number of volume for monograph')
290                                 "
291                             />
292                         </li>
293
294                         <li>
295                             <label for="title_monograph_edition"
296                                 >{{ $__("Edition of the monograph") }}:</label
297                             >
298                             <input
299                                 id="title_monograph_edition"
300                                 v-model="title.monograph_edition"
301                                 :placeholder="$__('Edition of the monograph')"
302                             />
303                         </li>
304
305                         <li>
306                             <label for="title_first_editor"
307                                 >{{ $__("First editor") }}:</label
308                             >
309                             <input
310                                 id="title_first_editor"
311                                 v-model="title.first_editor"
312                                 :placeholder="$__('First editor')"
313                             />
314                         </li>
315
316                         <li>
317                             <label for="title_parent_publication_title_id"
318                                 >{{
319                                     $__(
320                                         "Title identifier of the parent publication"
321                                     )
322                                 }}:</label
323                             >
324                             <input
325                                 id="title_parent_publication_title_id"
326                                 v-model="title.parent_publication_title_id"
327                                 :placeholder="
328                                     $__(
329                                         'Title identifier of the parent publication'
330                                     )
331                                 "
332                             />
333                         </li>
334
335                         <li>
336                             <label for="title_preceeding_publication_title_id"
337                                 >{{
338                                     $__(
339                                         "Title identifier of any preceding publication title"
340                                     )
341                                 }}:</label
342                             >
343                             <input
344                                 id="title_preceeding_publication_title_id"
345                                 v-model="title.preceeding_publication_title_id"
346                                 :placeholder="
347                                     $__(
348                                         'Title identifier of any preceding publication title'
349                                     )
350                                 "
351                             />
352                         </li>
353
354                         <li>
355                             <label for="title_access_type"
356                                 >{{
357                                     // FIXME May be fee-based (P) or Open Access (F).
358                                     $__("Access type")
359                                 }}:</label
360                             >
361                             <input
362                                 id="title_access_type"
363                                 v-model="title.access_type"
364                                 :placeholder="$__('Access type')"
365                             />
366                         </li>
367
368                         <EHoldingsTitlesFormAddResources
369                             :resources="title.resources"
370                         />
371                     </ol>
372                 </fieldset>
373                 <fieldset class="action">
374                     <input type="submit" value="Submit" />
375                     <router-link
376                         to="/cgi-bin/koha/erm/eholdings/local/titles"
377                         role="button"
378                         class="cancel"
379                         >{{ $__("Cancel") }}</router-link
380                     >
381                 </fieldset>
382             </form>
383         </div>
384     </div>
385 </template>
386
387 <script>
388 import { inject } from "vue"
389 import EHoldingsTitlesFormAddResources from "./EHoldingsLocalTitlesFormAddResources.vue"
390 import { setMessage, setError, setWarning } from "../../messages"
391 import { APIClient } from "../../fetch/api-client.js"
392 import { storeToRefs } from "pinia"
393
394 export default {
395     setup() {
396         const vendorStore = inject("vendorStore")
397         const { vendors } = storeToRefs(vendorStore)
398
399         const AVStore = inject("AVStore")
400         const { av_title_publication_types } = storeToRefs(AVStore)
401         const { get_lib_from_av } = AVStore
402
403         return {
404             vendors,
405             av_title_publication_types,
406             get_lib_from_av,
407         }
408     },
409     data() {
410         return {
411             title: {
412                 title_id: null,
413                 publication_title: "",
414                 external_id: "",
415                 print_identifier: "",
416                 online_identifier: "",
417                 date_first_issue_online: "",
418                 num_first_vol_online: "",
419                 num_first_issue_online: "",
420                 date_last_issue_online: "",
421                 num_last_vol_online: "",
422                 num_last_issue_online: "",
423                 title_url: "",
424                 first_author: "",
425                 embargo_info: "",
426                 coverage_depth: "",
427                 notes: "",
428                 publisher_name: "",
429                 publication_type: "",
430                 date_monograph_published_print: "",
431                 date_monograph_published_online: "",
432                 monograph_volume: "",
433                 monograph_edition: "",
434                 first_editor: "",
435                 parent_publication_title_id: "",
436                 preceeding_publication_title_id: "",
437                 access_type: "",
438                 resources: [],
439             },
440             initialized: false,
441         }
442     },
443     beforeRouteEnter(to, from, next) {
444         next(vm => {
445             if (to.params.title_id) {
446                 vm.title = vm.getTitle(to.params.title_id)
447             } else {
448                 vm.initialized = true
449             }
450         })
451     },
452     methods: {
453         getTitle(title_id) {
454             const client = APIClient.erm
455             client.localTitles.get(title_id).then(
456                 title => {
457                     this.title = title
458                     this.initialized = true
459                 },
460                 error => {}
461             )
462         },
463         checkForm(title) {
464             let errors = []
465
466             let resources = title.resources
467             const package_ids = resources.map(al => al.package_id)
468             const duplicate_package_ids = package_ids.filter(
469                 (id, i) => package_ids.indexOf(id) !== i
470             )
471
472             if (duplicate_package_ids.length) {
473                 errors.push(this.$__("A package is used several times"))
474             }
475
476             errors.forEach(function (e) {
477                 setWarning(e)
478             })
479             return !errors.length
480         },
481         onSubmit(e) {
482             e.preventDefault()
483
484             let title = JSON.parse(JSON.stringify(this.title)) // copy
485
486             if (!this.checkForm(title)) {
487                 return false
488             }
489
490             let title_id = title.title_id
491             delete title.title_id
492             delete title.biblio_id
493
494             // Cannot use the map/keepAttrs because of the reserved keywork 'package'
495             title.resources.forEach(function (e) {
496                 delete e.package
497                 delete e.resource_id
498             })
499
500             const client = APIClient.erm
501             if (title_id) {
502                 client.localTitles.update(title, title_id).then(
503                     success => {
504                         setMessage(this.$__("Title updated"))
505                         this.$router.push(
506                             "/cgi-bin/koha/erm/eholdings/local/titles"
507                         )
508                     },
509                     error => {}
510                 )
511             } else {
512                 client.localTitles.create(title).then(
513                     success => {
514                         setMessage(this.$__("Title created"))
515                         this.$router.push(
516                             "/cgi-bin/koha/erm/eholdings/local/titles"
517                         )
518                     },
519                     error => {}
520                 )
521             }
522         },
523     },
524     components: { EHoldingsTitlesFormAddResources },
525     name: "EHoldingsLocalTitlesFormAdd",
526 }
527 </script>
528 <style scoped>
529 fieldset.rows label {
530     width: 25rem;
531 }
532 </style>