11<script setup lang="ts">
2- import { Cron } from ' croner'
32import { ref , inject , h } from ' vue'
43import { useI18n } from ' vue-i18n'
54
@@ -11,7 +10,7 @@ import {
1110 useRulesetsStore ,
1211 usePluginsStore ,
1312} from ' @/stores'
14- import { deepClone , message , sampleID } from ' @/utils'
13+ import { alert , deepClone , formatDate , isValidCron , message , sampleID } from ' @/utils'
1514
1615import Button from ' @/components/Button/index.vue'
1716
@@ -46,18 +45,15 @@ const rulesetsStore = useRulesetsStore()
4645const pluginsStore = usePluginsStore ()
4746
4847const handleCancel = inject (' cancel' ) as any
48+ const handleSubmit = inject (' submit' ) as any
4949
50- const handleSubmit = async () => {
51- try {
52- const job = new Cron (task .value .cron , () => {})
53- job .stop ()
54- } catch (error : any ) {
55- message .error (error .message )
50+ const handleSave = async () => {
51+ const { ok, reason } = isValidCron (task .value .cron )
52+ if (! ok ) {
53+ message .error (reason )
5654 return
5755 }
5856
59- loading .value = true
60-
6157 switch (task .value .type ) {
6258 case ScheduledTasksType .UpdateSubscription :
6359 task .value .subscriptions = task .value .subscriptions .filter ((id ) =>
@@ -73,13 +69,15 @@ const handleSubmit = async () => {
7369 break
7470 }
7571
72+ loading .value = true
73+
7674 try {
7775 if (props .id ) {
7876 await scheduledTasksStore .editScheduledTask (props .id , task .value )
7977 } else {
8078 await scheduledTasksStore .addScheduledTask (task .value )
8179 }
82- handleCancel ()
80+ await handleSubmit ()
8381 } catch (error : any ) {
8482 console .error (error )
8583 message .error (error )
@@ -97,6 +95,28 @@ const handleUse = (list: string[], id: string) => {
9795 }
9896}
9997
98+ const handleValidate = () => {
99+ const { ok, reason } = isValidCron (task .value .cron )
100+ if (! ok ) {
101+ message .error (reason )
102+ return
103+ }
104+ message .success (' common.success' )
105+ }
106+
107+ const handleViewNextRuns = () => {
108+ const { ok, reason, instance } = isValidCron (task .value .cron )
109+ if (! ok ) {
110+ message .error (reason )
111+ return
112+ }
113+ const list = instance ! .nextRuns (99 ).map ((v , i ) => {
114+ const index = (i + 1 ).toString ().padStart (2 , ' 0' )
115+ return index + ' - ' .repeat (14 ) + formatDate (v .getTime (), ' YYYY/MM/DD HH:mm:ss' )
116+ })
117+ alert (' Next Run Time' , list .join (' \n ' ))
118+ }
119+
100120if (props .id ) {
101121 const s = scheduledTasksStore .getScheduledTaskById (props .id )
102122 if (s ) {
@@ -121,7 +141,7 @@ const modalSlots = {
121141 type: ' primary' ,
122142 loading: loading .value ,
123143 disabled: ! task .value .name || ! task .value .cron ,
124- onClick: handleSubmit ,
144+ onClick: handleSave ,
125145 },
126146 () => t (' common.save' ),
127147 ),
@@ -141,7 +161,14 @@ defineExpose({ modalSlots })
141161 <div class =" form-item" >
142162 {{ t('scheduledtask.cron') }} *
143163 <div class =" min-w-[75%]" >
144- <Input v-model =" task.cron" :placeholder =" t('scheduledtask.cronTips')" class =" w-full" />
164+ <Input v-model =" task.cron" :placeholder =" t('scheduledtask.cronTips')" class =" w-full" >
165+ <template #suffix >
166+ <Button @click =" handleValidate" type =" primary" size =" small" >Validate</Button >
167+ <Button @click =" handleViewNextRuns" type =" primary" size =" small" class =" ml-4" >
168+ Next Run Time
169+ </Button >
170+ </template >
171+ </Input >
145172 </div >
146173 </div >
147174 <div class =" form-item" >
0 commit comments