Skip to content

Commit

Permalink
Move empty settings creation to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuvonen committed Feb 1, 2024
1 parent 08a2630 commit 85b58fd
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/components/__tests__/SimpleTrainingCard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {useScheduleStore} from '@/stores/schedule';
import {Intensity, type Training} from '@/types';
import SimpleTrainingCard from '@/components/SimpleTrainingCard.vue';

const basicTraining = {
const basicTraining: Training = {
id: uuidv4(),
weekId: uuidv4(),
dayIndex: 0,
Expand All @@ -15,7 +15,7 @@ const basicTraining = {
duration: 1,
intensity: Intensity.MEDIUM,
location: 'Total wreck gym',
} as Training;
};

describe('SimpleTrainingCard', () => {
let scheduleStore: ReturnType<typeof useScheduleStore>;
Expand Down
4 changes: 2 additions & 2 deletions src/components/__tests__/TrainingCard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {useAppStateStore} from '@/stores/appState';
import {Intensity, type Training} from '@/types';
import TrainingCard from '@/components/TrainingCard.vue';

const basicTraining = {
const basicTraining: Training = {
id: uuidv4(),
weekId: uuidv4(),
dayIndex: 0,
Expand All @@ -17,7 +17,7 @@ const basicTraining = {
duration: 1,
intensity: Intensity.MEDIUM,
location: 'Total wreck gym',
} as Training;
};

describe('TrainingCard', () => {
let scheduleStore: ReturnType<typeof useScheduleStore>;
Expand Down
26 changes: 8 additions & 18 deletions src/components/__tests__/calendarExport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {v4 as uuidv4} from 'uuid';
import {useScheduleStore} from '@/stores/schedule';
import useCalendarExport from '@/hooks/calendarExport';
import {Intensity, type Week, type ScheduleSettings, type CalendarEvent} from '@/types';
import {getEmptySchedule} from '@/utils';

describe('useCalendarExport', () => {
let scheduleStore: ReturnType<typeof useScheduleStore>;
Expand Down Expand Up @@ -103,15 +104,9 @@ describe('useCalendarExport', () => {
];

const mockSettings: ScheduleSettings = {
...getEmptySchedule(),
name: 'Test Schedule',
startDate: new Date('2024-01-01'),
actualWeekNumbering: false,
availableActivities: ['running', 'swimming', 'sprint'],
defaultStartTime: {hours: 9, minutes: 0, seconds: 0},
defaultDuration: 1,
unitOfTime: 'h',
startsOnSunday: false,
darkMode: 'auto',
};

const events = await createTestComponent(mockSettings, mockWeeks);
Expand All @@ -130,7 +125,7 @@ describe('useCalendarExport', () => {
expect(events[0].productId).toBe('ajuvonen/getfit');
expect(events[0].classification).toBe('PRIVATE');
expect(events[0].calName).toBe('Test Schedule');
expect(events[0].start).toEqual([2024, 1, 1, 9, 0]);
expect(events[0].start).toEqual([2024, 1, 1, 12, 0]);

// Assertions for the second activity
expect(events[1].title).toBe('Test Training 2');
Expand All @@ -144,7 +139,7 @@ describe('useCalendarExport', () => {
expect(events[1].productId).toBe('ajuvonen/getfit');
expect(events[1].classification).toBe('PRIVATE');
expect(events[1].calName).toBe('Test Schedule');
expect(events[1].start).toEqual([2024, 1, 2, 9, 0]);
expect(events[1].start).toEqual([2024, 1, 2, 12, 0]);

// Assertions for the third activity
expect(events[2].title).toBe('Test Training 3');
Expand All @@ -158,7 +153,7 @@ describe('useCalendarExport', () => {
expect(events[2].productId).toBe('ajuvonen/getfit');
expect(events[2].classification).toBe('PRIVATE');
expect(events[2].calName).toBe('Test Schedule');
expect(events[2].start).toEqual([2024, 1, 2, 9, 30]);
expect(events[2].start).toEqual([2024, 1, 2, 12, 30]);

// Assertions for the fourth activity
expect(events[3].title).toBe('Test Training 4');
Expand All @@ -172,7 +167,7 @@ describe('useCalendarExport', () => {
expect(events[3].productId).toBe('ajuvonen/getfit');
expect(events[3].classification).toBe('PRIVATE');
expect(events[3].calName).toBe('Test Schedule');
expect(events[3].start).toEqual([2024, 1, 14, 9, 0]);
expect(events[3].start).toEqual([2024, 1, 14, 12, 0]);

// Assertions for the fifth activity
expect(events[4].title).toBe('Test Training 5');
Expand All @@ -186,7 +181,7 @@ describe('useCalendarExport', () => {
expect(events[4].productId).toBe('ajuvonen/getfit');
expect(events[4].classification).toBe('PRIVATE');
expect(events[4].calName).toBe('Test Schedule');
expect(events[4].start).toEqual([2024, 1, 14, 10, 0]);
expect(events[4].start).toEqual([2024, 1, 14, 13, 0]);
});

it('createCalendarEvents moves to next date when durations cross over', async () => {
Expand Down Expand Up @@ -222,15 +217,10 @@ describe('useCalendarExport', () => {
];

const mockSettings: ScheduleSettings = {
...getEmptySchedule(),
name: 'Test Schedule',
startDate: new Date('2024-01-01'),
actualWeekNumbering: false,
availableActivities: ['running', 'swimming', 'sprint'],
defaultStartTime: {hours: 23, minutes: 30, seconds: 0},
defaultDuration: 1,
unitOfTime: 'h',
startsOnSunday: false,
darkMode: 'auto',
};

const events = await createTestComponent(mockSettings, mockWeeks);
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/calendarExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function useCalendarExport() {
});
let accumulatedDuration = 0;
let currentDayIndex = 0;
return trainings.map(
return trainings.map<CalendarEvent>(
({activity, dayIndex, title, description, duration, intensity, location}) => {
if (dayIndex !== currentDayIndex) {
accumulatedDuration = 0;
Expand All @@ -41,7 +41,7 @@ export default function useCalendarExport() {
productId: 'ajuvonen/getfit',
classification: 'PRIVATE',
calName: settings.value.name || t('export.filename'),
} as CalendarEvent;
};
},
);
});
Expand Down
23 changes: 3 additions & 20 deletions src/stores/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,12 @@ import {useStorage} from '@vueuse/core';
import {v4 as uuidv4} from 'uuid';
import {DateTime} from 'luxon';
import type {ScheduleSettings, Training, Week} from '@/types';
import {ACTIVITIES} from '@/constants';
import {roundNearestQuarter} from '@/utils';
import {roundNearestQuarter, getEmptySchedule} from '@/utils';
import {computed, watch} from 'vue';

const getEmptySchedule = (): ScheduleSettings => ({
name: '',
startsOnSunday: false,
startDate: null,
actualWeekNumbering: false,
availableActivities: ACTIVITIES.map(({value}) => value),
defaultStartTime: {
hours: 12,
minutes: 0,
seconds: 0,
},
defaultDuration: 1,
unitOfTime: 'h',
darkMode: 'auto',
});

export const useScheduleStore = defineStore('schedule', () => {
// State refs
const settings = useStorage('getfit-settings', getEmptySchedule(), localStorage, {
const settings = useStorage<ScheduleSettings>('getfit-settings', getEmptySchedule(), localStorage, {
mergeDefaults: true,
serializer: {
read: (v: any) => v ? JSON.parse(v, (key, value) => {
Expand All @@ -38,7 +21,7 @@ export const useScheduleStore = defineStore('schedule', () => {
},
});

const weeks = useStorage('getfit-schedule', [] as Week[], localStorage, {mergeDefaults: true});
const weeks = useStorage<Week[]>('getfit-schedule', [], localStorage, {mergeDefaults: true});

// Computed getters
const getTargetWeekAndTraining = computed(() => (weekId: string, trainingId?: string) => {
Expand Down
18 changes: 17 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {unref} from 'vue';
import {helpers} from '@vuelidate/validators';
import type {ErrorObject} from '@vuelidate/core';
import {Intensity} from '@/types';
import {Intensity, type ScheduleSettings} from '@/types';
import {ACTIVITIES} from '@/constants';

export const roundNearestQuarter = function (number: number, precision: number) {
Expand All @@ -24,6 +24,22 @@ export const getIntensityColor = (intensity: Intensity) => {
}
};

export const getEmptySchedule = (): ScheduleSettings => ({
name: '',
startsOnSunday: false,
startDate: null,
actualWeekNumbering: false,
availableActivities: ACTIVITIES.map(({value}) => value),
defaultStartTime: {
hours: 12,
minutes: 0,
seconds: 0,
},
defaultDuration: 1,
unitOfTime: 'h',
darkMode: 'auto',
});

export const decimalRegex = helpers.regex(/^\d+(.(00?|25|50?|75))?$/);

export const getValidationErrors = (errors: ErrorObject[]) =>
Expand Down

0 comments on commit 85b58fd

Please sign in to comment.