Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added revised flow #927

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class AutoImportJobsSchedular {
private readonly scheduleUserJob: ScheduleUserJob
) {}

@Cron(CRON_SCHEDULE.CRON_TIME)
@Cron(CRON_SCHEDULE.TEST_CRON_TIME)
async handleCronSchedular() {
await this.fetchAndExecuteScheduledJobs();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,56 @@ interface IAutoImportPhase3Props {
texts: typeof WIDGET_TEXTS;
}

const getDefaultValuesForFrequency = (frequency: AUTOIMPORTSCHEDULERFREQUENCY): Partial<RecurrenceFormData> => {
const baseDefaults = {
recurrenceType: frequency,
endsNever: true,
endsOn: undefined,
};

switch (frequency) {
case AUTOIMPORTSCHEDULERFREQUENCY.DAILY:
return {
...baseDefaults,
dailyType: 'every',
dailyFrequency: 1,
};
case AUTOIMPORTSCHEDULERFREQUENCY.WEEKLY:
return {
...baseDefaults,
frequency: 1,
selectedDays: [],
};
case AUTOIMPORTSCHEDULERFREQUENCY.MONTHLY:
return {
...baseDefaults,
frequency: 1,
monthlyType: 'onDay',
monthlyDayNumber: 1,
monthlyDayPosition: 'First',
monthlyDayOfWeek: 'Monday',
};
case AUTOIMPORTSCHEDULERFREQUENCY.YEARLY:
return {
...baseDefaults,
yearlyMonth: 'January',
yearlyType: 'onDay',
yearlyDayNumber: 1,
yearlyDayPosition: 'First',
yearlyDayOfWeek: 'Monday',
};
default:
return baseDefaults;
}
};

export function AutoImportPhase3({ onNextClick }: IAutoImportPhase3Props) {
const { classes } = useStyles();

const [cronExpression, setCronExpression] = useState<string>();

const { control, watch } = useForm<RecurrenceFormData>({
defaultValues: {
recurrenceType: AUTOIMPORTSCHEDULERFREQUENCY.DAILY,
frequency: 1,
selectedDays: [],
endsNever: true,
},
const { control, watch, reset } = useForm<RecurrenceFormData>({
defaultValues: getDefaultValuesForFrequency(AUTOIMPORTSCHEDULERFREQUENCY.DAILY),
});

const { updateUserJob, isUpdateUserJobLoading } = useAutoImportPhase3({
Expand All @@ -39,14 +77,18 @@ export function AutoImportPhase3({ onNextClick }: IAutoImportPhase3Props) {

const formValues = watch();

const handleTabChange = (newFrequency: AUTOIMPORTSCHEDULERFREQUENCY) => {
reset(getDefaultValuesForFrequency(newFrequency));
};

useEffect(() => {
const newCronExpression = generateCronExpression(formValues);
setCronExpression(newCronExpression);
}, [formValues, generateCronExpression]);
}, [formValues]);

const handleNextClick = () => {
updateUserJob({
cron: cronExpression,
cron: '* * * * *', // cronExpression,
endsOn: formValues.endsNever ? undefined : formValues.endsOn,
});
};
Expand All @@ -60,7 +102,14 @@ export function AutoImportPhase3({ onNextClick }: IAutoImportPhase3Props) {
name="recurrenceType"
control={control}
render={({ field }) => (
<Tabs value={field.value} onTabChange={(val) => field.onChange(val)} classNames={{ tab: classes.tab }}>
<Tabs
value={field.value}
onTabChange={(val) => {
field.onChange(val);
handleTabChange(val as AUTOIMPORTSCHEDULERFREQUENCY);
}}
classNames={{ tab: classes.tab }}
>
<Tabs.List>
{autoImportSchedulerFrequency.map((type) => (
<Tabs.Tab key={type} value={type}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,32 @@ export function SchedulerFrequency({ autoImportFrequency, control }: SchedulerFr
name="frequency"
control={control}
defaultValue={1}
render={({ field }) => (
<Group align="center">
<span style={{ color: colors.StrokeLight, fontWeight: 400 }}>Every</span>
<NumberInput {...field} min={1} max={99} style={{ width: 80 }} onChange={(val) => field.onChange(val)} />
<span style={{ color: colors.StrokeLight, fontWeight: 400 }}>week(s) once</span>
</Group>
render={({ field: frequencyField }) => (
<Controller
name="selectedDays"
control={control}
defaultValue={[]}
render={({ field: selectedDaysField }) => (
<Group align="center">
<span style={{ color: colors.StrokeLight, fontWeight: 400 }}>Every</span>
<NumberInput
{...frequencyField}
min={1}
max={5}
style={{ width: 80 }}
onChange={(val: number | '') => {
const numValue = typeof val === 'number' ? val : 1;
frequencyField.onChange(numValue);
if (numValue > 1) {
selectedDaysField.onChange([]);
}
}}
disabled={(selectedDaysField.value?.length ?? 0) > 0}
/>
<span style={{ color: colors.StrokeLight, fontWeight: 400 }}>week(s) once</span>
</Group>
)}
/>
)}
/>
<Flex gap="md" wrap="wrap">
Expand All @@ -84,19 +104,30 @@ export function SchedulerFrequency({ autoImportFrequency, control }: SchedulerFr
name="selectedDays"
control={control}
defaultValue={[]}
render={({ field }) => (
<Checkbox
style={{}}
label={weekDay.short}
checked={field.value?.includes(weekDay.full)}
onChange={(event) => {
const isChecked = event.currentTarget.checked;
const currentSelectedDays = field.value || [];
const updatedDays = isChecked
? [...currentSelectedDays, weekDay.full]
: currentSelectedDays.filter((day: string) => day !== weekDay.full);
field.onChange(updatedDays);
}}
render={({ field: selectedDaysField }) => (
<Controller
name="frequency"
control={control}
render={({ field: frequencyField }) => (
<Checkbox
style={{}}
label={weekDay.short}
checked={selectedDaysField.value?.includes(weekDay.full) ?? false}
disabled={Number(frequencyField.value) > 1}
onChange={(event) => {
const isChecked = event.currentTarget.checked;
const currentSelectedDays = selectedDaysField.value ?? [];
const updatedDays = isChecked
? [...currentSelectedDays, weekDay.full]
: currentSelectedDays.filter((day: string) => day !== weekDay.full);

selectedDaysField.onChange(updatedDays);
if (updatedDays.length > 0) {
frequencyField.onChange(1);
}
}}
/>
)}
/>
)}
/>
Expand Down
Loading
Loading