EmissionManagerModalCreate.vue 7.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<template>
  <div>
    <b-modal
      ref="modalEmissionManagerCreate"
      title="Create a new schedule"
      size="lg"
      @ok="create"
    >
      <b-row>
        <b-col cols="3">
          Creating schedule for:
        </b-col>
        <b-col cols="9">
          <b v-if="$parent.loaded.shows">
            {{ $parent.shows[$parent.currentShow].name }}
          </b>!
        </b-col>
      </b-row>

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
      <b-alert
        variant="warning"
        :show="pastEventWarning"
      >
        Past events will be ignored. Start date was set to today!<br>
        Try again or change the start date to something in the future.
      </b-alert>

      <div v-if="loaded">
        <b-row>
          <b-col cols="2">
            Start:
          </b-col>
          <b-col cols="3">
            <b-form-input
              v-model="valuePick.dstart"
              type="date"
            />
          </b-col>
          <b-col cols="3">
            <b-form-input
              v-model="valuePick.tstart"
              type="time"
            />
          </b-col>
          <b-col cols="1">
            End:
          </b-col>
          <b-col cols="3">
            <b-form-input
              v-model="valuePick.tend"
              type="time"
            />
          </b-col>
        </b-row>

        <b-row>
          <b-col cols="2">
            Type of event:
          </b-col>
          <b-col cols="6">
            <b-form-select
              v-model="valuePick.rrule"
              :options="rruleOptions"
            />
          </b-col>
          <b-col v-if="valuePick.rrule != 1">
            Until:
          </b-col>
          <b-col v-if="valuePick.rrule != 1">
            <b-form-input
              v-model="valuePick.until"
              type="date"
            />
          </b-col>
        </b-row>
      </div>
77
78
79
80
81
82
    </b-modal>
  </div>
</template>

<script>
import axios from 'axios'
83
import prettyDate from '../mixins/prettyDate'
84
85

export default {
86
87
  mixins: [ prettyDate ],

88
89
90
  data () {
    return {
      newSchedule: null,
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
      loaded: false,
      pastEventWarning: false,
      valuePick: {
        dstart: null,
        tstart: null,
        tend: null,
        until: null,
        rrule: 1
      },
      rruleOptions: [
        { value: 1, text: 'einmalig' },
        { value: 2, text: 'täglich' },
        { value: 3, text: 'werktäglich' },
        { value: 4, text: 'wöchentlich' },
        { value: 5, text: 'zweiwöchentlich' },
        { value: 6, text: 'vierwöchentlich' },
        { value: 7, text: 'gerade Kalenderwoche' },
        { value: 8, text: 'ungerade Kalenderwoche' },
        { value: 9, text: 'Jede 1. Woche im Monat' },
        { value: 10, text: 'Jede 2. Woche im Monat' },
        { value: 11, text: 'Jede 3. Woche im Monat' },
        { value: 12, text: 'Jede 4. Woche im Monat' },
        { value: 13, text: 'Jede 5. Woche im Monat' },
      ],
115
116
117
118
119
120
121
    }
  },

  methods: {
    create (event) {
      // prevent the modal from closing automatically on click
      event.preventDefault()
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
      // check for past dates; as past dates will be ignored by steering we
      // want to make the user aware - otherwise it might get confusing in
      // conflict resolution
      let now = this.apiDate(new Date())
      if (this.valuePick.dstart < now) {
        this.valuePick.dstart = now
        this.pastEventWarning = true
        return
      } else {
        this.pastEventWarning = false
      }
      // take all values that have been picked and put them into our new
      // schedule. so far we do not need any transformations.
      this.newSchedule.schedule.dstart = this.valuePick.dstart
      this.newSchedule.schedule.tstart = this.valuePick.tstart
      this.newSchedule.schedule.tend = this.valuePick.tend
      this.newSchedule.schedule.until = this.valuePick.until
      this.newSchedule.schedule.rrule = this.valuePick.rrule
      // now generate the URL and POST our new schedule
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
      let uri = process.env.VUE_APP_API_STEERING_SHOWS + this.$parent.shows[this.$parent.currentShow].id + '/schedules/'
      axios.post(uri, this.newSchedule, {
        withCredentials: true,
        headers: { 'Authorization': 'Bearer ' + this.$parent.$parent.user.access_token }
      }).then(response => {
        // we have to check if there are any conflicts with existing timeslots
        let conflicts = false
        for (let i in response.data.projected) {
          if (response.data.projected[i].collisions.length > 0) {
            conflicts = true
            break
          }
        }
        // if there are no conflicts we can set an empty solutions object
        // in our newSchedule and submit it.
        if (!conflicts) {
          this.newSchedule.solutions = {}
          this.submit()
        // otherwise we have to resolve the conflict first.
        } else {
161
162
          this.$parent.resolve(response.data)
          this.$refs.modalEmissionManagerCreate.hide()
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
        }
      }).catch(error => {
        this.$log.error(error.response.status + ' ' + error.response.statusText)
        this.$log.error(error.response)
        alert('Error: could not create schedule. See console for details.')
        // and we leave the modal open, so no call to its .hide function here
      })
    },

    submit () {
      let uri = process.env.VUE_APP_API_STEERING_SHOWS + this.$parent.shows[this.$parent.currentShow].id + '/schedules/'
      axios.post(uri, this.newSchedule, {
        withCredentials: true,
        headers: { 'Authorization': 'Bearer ' + this.$parent.$parent.user.access_token }
      }).then(response => {
        // if for some reason a new conflict arose, e.g. because in the meantime
        // someone else inserted a conflicting schedule, we have to resolve.
        // TODO: based on single event schedule
        // TODO: check for complex schedules with resolved conflicts
        if (response.data.projected === undefined) {
          this.$parent.renderView(null)
        } else {
185
          this.$parent.resolve(response.data)
186
        }
187
        this.$refs.modalEmissionManagerCreate.hide()
188
189
190
191
192
193
194
195
196
197
      }).catch(error => {
        this.$log.error(error.response.status + ' ' + error.response.statusText)
        this.$log.error(error.response)
        alert('Error: could not submit final schedule. See console for details.')
        // and we leave the modal open, so no call to its .hide function here
      })
    },

    // initialise a new schedule and open the modal
    open (start, end) {
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
      let dstart = start.format('YYYY-MM-DD')
      let tstart = start.format('HH:mm')
      let tend = end.format('HH:mm')
      let until = end.format('YYYY-MM-DD')
      let now = this.apiDate(new Date())
      if (dstart < now) {
        dstart = now
        this.pastEventWarning = true
      } else {
        this.pastEventWarning = false
      }
      this.valuePick.dstart = dstart
      this.valuePick.tstart = tstart
      this.valuePick.tend = tend
      this.valuePick.until = until
213
214
215
216
217
      this.newSchedule = {
        schedule: {
          rrule: 1,
          show: 21,
          byweekday: 0,
218
219
220
221
          dstart: dstart,
          tstart: tstart,
          tend: tend,
          until: until,
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
          is_repetition: false,
          add_days_no: 0,
          add_business_days_only: false,
          fallback_id: 0,
          automation_id: 0,
        }
      }
      this.loaded = true
      this.$refs.modalEmissionManagerCreate.show()
    },
  }
}
</script>

<style scoped>
237
238
.row {
  margin-bottom: 1em;
239
240
}
</style>