ModalEdit.vue 11.5 KB
Newer Older
1
2
3
4
5
6
7
<template>
  <div>
    <b-modal
      ref="modalEmissionManagerEdit"
      title="Edit a schedule"
      size="lg"
    >
8
      <p v-if="loaded.shows">
9
        Schedule for: <b>{{ selectedShow.name }}</b>
10
11
      </p>

12
      <div v-if="timeslot && loaded.schedule">
13
        This timeslot starts at
14
        <b-badge variant="info">
15
16
17
          {{ prettyDateTime(timeslot.start) }}
        </b-badge>
        and ends at
18
        <b-badge variant="info">
19
20
          {{ prettyDateTime(timeslot.end) }}
        </b-badge>
21
22

        <div v-if="schedule.rrule === 1">
23
24
          <p>
            This is a single emission.
25
26
27
            <span v-if="!loaded.scheduleTimeslots">
              <br>
              <img
28
                src="../../assets/radio.gif"
29
30
31
                alt="loading schedule data"
              >
            </span>
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
            <span v-else>
              <span v-if="scheduleTimeslots.length > 1">
                But due to a previous conflict resolution there is a second timeslot, from
                <b-badge variant="info">
                  <span v-if="timeslot.start === scheduleTimeslots[0].start">{{ prettyDateTime(scheduleTimeslots[1].start) }}</span>
                  <span v-else>{{ prettyDateTime(scheduleTimeslots[0].start) }}</span>
                </b-badge>
                to
                <b-badge variant="info">
                  <span v-if="timeslot.start === scheduleTimeslots[0].start">{{ prettyDateTime(scheduleTimeslots[1].end) }}</span>
                  <span v-else>{{ prettyDateTime(scheduleTimeslots[0].end) }}</span>
                </b-badge>
                .
              </span>
              <span v-else>No other timeslots in this schedule.</span>
            </span>
          </p>
49
50
51
        </div>
        <div v-else>
          <p>This is a recurring event: <b>{{ rruleRender(schedule.rrule) }}</b>, until: {{ prettyDate(schedule.until) }}</p>
52
          <p>All timeslots of this schedule:</p>
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

          <b-table
            id="emission-table"
            striped
            :per-page="perPage"
            :current-page="currentPage"
            :fields="['start', 'end']"
            :items="scheduleTimeslots"
            :busy="!loaded.scheduleTimeslots"
            :tbody-tr-class="trClass"
          >
            <template v-slot:cell(start)="data">
              {{ prettyDateTime(data.item.start) }}
            </template>

            <template v-slot:cell(end)="data">
              {{ prettyDateTime(data.item.end) }}
            </template>
          </b-table>

          <div class="tw-w-full tw-flex tw-justify-end">
            <b-pagination
              v-model="currentPage"
              :total-rows="scheduleTimeslots.length"
              :per-page="perPage"
              aria-controls="emission-table"
            />
          </div>
81
        </div>
82
83
84
85
86
87
88
      </div>
      <div v-else>
        <img
          src="../../assets/radio.gif"
          alt="loading schedule data"
        >
      </div>
