Skip to content

Commit

Permalink
feat: debounced of LearnRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
baboon-king committed May 28, 2024
1 parent 59c752b commit 5297388
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions apps/client/composables/learnRecord.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import type { MaybeRef } from "vue";

import { ref, toValue, watchEffect } from "vue";
import { refDebounced } from "@vueuse/core";
import { ref, toValue, watch } from "vue";

import type { UserLearnRecordResponse } from "~/api/userLearnRecord";
import { fetchLearnRecord } from "~/api/userLearnRecord";
import { useUserStore } from "~/store/user";

interface UseLearnRecordOptions {
year?: MaybeRef<number>;
userId: string;
/**
* @default string current user
*/
userId?: string;
}

export function useLearnRecord(options: UseLearnRecordOptions) {
const { userId } = options || {};
const userStore = useUserStore();
const { userId = userStore.userInfo?.sub! } = options || {};

const learnRecord = ref<UserLearnRecordResponse>({
list: [],
totalCount: 0,
});

const year = ref(options?.year || new Date().getFullYear());
const debouncedYear = refDebounced(year, 1500);

function getQuery() {
const yearStr = toValue(year);
Expand All @@ -32,9 +40,15 @@ export function useLearnRecord(options: UseLearnRecordOptions) {
const res = await fetchLearnRecord(getQuery());
learnRecord.value = res;
}
watchEffect(() => {
updateLearnRecord();
});
watch(
debouncedYear,
() => {
updateLearnRecord();
},
{
immediate: true,
},
);

return {
year,
Expand Down

0 comments on commit 5297388

Please sign in to comment.