Skip to content

Commit

Permalink
feat: [SIG-582]: code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SagarRajput-7 committed May 19, 2024
1 parent 67fb1d4 commit e168238
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
4 changes: 2 additions & 2 deletions frontend/src/api/plannedDowntime/getAllDowntimeSchedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { useQuery, UseQueryResult } from 'react-query';
export type Recurrence = {
startTime?: string | null;
endTime?: string | null;
duration?: string | null;
repeatType: string | Option | null;
duration?: number | string | null;
repeatType?: string | Option | null;
repeatOn?: string[] | null;
};

Expand Down
13 changes: 12 additions & 1 deletion frontend/src/container/PlannedDowntime/PlannedDowntimeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { AlertRuleTags } from './PlannedDowntimeList';
import {
createEditDowntimeSchedule,
getAlertOptionsFromIds,
getDurationInfo,
recurrenceOptions,
} from './PlannedDowntimeutils';

Expand Down Expand Up @@ -235,7 +236,13 @@ export function PlannedDowntimeForm(
: {
repeatType: recurrenceOptions.doesNotRepeat,
},
recurrence: initialValues.schedule?.recurrence,
recurrence: {
...initialValues.schedule?.recurrence,
duration: getDurationInfo(
initialValues.schedule?.recurrence?.duration as string,
)?.value,
},
timezone: initialValues.schedule?.timezone as string,
};
return formData;
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -326,6 +333,10 @@ export function PlannedDowntimeForm(
addonAfter={
<Select
defaultValue="m"
value={
getDurationInfo(initialValues.schedule?.recurrence?.duration as string)
?.unit
}
onChange={(value): void => setDurationUnit(value)}
>
<Select.Option value="m">Mins</Select.Option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export function CustomCollapseList(
<HeaderComponent
duration={
schedule?.recurrence?.duration
? schedule?.recurrence?.duration
? (schedule?.recurrence?.duration as string)
: getDuration(schedule?.startTime, schedule?.endTime)
}
name={defaultTo(name, '')}
Expand Down
26 changes: 26 additions & 0 deletions frontend/src/container/PlannedDowntime/PlannedDowntimeutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,29 @@ export const recurrenceOptions = {
weekly: { label: 'Weekly', value: 'weekly' },
monthly: { label: 'Monthly', value: 'monthly' },
};

interface DurationInfo {
value: number;
unit: string;
}

export function getDurationInfo(
durationString: string | undefined | null,
): DurationInfo | null {
if (!durationString) {
return null;
}
// Regular expression to extract value and unit from the duration string
const durationRegex = /(\d+)([hms])/;
// Match the value and unit parts in the duration string
const match = durationString.match(durationRegex);
if (match && match.length >= 3) {
// Extract value and unit from the match
const value = parseInt(match[1], 10);
const unit = match[2];
// Return duration info object
return { value, unit };
}
// If no value or unit part found, return null
return null;
}

0 comments on commit e168238

Please sign in to comment.