89

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
      <hr>

      <h4>Add repetition of schedule</h4>

      <b-row>
        <b-col cols="6">
          <label class="tw-leading-loose">
            When to repeat?

            <b-form-select
              v-model="repetitionRule"
              :options="repetitionOptions"
            >
            </b-form-select>
          </label>

          <b-checkbox v-model="useSameTime">
            Use same start and end time
          </b-checkbox>
        </b-col>
        <b-col cols="6" v-if="!useSameTime">
          <label class="tw-leading-loose">
            Repeat at
            <b-form-input type="time" />
          </label>
        </b-col>
      </b-row>

      <b-row
        v-if="repetitionRule === 3"
        class="tw-mt-4"
      >
        <b-col cols="6">
          <label class="tw-leading-loose">
            How many days later?

            <b-form-input
              v-model="addNoOfDays"
              type="number"
            />
          </label>

          <b-checkbox v-model="onlyBusinessDays">
            Only count business days
          </b-checkbox>
        </b-col>
      </b-row>

      <b-row class="mt-4">
        <b-col>
          <b-button
            variant="primary"
            size="sm"
            @click="createRepetitionSchedule"
          >
            Create repetition schedule
          </b-button>
        </b-col>
      </b-row>

      <template v-slot:modal-footer>
        <div
          v-if="loaded.scheduleTimeslots"
          class="tw-w-full tw-flex tw-justify-between tw-items-center"
        >
          <div class="tw-space-x-2">
            <b-button
              variant="danger"
              size="sm"
              @click="deleteFullSchedule(schedule.id)"
            >
              <span v-if="scheduleTimeslots.length === 1">Delete</span>
              <span v-else-if="schedule.rrule === 1">Delete both</span>
              <span v-else>Delete schedule and all timeslots</span>
            </b-button>

            <b-button
              v-if="schedule.rrule > 1 && scheduleTimeslots.length > 1"
              variant="danger"
              size="sm"
              @click="deleteSingleTimeslot(schedule.id, timeslot.id)"
            >
              Delete this timeslot
            </b-button>

            <b-button
              v-if="schedule.rrule > 1 && scheduleTimeslots.length > 1"
              variant="danger"
              size="sm"
              @click="deleteAllFutureTimeslots(schedule.id, timeslot.id)"
            >
              Delete all timeslots
            </b-button>
183
184
185
186
          </div>
        </div>
        <div v-else>
          <img
187
            src="../../assets/radio.gif"
188
189
            alt="loading timeslot data"
          >
190
        </div>
191
      </template>
192
    </b-modal>
193
194
195
196
197
198
199
200
201
202

    <b-modal
      ref="modalEmissionManagerDeleteTimeslots"
      title="Deleting timeslots"
      size="lg"
      centered
      hide-footer
      no-close-on-esc
      no-close-on-backdrop
    >
203
      <div>
204
        <img
205
          src="../../assets/radio.gif"
206
207
208
209
210
211
212
213
214
215
          alt="loading schedule data"
        >
        <b-progress
          :value="deletion.count"
          :max="deletion.amount"
          variant="info"
          animated
        />
      </div>
    </b-modal>
216
217
218
219
  </div>
</template>

<script>
220
import { mapGetters } from 'vuex'
221
222
import prettyDate from '../../mixins/prettyDate'
import rrules from '../../mixins/rrules'
223
224
225
226
227
228

