1
1
import { Accordion , Button , Loader } from '@mantine/core' ;
2
2
import { ContextModalProps , useModals } from '@mantine/modals' ;
3
3
import { showNotification } from '@mantine/notifications' ;
4
- import { useState , useRef } from 'preact/hooks' ;
4
+ import { useState , useRef , useEffect } from 'preact/hooks' ;
5
5
import { DocDiffViewer } from '@/components/DocDiffViewer/DocDiffViewer.js' ;
6
6
import { Text } from '@/components/Text/Text.js' ;
7
7
import { dbPublishDoc , dbScheduleDoc } from '@/db/docs.js' ;
@@ -44,6 +44,19 @@ export function PublishDocModal(
44
44
const modals = useModals ( ) ;
45
45
const modalTheme = useModalTheme ( ) ;
46
46
47
+ const [ currentDateTime , setCurrentDateTime ] = useState ( '' ) ;
48
+
49
+ // This will update the current date and time every time the component renders.
50
+ useEffect ( ( ) => {
51
+ const now = new Date ( ) ;
52
+ const localISOTime = new Date (
53
+ now . getTime ( ) - now . getTimezoneOffset ( ) * 60000
54
+ )
55
+ . toISOString ( )
56
+ . slice ( 0 , 16 ) ; // Format to "YYYY-MM-DDTHH:mm"
57
+ setCurrentDateTime ( localISOTime ) ;
58
+ } , [ ] ) ;
59
+
47
60
const buttonLabel = publishType === 'scheduled' ? 'Schedule' : 'Publish' ;
48
61
49
62
async function publish ( ) {
@@ -70,8 +83,18 @@ export function PublishDocModal(
70
83
71
84
async function schedule ( ) {
72
85
try {
73
- setLoading ( true ) ;
74
86
const millis = Math . floor ( new Date ( scheduledDate ) . getTime ( ) ) ;
87
+ const now = Math . floor ( new Date ( ) . getTime ( ) ) ;
88
+ if ( millis <= now ) {
89
+ showNotification ( {
90
+ title : 'Schedule failed' ,
91
+ message : `Failed to schedule ${ props . docId } . Please choose a date/time in the future when using the scheduled publishing.` ,
92
+ color : 'red' ,
93
+ autoClose : false ,
94
+ } ) ;
95
+ return ;
96
+ }
97
+ setLoading ( true ) ;
75
98
await dbScheduleDoc ( props . docId , millis ) ;
76
99
setLoading ( false ) ;
77
100
showNotification ( {
@@ -182,6 +205,7 @@ export function PublishDocModal(
182
205
ref = { dateTimeRef }
183
206
type = "datetime-local"
184
207
disabled = { publishType !== 'scheduled' }
208
+ min = { currentDateTime }
185
209
value = { scheduledDate }
186
210
onChange = { ( e : Event ) => {
187
211
const target = e . target as HTMLInputElement ;
0 commit comments