Update cron expression builder add daily interval in dropdown

This commit is contained in:
advplyr 2022-08-18 17:56:52 -05:00
parent dd9a072231
commit a574d06e22

View File

@ -14,7 +14,7 @@
<ui-multi-select-dropdown v-if="selectedInterval === 'custom'" v-model="selectedWeekdays" @input="updateCron" label="Weekdays to run" :items="weekdays" /> <ui-multi-select-dropdown v-if="selectedInterval === 'custom'" v-model="selectedWeekdays" @input="updateCron" label="Weekdays to run" :items="weekdays" />
<div v-if="selectedWeekdays.length && selectedInterval === 'custom'" class="flex items-center py-2"> <div v-if="(selectedWeekdays.length && selectedInterval === 'custom') || selectedInterval === 'daily'" class="flex items-center py-2">
<ui-text-input-with-label v-model="selectedHour" @input="updateCron" @blur="hourBlur" type="number" label="Hour" class="max-w-20" /> <ui-text-input-with-label v-model="selectedHour" @input="updateCron" @blur="hourBlur" type="number" label="Hour" class="max-w-20" />
<p class="text-xl px-2 mt-4">:</p> <p class="text-xl px-2 mt-4">:</p>
<ui-text-input-with-label v-model="selectedMinute" @input="updateCron" @blur="minuteBlur" type="number" label="Minute" class="max-w-20" /> <ui-text-input-with-label v-model="selectedMinute" @input="updateCron" @blur="minuteBlur" type="number" label="Minute" class="max-w-20" />
@ -71,7 +71,7 @@ export default {
return !(isNaN(this.selectedHour) || this.selectedHour === '' || this.selectedHour < 0 || this.selectedHour > 23) return !(isNaN(this.selectedHour) || this.selectedHour === '' || this.selectedHour < 0 || this.selectedHour > 23)
}, },
description() { description() {
if (this.selectedInterval !== 'custom' || !this.selectedWeekdays.length) return '' if ((this.selectedInterval !== 'custom' || !this.selectedWeekdays.length) && this.selectedInterval !== 'daily') return ''
if (!this.hourIsValid) { if (!this.hourIsValid) {
return `<span class="text-error">Invalid hour must be 0-23 | ${this.selectedHour < 0 || this.selectedHour > 23}</span>` return `<span class="text-error">Invalid hour must be 0-23 | ${this.selectedHour < 0 || this.selectedHour > 23}</span>`
@ -81,14 +81,16 @@ export default {
} }
var description = 'Run every ' var description = 'Run every '
const weekdayTexts = var weekdayTexts = ''
this.selectedWeekdays.length === 7 if (this.selectedWeekdays.length === 7 || this.selectedInterval === 'daily') {
? 'day' weekdayTexts = 'day'
: this.selectedWeekdays } else {
.map((weekday) => { weekdayTexts = this.selectedWeekdays
return this.weekdays.find((w) => w.value === weekday).text .map((weekday) => {
}) return this.weekdays.find((w) => w.value === weekday).text
.join(', ') })
.join(', ')
}
description += `<span class="font-bold text-white">${weekdayTexts}</span>` description += `<span class="font-bold text-white">${weekdayTexts}</span>`
const hourString = this.selectedHour.toString() const hourString = this.selectedHour.toString()
@ -103,28 +105,32 @@ export default {
value: 'custom' value: 'custom'
}, },
{ {
text: 'Every 15 minutes', text: 'Every day',
value: '*/15 * * * *' value: 'daily'
}, },
{ {
text: 'Every 30 minutes', text: 'Every 12 hours',
value: '*/30 * * * *' value: '0 */12 * * *'
},
{
text: 'Every hour',
value: '0 * * * *'
},
{
text: 'Every 2 hours',
value: '0 */2 * * *'
}, },
{ {
text: 'Every 6 hours', text: 'Every 6 hours',
value: '0 */6 * * *' value: '0 */6 * * *'
}, },
{ {
text: 'Every 12 hours', text: 'Every 2 hours',
value: '0 */12 * * *' value: '0 */2 * * *'
},
{
text: 'Every hour',
value: '0 * * * *'
},
{
text: 'Every 30 minutes',
value: '*/30 * * * *'
},
{
text: 'Every 15 minutes',
value: '*/15 * * * *'
} }
] ]
}, },
@ -177,7 +183,15 @@ export default {
return return
} }
this.selectedWeekdays.sort() this.selectedWeekdays.sort()
this.cronExpression = `${this.selectedMinute} ${this.selectedHour} * * ${this.selectedWeekdays.join(',')}`
const daysOfWeekPiece = this.selectedWeekdays.length === 7 ? '*' : this.selectedWeekdays.join(',')
this.cronExpression = `${this.selectedMinute} ${this.selectedHour} * * ${daysOfWeekPiece}`
} else if (this.selectedInterval === 'daily') {
if (!this.minuteIsValid || !this.hourIsValid) {
this.cronExpression = null
return
}
this.cronExpression = `${this.selectedMinute} ${this.selectedHour} * * *`
} else { } else {
this.cronExpression = this.selectedInterval this.cronExpression = this.selectedInterval
} }
@ -278,14 +292,16 @@ export default {
isCustomCron = true isCustomCron = true
} else if (pieces[2] !== '*' || pieces[3] !== '*') { } else if (pieces[2] !== '*' || pieces[3] !== '*') {
isCustomCron = true isCustomCron = true
} else if (pieces[4].split(',').some((num) => isNaN(num))) { } else if (pieces[4] !== '*' && pieces[4].split(',').some((num) => isNaN(num))) {
isCustomCron = true isCustomCron = true
} }
if (isCustomCron) { if (isCustomCron) {
this.showAdvancedView = true this.showAdvancedView = true
} else { } else {
this.selectedWeekdays = pieces[4].split(',').map((num) => Number(num)) if (pieces[4] === '*') this.selectedInterval = 'daily'
this.selectedWeekdays = pieces[4] === '*' ? [0, 1, 2, 3, 4, 5, 6] : pieces[4].split(',').map((num) => Number(num))
this.selectedHour = pieces[1] this.selectedHour = pieces[1]
this.selectedMinute = pieces[0] this.selectedMinute = pieces[0]
} }