Skip to content

Commit

Permalink
chore(): migrate from enum to map
Browse files Browse the repository at this point in the history
  • Loading branch information
e11sy committed Jan 25, 2025
1 parent a94df9e commit 59b5045
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
14 changes: 10 additions & 4 deletions src/components/project/settings/NotificationsAddRule.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ import FormTextFieldset from './../../forms/TextFieldset.vue';
import RadioButtonGroup, { RadioButtonGroupItem } from './../../forms/RadioButtonGroup.vue';
import UiCheckbox from './../../forms/UiCheckbox.vue';
import UiButton, { UiButtonComponent } from './../../utils/UiButton.vue';
import { ProjectNotificationsRule, ReceiveTypes, NotificationTresholdPeriodEnum } from '@/types/project-notifications';
import { ProjectNotificationsRule, ReceiveTypes, thresholdPeriodToMilliseconds, millisecondsToThresholdPeriod } from '@/types/project-notifications';
import {
ProjectNotificationsAddRulePayload,
ProjectNotificationsUpdateRulePayload
Expand Down Expand Up @@ -207,7 +207,7 @@ export default Vue.extend({
},
},
threshold: parseInt(selectedThreshold),
thresholdPeriod: NotificationTresholdPeriodEnum[selectedThresholdPeriod.id],
thresholdPeriod: thresholdPeriodToMilliseconds.get(selectedThresholdPeriod.id),
whatToReceive: ReceiveTypes.ONLY_NEW,
including: [],
excluding: [],
Expand Down Expand Up @@ -302,7 +302,11 @@ export default Vue.extend({
},
selectedThresholdPeriod: {
handler: function (value: CustomSelectOption): void {
this.$set(this.form, 'thresholdPeriod', NotificationTresholdPeriodEnum[value.id]);
console.log('currentValue', value);
if (!value) {
return;
}
this.$set(this.form, 'thresholdPeriod', thresholdPeriodToMilliseconds.get(value.id));
},
},
},
Expand All @@ -322,13 +326,15 @@ export default Vue.extend({
if (this.rule) {
const mergedRule = deepMerge(this.form, this.rule) as FormFilledByRule;
console.log('merge rule', this.rule)
/**
* Set selecteable fields to currently saved un rule
* If nothing is stored in rule, set default values
*/
this.$data.selectedThreshold = this.rule.threshold?.toString() ?? '100';
this.$data.selectedThresholdPeriod = this.seenMoreThresholdPeriod.find((option) => {
return option.id === NotificationTresholdPeriodEnum[this.rule.thresholdPeriod ?? 'hour'];
return option.id === millisecondsToThresholdPeriod.get(this.rule.thresholdPeriod ?? 3600000);
}) as CustomSelectOption;
/**
Expand Down
28 changes: 19 additions & 9 deletions src/types/project-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,34 @@ export enum ReceiveTypes {
ONLY_NEW = 'ONLY_NEW',
}

export enum NotificationTresholdPeriodEnum {
/**
* Available periods to receive notification
* This map is used for comparisons between displayed threshold period values and stored ones
*/
export const thresholdPeriodToMilliseconds = new Map<string, number>([
/**
* One minute in milliseconds
*/
'minute' = 60000,

['minute', 60000],
/**
* One hour in milliseconds
*/
'hour' = 3600000,

['hour', 3600000],
/**
* One day in milliseconds
*/
'day' = 86400000,

['day', 86400000],
/**
* One week in milliseconds
*/
'week' = 604800000,
}
['week', 604800000],
]);


/**
* Available periods to receive notification
* This map is used for comparisons between stored threshold period values and displayed ones
*/
export const millisecondsToThresholdPeriod = new Map<number, string>(
Array.from(thresholdPeriodToMilliseconds.entries()).map(([key, value]) => [value, key])
);

0 comments on commit 59b5045

Please sign in to comment.