Skip to content

Commit 86d95c4

Browse files
fix: Connect time input to cron expression in auto-import scheduling (#1096)
2 parents e277d1f + b38a959 commit 86d95c4

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

apps/widget/src/components/widget/Phases/AutoImport/AutoImportPhase3/AutoImportPhase3.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ interface IAutoImportPhase3Props {
2121
}
2222

2323
const getDefaultValuesForFrequency = (frequency: AUTOIMPORTSCHEDULERFREQUENCY): Partial<RecurrenceFormData> => {
24+
const now = new Date();
25+
const defaultTime = `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
26+
2427
const baseDefaults = {
2528
recurrenceType: frequency,
2629
endsNever: true,
2730
endsOn: undefined,
31+
time: defaultTime,
2832
};
2933

3034
switch (frequency) {
@@ -208,7 +212,7 @@ export function AutoImportPhase3({ onNextClick }: IAutoImportPhase3Props) {
208212
{cronExpression && (
209213
<Stack spacing="xs">
210214
<Text fw="bolder" color={colors.StrokeLight}>
211-
First import will run: {getFormattedFirstRunTime()}
215+
First import will run: {getFormattedFirstRunTime(formValues)}
212216
</Text>
213217
<Text fw="normal" color={colors.StrokeLight} size="sm">
214218
Then repeats: {parseCronExpression.toString(cronExpression)}

apps/widget/src/util/helpers/common.helpers.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,25 @@ export const getFirstRunTime = (): Date => {
1616
return new Date(now.getTime() + 5 * 60 * 1000); // 5 minutes from now
1717
};
1818

19-
export const getFormattedFirstRunTime = (): string => {
20-
return getFirstRunTime().toLocaleString('en-US', {
19+
export const getFormattedFirstRunTime = (formData?: RecurrenceFormData): string => {
20+
let firstRunTime: Date;
21+
22+
if (formData?.time) {
23+
const [hours, minutes] = formData.time.split(':').map(Number);
24+
const now = new Date();
25+
firstRunTime = new Date();
26+
firstRunTime.setHours(hours, minutes, 0, 0);
27+
28+
// If the time has already passed today, schedule for tomorrow
29+
if (firstRunTime <= now) {
30+
firstRunTime.setDate(firstRunTime.getDate() + 1);
31+
}
32+
} else {
33+
// Fallback to 5 minutes from now
34+
firstRunTime = getFirstRunTime();
35+
}
36+
37+
return firstRunTime.toLocaleString('en-US', {
2138
year: 'numeric',
2239
month: 'short',
2340
day: 'numeric',
@@ -222,9 +239,19 @@ const calculateMonthlyPattern = (frequency: number, consecutiveMonths = 1, input
222239
};
223240

224241
export const generateCronExpression = (data: RecurrenceFormData): string => {
225-
const firstRunTime = getFirstRunTime();
226-
const minutes = firstRunTime.getMinutes();
227-
const hours = firstRunTime.getHours();
242+
let minutes = 0;
243+
let hours = 0;
244+
245+
if (data.time) {
246+
const [timeHours, timeMinutes] = data.time.split(':').map(Number);
247+
hours = timeHours;
248+
minutes = timeMinutes;
249+
} else {
250+
// Fallback to current time + 5 minutes if no time specified
251+
const firstRunTime = getFirstRunTime();
252+
minutes = firstRunTime.getMinutes();
253+
hours = firstRunTime.getHours();
254+
}
228255

229256
const getMonthlyWeekDayIndex = (dayName?: string): number => {
230257
if (!dayName) return 0;

0 commit comments

Comments
 (0)