export default {
  mixins: [ prettyDate, rrules ],

  data () {
    return {
229
230
      currentPage: 1,
      perPage: 5,
231
      timeslot: null,
232
233
234
      deletion: {
        amount: 0,
        count: 0,
235
236
237
238
239
240
241
242
243
244
245
      },
      useSameTime: true,
      onlyBusinessDays: false,
      addNoOfDays: 1,
      repetitionRule: 1,
      repetitionOptions: [
        { value: 1, text: "On the following day" },
        { value: 2, text: "On the following business day" },
        { value: 3, text: "Number of days later" },
        { value: 4, text: "Custom (WIP)" },
      ]
246
247
248
    }
  },

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  computed: {
    loaded () {
      return {
        shows: this.$store.state.shows.loaded.shows,
        schedule: this.$store.state.shows.loaded.schedule,
        scheduleTimeslots: this.$store.state.shows.loaded.scheduleTimeslots,
      }
    },

    ...mapGetters({
      selectedShow: 'shows/selectedShow',
      schedule: 'shows/schedule',
      scheduleTimeslots: 'shows/scheduleTimeslots',
    })
  },

265
  methods: {
266
267
268
269
    trClass(item, type) {
      if (!item || type !== 'row') {
        return '';
      }
270

271
272
273
274
275
      return item.id === this.timeslot.id
              ? 'table-info'
              : ''
    },

276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    createRepetitionSchedule() {
      const { onlyBusinessDays, addNoOfDays } = this.getRepetitionParameters();
      const { dstart, tstart, tend, rrule, until, fallback_id, automation_id, byweekday } = this.schedule;

      const newSchedule = {
        schedule: {
          dstart,
          tstart,
          tend,
          rrule,
          until,
          fallback_id,
          automation_id,
          byweekday,
          is_repetition: true,
          add_business_days_only: onlyBusinessDays,
          add_days_no: addNoOfDays,
        }
      };

      console.log(newSchedule);

      this.$store.dispatch('shows/submitSchedule', {
        showId: this.selectedShow.id,
        schedule: newSchedule,
        callback: (response) => {
          if (response.data.projected === undefined) {
            this.$parent.renderView(null)
          } else {
            this.$log.debug('Timeslot conflict. Switching to resolve mode.');
            this.$parent.resolve(response.data)
          }
          this.$refs.modalEmissionManagerEdit.hide()
        },
      });
    },

313
    deleteFullSchedule (id) {
314
315
316
317
      this.$store.dispatch('shows/deleteSchedule', {
        show: this.selectedShow.id,
        schedule: id,
        callback: () => {
318
          this.$refs.modalEmissionManagerEdit.hide();
319
320
          this.$parent.renderView(null)
        }
321
322
323
      })
    },

324
    deleteSingleTimeslot (scheduleId, timeslotId) {
325
326
327
328
329
330
331
332
      this.$store.dispatch('shows/deleteTimeslot', {
        show: this.selectedShow.id,
        schedule: scheduleId,
        timeslot: timeslotId,
        callback: () => {
          this.$refs.modalEmissionManagerEdit.hide()
          this.$parent.renderView(null)
        }
333
334
335
      })
    },

336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
    deleteAllFutureTimeslots (scheduleId) {
      let startDate = new Date(this.timeslot.start)
      let toDelete = []
      for (let slot of this.scheduleTimeslots) {
        if (new Date(slot.start) >= startDate) {
          toDelete.push(slot.id)
        }
      }

      if (toDelete.length === this.scheduleTimeslots.length) {
        this.$log.debug('deleting full schedule')
        this.deleteFullSchedule(scheduleId)
      }

      else {
        this.deletion.amount = toDelete.length
        this.deletion.count = 0
        this.$refs.modalEmissionManagerDeleteTimeslots.show()

        for (let i in toDelete) {
          this.$log.debug('Deleting timeslot', toDelete[i])
357
358
359
360
361
362
363
364
365
366
367
368
          this.$store.dispatch('shows/deleteTimeslot', {
            show: this.selectedShow.id,
            schedule: scheduleId,
            timeslot: toDelete[i],
            callback: () => {
              this.deletion.count++
              this.$log.debug('deleted ' + this.deletion.count + ' timeslots')
              if (this.deletion.count === this.deletion.amount) {
                this.$parent.renderView(null)
                this.$refs.modalEmissionManagerDeleteTimeslots.hide()
                this.$refs.modalEmissionManagerEdit.hide()
              }
369
370
371
372
            }
          })
        }
      }
373
374
    },

375
376
377
378
379
380
381
    loadSchedule (scheduleId) {
      this.$store.dispatch('shows/fetchSchedule', {
        show: this.selectedShow.id,
        schedule: scheduleId,
        callback: () => {
          this.loadScheduleTimeslots(scheduleId)
        }
382
383
384
      })
    },

385
386
387
388
389
390
    loadScheduleTimeslots (scheduleId) {
      this.$store.dispatch('shows/fetchTimeslots', {
        id: this.selectedShow.id,
        schedule: scheduleId,
        start: this.schedule.dstart,
        end: this.schedule.until,
391
392
      })
    },
393

394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
    getRepetitionParameters() {
      console.log(typeof this.repetitionRule);

      if (this.repetitionRule == 1) {
        return {
          onlyBusinessDays: false,
          addNoOfDays: 1,
        }
      }

      if (this.repetitionRule == 2) {
        return {
          onlyBusinessDays: true,
          addNoOfDays: 1,
        }
      }

      return {
        onlyBusinessDays: this.onlyBusinessDays,
        addNoOfDays: this.addNoOfDays,
      }
    },

417
    // initialise a new schedule and open the modal
418
    open (timeslot) {
419
420
      this.timeslot = timeslot
      this.$refs.modalEmissionManagerEdit.show()
421
      this.loadSchedule(timeslot.schedule)
422
    },
423

424
425
426
427
428
429
  }
}
</script>

<style scoped>
</style>