From e6654140c7c9083632bcd6614593f413cf58416d Mon Sep 17 00:00:00 2001 From: Dominik Pschenitschni Date: Thu, 21 Jul 2022 19:16:13 +0200 Subject: [PATCH] fix: merge duplicate types --- src/models/task.ts | 13 ++++------ src/modules/parseTaskText.ts | 48 +++++++++++++----------------------- src/types/IRepeats.ts | 14 +++++++++++ 3 files changed, 36 insertions(+), 39 deletions(-) create mode 100644 src/types/IRepeats.ts diff --git a/src/models/task.ts b/src/models/task.ts index 92003d6da..88e2f141b 100644 --- a/src/models/task.ts +++ b/src/models/task.ts @@ -6,6 +6,7 @@ import LabelModel, { type ILabel } from '@/models/label' import AttachmentModel, {type IAttachment} from '@/models/attachment' import SubscriptionModel, { type ISubscription } from '@/models/subscription' import type { IList } from '@/models/list' +import type {IRepeats} from '@/types/IRepeats' import {parseDateOrNull} from '@/helpers/parseDateOrNull' import type { IBucket } from './bucket' @@ -21,11 +22,6 @@ export const TASK_REPEAT_MODES = { export type TaskRepeatMode = typeof TASK_REPEAT_MODES[keyof typeof TASK_REPEAT_MODES] -export interface RepeatAfter { - type: 'hours' | 'weeks' | 'months' | 'years' | 'days' - amount: number -} - export interface ITask extends AbstractModel { id: number title: string @@ -39,7 +35,7 @@ export interface ITask extends AbstractModel { dueDate: Date | null startDate: Date | null endDate: Date | null - repeatAfter: number | RepeatAfter + repeatAfter: number | IRepeats repeatFromCurrentDate: boolean repeatMode: TaskRepeatMode reminderDates: Date[] @@ -77,7 +73,7 @@ export default class TaskModel extends AbstractModel implements ITask { dueDate: Date | null startDate: Date | null endDate: Date | null - declare repeatAfter: number | RepeatAfter + declare repeatAfter: number | IRepeats declare repeatFromCurrentDate: boolean declare repeatMode: TaskRepeatMode reminderDates: Date[] @@ -99,6 +95,7 @@ export default class TaskModel extends AbstractModel implements ITask { updated: Date listId: IList['id'] // Meta, only used when creating a new task + declare bucketId: IBucket['id'] constructor(data: Partial) { super(data) @@ -160,7 +157,6 @@ export default class TaskModel extends AbstractModel implements ITask { this.listId = Number(this.listId) } -bucketId: number defaults() { return { @@ -198,6 +194,7 @@ bucketId: number updated: null, listId: 0, // Meta, only used when creating a new task + bucketId: 0, } } diff --git a/src/modules/parseTaskText.ts b/src/modules/parseTaskText.ts index d30975b8d..b9aa1599e 100644 --- a/src/modules/parseTaskText.ts +++ b/src/modules/parseTaskText.ts @@ -1,5 +1,6 @@ import {parseDate} from '../helpers/time/parseDate' import {PRIORITIES} from '@/models/constants/priorities' +import {REPEAT_TYPES, type IRepeats, type RepeatType} from '@/types/IRepeats' const VIKUNJA_PREFIXES: Prefixes = { label: '*', @@ -27,24 +28,9 @@ export const PREFIXES = { [PrefixMode.Todoist]: TODOIST_PREFIXES, } -const priorities = PRIORITIES - -enum RepeatType { - Hours = 'hours', - Days = 'days', - Weeks = 'weeks', - Months = 'months', - Years = 'years', -} - -interface Repeats { - type: RepeatType, - amount: number, -} - interface repeatParsedResult { textWithoutMatched: string, - repeats: Repeats | null, + repeats: IRepeats | null, } export interface ParsedTaskText { @@ -54,7 +40,7 @@ export interface ParsedTaskText { list: string | null, priority: number | null, assignees: string[], - repeats: Repeats | null, + repeats: IRepeats | null, } interface Prefixes { @@ -144,7 +130,7 @@ const getPriority = (text: string, prefix: string): number | null => { } for (const p of ps) { - for (const pi of Object.values(priorities)) { + for (const pi of Object.values(PRIORITIES)) { if (pi === parseInt(p)) { return parseInt(p) } @@ -199,55 +185,55 @@ const getRepeats = (text: string): repeatParsedResult => { default: amount = results[3] ? parseInt(results[3]) : 1 } - let type: RepeatType = RepeatType.Hours + let type: RepeatType = REPEAT_TYPES.Hours switch (results[0]) { case 'biennially': - type = RepeatType.Years + type = REPEAT_TYPES.Years amount = 2 break case 'bianually': case 'semiannually': - type = RepeatType.Months + type = REPEAT_TYPES.Months amount = 6 break case 'yearly': case 'anually': - type = RepeatType.Years + type = REPEAT_TYPES.Years break case 'daily': - type = RepeatType.Days + type = REPEAT_TYPES.Days break case 'hourly': - type = RepeatType.Hours + type = REPEAT_TYPES.Hours break case 'monthly': - type = RepeatType.Months + type = REPEAT_TYPES.Months break case 'weekly': - type = RepeatType.Weeks + type = REPEAT_TYPES.Weeks break default: switch (results[5]) { case 'hour': case 'hours': - type = RepeatType.Hours + type = REPEAT_TYPES.Hours break case 'day': case 'days': - type = RepeatType.Days + type = REPEAT_TYPES.Days break case 'week': case 'weeks': - type = RepeatType.Weeks + type = REPEAT_TYPES.Weeks break case 'month': case 'months': - type = RepeatType.Months + type = REPEAT_TYPES.Months break case 'year': case 'years': - type = RepeatType.Years + type = REPEAT_TYPES.Years break } } diff --git a/src/types/IRepeats.ts b/src/types/IRepeats.ts new file mode 100644 index 000000000..e2ab90777 --- /dev/null +++ b/src/types/IRepeats.ts @@ -0,0 +1,14 @@ +export const REPEAT_TYPES = { + Hours: 'hours', + Days: 'days', + Weeks: 'weeks', + Months: 'months', + Years: 'years', +} as const + +export type RepeatType = typeof REPEAT_TYPES[keyof typeof REPEAT_TYPES] + +export interface IRepeats { + type: RepeatType, + amount: number, +} \ No newline at end of file