-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CalendarGraph data follow username
- Loading branch information
1 parent
781ee68
commit d345aa3
Showing
3 changed files
with
41 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,44 @@ | ||
import { ref } from "vue"; | ||
import type { MaybeRef } from "vue"; | ||
|
||
import { ref, toValue, watchEffect } from "vue"; | ||
|
||
import type { UserLearnRecordResponse } from "~/api/userLearnRecord"; | ||
import { fetchLearnRecord } from "~/api/userLearnRecord"; | ||
|
||
let year: number | undefined = 0; | ||
const learnRecord = ref<UserLearnRecordResponse>({ | ||
list: [], | ||
totalCount: 0, | ||
}); | ||
let isSetup = false; | ||
|
||
export function useLearnRecord() { | ||
function setQueryYear(val?: number) { | ||
if (year !== val) { | ||
year = val; | ||
isSetup = false; | ||
} | ||
} | ||
import { fetchLearnRecordByUserId } from "~/api/userLearnRecord"; | ||
|
||
interface UseLearnRecordOptions { | ||
year?: MaybeRef<number>; | ||
userId: string; | ||
} | ||
|
||
export function useLearnRecord(options: UseLearnRecordOptions) { | ||
const { userId } = options || {}; | ||
const learnRecord = ref<UserLearnRecordResponse>({ | ||
list: [], | ||
totalCount: 0, | ||
}); | ||
|
||
const year = ref(options.year || new Date().getFullYear()); | ||
|
||
function getQuery() { | ||
const yearStr = toValue(year); | ||
return { | ||
startDate: year ? `${year}-01-01` : undefined, | ||
endDate: year ? `${year}-12-31` : undefined, | ||
userId, | ||
startDate: yearStr ? `${yearStr}-01-01` : undefined, | ||
endDate: yearStr ? `${yearStr}-12-31` : undefined, | ||
}; | ||
} | ||
|
||
async function updateLearnRecord() { | ||
const res = await fetchLearnRecord(getQuery()); | ||
learnRecord.value = res; | ||
} | ||
|
||
async function setupLearnRecord() { | ||
if (isSetup) return; | ||
isSetup = true; | ||
const res = await fetchLearnRecord(getQuery()); | ||
const res = await fetchLearnRecordByUserId(getQuery()); | ||
learnRecord.value = res; | ||
} | ||
watchEffect(() => { | ||
updateLearnRecord(); | ||
}); | ||
|
||
return { | ||
year, | ||
learnRecord, | ||
updateLearnRecord, | ||
setQueryYear, | ||
setupLearnRecord, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters