Код: Выделить всё
{{ vmData.name }}
v-if="formDisabled"
class="progress is-small is-primary"
max="100"
>
15%
Start Time
[i] v-model="vmData.startTime"
class="input"
type="time"
:disabled="vmData.stopTime == null || formDisabled"
/>
Clear
Stop Time
Days of Week
Mon
Tue
Wed
Thu
Fri
Sat
Sun
0" class="notification is-warning is-light">
{{ error }}
[/i] Remove Schedule
[i][/i] Apply
import { timeToDate } from "../helper.js";
export default {
props: ["vm"],
emits: ["applied"],
watch: {
vm: function (newVal) {
this.vmData = newVal;
},
vmData: {
handler: function (newVal) {
let errors = [];
if (newVal.stopTime == null) {
errors.push("Schedule requires a stop time");
} else {
// Check if at least one day is defined
let dayCount = 0;
Object.keys(newVal.daysOfWeek).forEach(function (value) {
if (newVal.daysOfWeek[value]) {
dayCount += 1;
}
});
if (dayCount == 0) {
errors.push("Schedule requires at least 1 day set");
}
// Check if start date is before end date
if (newVal.startTime && newVal.stopTime) {
if (timeToDate(newVal.startTime) >= timeToDate(newVal.stopTime)) {
errors.push("Start time should be before stop time");
} else if (timeToDate(newVal.stopTime) - timeToDate(newVal.startTime) < 1800000) {
errors.push("Schedule should be at least 30 minutes long")
}
}
}
this.formErrors = errors;
},
deep: true,
},
},
mounted() {
// Make a deep copy of this
this.vmData = JSON.parse(JSON.stringify(this.vm));
// Work out if it's an empty schedule
if (!this.vm.stopTime) {
this.emptySchedule = true;
} else {
this.emptySchedule = false;
}
},
methods: {
clearStartTime: function () {
this.vmData.startTime = null;
},
removeSchedule: function () {
this.formDisabled = true;
let headers = new Headers({
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json",
});
fetch(`/api/schedule`, {
method: "DELETE",
headers: headers,
body: JSON.stringify(this.vmData),
}).then(() => {
this.formDisabled = false;
this.$emit("applied");
});
},
updateSchedule: function () {
this.formDisabled = true;
let headers = new Headers({
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json",
});
fetch(`/api/schedule`, {
method: "POST",
headers: headers,
body: JSON.stringify(this.vmData),
}).then(() => {
this.formDisabled = false;
this.$emit("applied");
});
},
},
data() {
return {
formDisabled: false,
vmData: null,
formErrors: [],
emptySchedule: null,
};
},
};
Подробнее здесь: https://stackoverflow.com/questions/793 ... n-back-end