Skip to content

Commit 00c7278

Browse files
committed
fix(history): clear state when switching users
1 parent 1f27433 commit 00c7278

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

src/components/views/history/HistoryComponent.vue

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<script lang="ts" setup>
22
import { onActivated, onDeactivated, onMounted, ref, watch } from 'vue';
33
4-
import type { WatchStopHandle } from 'vue';
5-
64
import type { OnScroll, OnUpdated } from '~/components/common/list/ListScroll.model';
75
86
import ListScroll from '~/components/common/list/ListScroll.vue';
@@ -18,22 +16,29 @@ const {
1816
pageSize,
1917
belowThreshold,
2018
} = useHistoryStoreRefs();
21-
const { fetchHistory } = useHistoryStore();
19+
const { fetchHistory, clearState } = useHistoryStore();
2220
2321
const { user } = useUserSettingsStoreRefs();
2422
25-
onMounted(() => {
26-
fetchHistory();
27-
});
28-
29-
const watcher = ref<WatchStopHandle>();
23+
const active = ref(false);
3024
3125
onActivated(() => {
32-
watcher.value = watch(user, () => fetchHistory());
26+
active.value = true;
27+
fetchHistory();
3328
});
3429
3530
onDeactivated(() => {
36-
watcher.value?.();
31+
active.value = false;
32+
});
33+
34+
onMounted(() => {
35+
watch(user, () => {
36+
if (active.value) {
37+
fetchHistory();
38+
} else {
39+
clearState();
40+
}
41+
});
3742
});
3843
3944
const onScroll: OnScroll = async listRef => {

src/stores/data/history.store.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,22 @@ export const useHistoryStore = defineStore('data.history', () => {
1414
const pageSize = ref(100);
1515
const history = ref<TraktHistory[]>([]);
1616
const pagination = ref<TraktClientPagination>();
17+
18+
const searchHistory = ref('');
19+
20+
const historyStart = ref<Date | undefined>(undefined);
21+
const historyEnd = ref<Date | undefined>(undefined);
22+
1723
const threshold = ref(10);
1824

25+
const clearState = () => {
26+
history.value = [];
27+
pagination.value = undefined;
28+
searchHistory.value = '';
29+
historyStart.value = undefined;
30+
historyEnd.value = undefined;
31+
};
32+
1933
const belowThreshold = computed(
2034
() =>
2135
pagination.value?.page &&
@@ -29,7 +43,6 @@ export const useHistoryStore = defineStore('data.history', () => {
2943
.map((_, i) => ({ id: -1 * (i + 1) }) as TraktHistory),
3044
);
3145

32-
const searchHistory = ref('');
3346
const filteredHistory = computed<TraktHistory[]>(() => {
3447
if (!searchHistory.value) return history.value;
3548
const _searchRaw = searchHistory.value.toLowerCase().trim();
@@ -50,9 +63,6 @@ export const useHistoryStore = defineStore('data.history', () => {
5063
});
5164
});
5265

53-
const historyStart = ref<Date | undefined>(undefined);
54-
const historyEnd = ref<Date | undefined>(undefined);
55-
5666
const fetchHistory = async ({
5767
page,
5868
limit = pageSize.value,
@@ -113,6 +123,7 @@ export const useHistoryStore = defineStore('data.history', () => {
113123
historyStart,
114124
historyEnd,
115125
setHistoryRange,
126+
clearState,
116127
};
117128
});
118129

src/styles/base.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
--bg-color: var(--bg-color-30);
9191
--bg-color-hover: var(--bg-black-60);
9292
}
93-
93+
9494
/*
9595
@media (prefers-color-scheme: light) {
9696
!* semantic color variables for this project *!

0 commit comments

Comments
 (0)