Skip to content

Commit 9639f59

Browse files
committed
feat: [v2] prevent scheduled publishing with past date/time
1 parent 2f9ab14 commit 9639f59

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

packages/root-cms/ui/components/DocActionsMenu/DocActionsMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export function DocActionsMenu(props: DocActionsMenuProps) {
238238
icon={<IconCopy size={20} />}
239239
onClick={() => copyDocModal.open()}
240240
>
241-
Copy
241+
Copy to...
242242
</Menu.Item>
243243
{sys.modifiedAt &&
244244
sys.publishedAt &&

packages/root-cms/ui/components/PublishDocModal/PublishDocModal.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Accordion, Button, Loader} from '@mantine/core';
22
import {ContextModalProps, useModals} from '@mantine/modals';
33
import {showNotification} from '@mantine/notifications';
4-
import {useState, useRef} from 'preact/hooks';
4+
import {useState, useRef, useEffect} from 'preact/hooks';
55
import {DocDiffViewer} from '@/components/DocDiffViewer/DocDiffViewer.js';
66
import {Text} from '@/components/Text/Text.js';
77
import {dbPublishDoc, dbScheduleDoc} from '@/db/docs.js';
@@ -44,6 +44,19 @@ export function PublishDocModal(
4444
const modals = useModals();
4545
const modalTheme = useModalTheme();
4646

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+
4760
const buttonLabel = publishType === 'scheduled' ? 'Schedule' : 'Publish';
4861

4962
async function publish() {
@@ -70,8 +83,18 @@ export function PublishDocModal(
7083

7184
async function schedule() {
7285
try {
73-
setLoading(true);
7486
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);
7598
await dbScheduleDoc(props.docId, millis);
7699
setLoading(false);
77100
showNotification({
@@ -182,6 +205,7 @@ export function PublishDocModal(
182205
ref={dateTimeRef}
183206
type="datetime-local"
184207
disabled={publishType !== 'scheduled'}
208+
min={currentDateTime}
185209
value={scheduledDate}
186210
onChange={(e: Event) => {
187211
const target = e.target as HTMLInputElement;

0 commit comments

Comments
 (0)