Skip to content

Commit

Permalink
Watch for date changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuvonen committed Sep 23, 2024
1 parent 58988fb commit 825c5a5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
28 changes: 27 additions & 1 deletion src/components/__tests__/LogView.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {describe, it, expect, beforeEach} from 'vitest';
import {describe, it, expect, beforeEach, vi} from 'vitest';
import {mount} from '@vue/test-utils';
import {DateTime} from 'luxon';
import LogView from '@/views/LogView.vue';
Expand Down Expand Up @@ -50,4 +50,30 @@ describe('LogView', () => {
});
expect(wrapper.html()).toMatchSnapshot();
});

it('renders empty when week changes', async () => {
activityStore.startDate = thisWeek;
activityStore.weeks.push({
veggies: ['rye', 'rice', 'wheat'],
startDate: thisWeek,
});
const wrapper = mount(LogView, {
global: {
stubs: {
CategoryStatusChart: true,
},
},
});
expect(wrapper.find('.front-page-animation').exists()).toBe(false);

vi.setSystemTime(thisWeek.plus({days: 1}).toJSDate());
await new Promise((resolve) => setTimeout(resolve, 1000));
expect(wrapper.find('.front-page-animation').exists()).toBe(false);

vi.setSystemTime(thisWeek.plus({weeks: 1}).toJSDate());
await new Promise((resolve) => setTimeout(resolve, 1000));
expect(wrapper.find('.front-page-animation').exists()).toBe(true);

vi.useRealTimers();
});
});
39 changes: 25 additions & 14 deletions src/stores/activityStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {computed} from 'vue';
import {computed, ref, watchEffect} from 'vue';
import {defineStore} from 'pinia';
import {useStorage} from '@vueuse/core';
import {useNow, useStorage} from '@vueuse/core';
import {DateTime} from 'luxon';
import {
difference,
Expand All @@ -19,6 +19,15 @@ import type {Challenge, Week} from '@/utils/types';
import {dateParser, getRandomVeggie} from '@/utils/helpers';

export const useActivityStore = defineStore('activity', () => {
const reactiveNow = useNow({interval: 1000});
const currentDate = ref(DateTime.now());
watchEffect(() => {
const now = DateTime.fromJSDate(reactiveNow.value) as DateTime<true>;
if (!currentDate.value.hasSame(now, 'day')) {
currentDate.value = now;
}
});

// State refs
const startDate = useStorage<DateTime | null>('veggies-start-date', null, localStorage, {
mergeDefaults: true,
Expand Down Expand Up @@ -46,12 +55,14 @@ export const useActivityStore = defineStore('activity', () => {

// Computed getters
const getTotalWeeks = computed(() =>
startDate.value ? Math.ceil(DateTime.now().diff(startDate.value, 'week').weeks) : 1,
startDate.value ? Math.ceil(currentDate.value.diff(startDate.value, 'week').weeks) : 1,
);

const getWeekStarts = computed(() =>
[...Array(getTotalWeeks.value)].map((_, weekIndex) =>
startDate.value ? startDate.value.plus({weeks: weekIndex}) : DateTime.now().startOf('week'),
startDate.value
? startDate.value.plus({weeks: weekIndex})
: currentDate.value.startOf('week'),
),
);

Expand Down Expand Up @@ -80,17 +91,17 @@ export const useActivityStore = defineStore('activity', () => {
);

const currentVeggies = computed({
get: () => veggiesForWeek.value(DateTime.now().startOf('week')),
get: () => veggiesForWeek.value(currentDate.value.startOf('week')),
set: (veggies: string[]) => {
const now = DateTime.now().startOf('week');
const targetWeek = weeks.value.find(({startDate}) => startDate.equals(now));
const weekStart = currentDate.value.startOf('week');
const targetWeek = weeks.value.find(({startDate}) => startDate.equals(weekStart));
if (!targetWeek) {
weeks.value.push({
startDate: now,
startDate: weekStart,
veggies,
});
challenges.value.push({
startDate: now,
startDate: weekStart,
veggie: getRandomVeggie(),
});
} else {
Expand All @@ -101,7 +112,7 @@ export const useActivityStore = defineStore('activity', () => {

const currentChallenge = computed(
() =>
challenges.value.find(({startDate}) => startDate.equals(DateTime.now().startOf('week')))
challenges.value.find(({startDate}) => startDate.equals(currentDate.value.startOf('week')))
?.veggie,
);

Expand All @@ -119,15 +130,15 @@ export const useActivityStore = defineStore('activity', () => {

// Actions
const toggleVeggie = (targetVeggie: string) => {
const now = DateTime.now().startOf('week');
const targetWeek = weeks.value.find(({startDate}) => startDate.equals(now));
const weekStart = currentDate.value.startOf('week');
const targetWeek = weeks.value.find(({startDate}) => startDate.equals(weekStart));
if (!targetWeek) {
weeks.value.push({
startDate: now,
startDate: weekStart,
veggies: [targetVeggie],
});
challenges.value.push({
startDate: now,
startDate: weekStart,
veggie: getRandomVeggie(),
});
} else if (!targetWeek.veggies.includes(targetVeggie)) {
Expand Down

0 comments on commit 825c5a5

Please sign in to comment.