Skip to content

Commit 9bddad1

Browse files
committed
feat: CalendarGraph data follow username
1 parent b169ca2 commit 9bddad1

File tree

3 files changed

+41
-49
lines changed

3 files changed

+41
-49
lines changed

apps/client/api/userLearnRecord.ts

+9
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@ export async function fetchLearnRecord(params: UserLearnRecord) {
1818
},
1919
);
2020
}
21+
22+
export async function fetchLearnRecordByUserId(params: UserLearnRecord & { userId: string }) {
23+
return await http.get<unknown, UserLearnRecordResponse>(
24+
`/user-learn-record/finishCountByUserId`,
25+
{
26+
params,
27+
},
28+
);
29+
}
+27-29
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
1-
import { ref } from "vue";
1+
import type { MaybeRef } from "vue";
2+
3+
import { ref, toValue, watchEffect } from "vue";
24

35
import type { UserLearnRecordResponse } from "~/api/userLearnRecord";
4-
import { fetchLearnRecord } from "~/api/userLearnRecord";
5-
6-
let year: number | undefined = 0;
7-
const learnRecord = ref<UserLearnRecordResponse>({
8-
list: [],
9-
totalCount: 0,
10-
});
11-
let isSetup = false;
12-
13-
export function useLearnRecord() {
14-
function setQueryYear(val?: number) {
15-
if (year !== val) {
16-
year = val;
17-
isSetup = false;
18-
}
19-
}
6+
import { fetchLearnRecordByUserId } from "~/api/userLearnRecord";
7+
8+
interface UseLearnRecordOptions {
9+
year?: MaybeRef<number>;
10+
userId: string;
11+
}
12+
13+
export function useLearnRecord(options: UseLearnRecordOptions) {
14+
const { userId } = options || {};
15+
const learnRecord = ref<UserLearnRecordResponse>({
16+
list: [],
17+
totalCount: 0,
18+
});
19+
20+
const year = ref(options.year || new Date().getFullYear());
2021

2122
function getQuery() {
23+
const yearStr = toValue(year);
2224
return {
23-
startDate: year ? `${year}-01-01` : undefined,
24-
endDate: year ? `${year}-12-31` : undefined,
25+
userId,
26+
startDate: yearStr ? `${yearStr}-01-01` : undefined,
27+
endDate: yearStr ? `${yearStr}-12-31` : undefined,
2528
};
2629
}
2730

2831
async function updateLearnRecord() {
29-
const res = await fetchLearnRecord(getQuery());
30-
learnRecord.value = res;
31-
}
32-
33-
async function setupLearnRecord() {
34-
if (isSetup) return;
35-
isSetup = true;
36-
const res = await fetchLearnRecord(getQuery());
32+
const res = await fetchLearnRecordByUserId(getQuery());
3733
learnRecord.value = res;
3834
}
35+
watchEffect(() => {
36+
updateLearnRecord();
37+
});
3938

4039
return {
40+
year,
4141
learnRecord,
4242
updateLearnRecord,
43-
setQueryYear,
44-
setupLearnRecord,
4543
};
4644
}

apps/client/pages/User/[username].vue

+5-20
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,25 @@
4040
class="mt-10"
4141
:data="learnRecord.list"
4242
:totalCount="learnRecord.totalCount"
43-
@toggleYear="toggleYear"
43+
@toggleYear="onToggleYear"
4444
/>
4545
</div>
4646
</div>
4747
</template>
4848

4949
<script setup lang="ts">
5050
import { useRoute } from "#app";
51-
import { ref } from "vue";
5251
5352
import { getUserByUsername } from "~/api/user";
5453
import { useLearnRecord } from "~/composables/learnRecord";
55-
import { type CalendarData } from "~/composables/user/calendarGraph";
5654
5755
const route = useRoute();
5856
const user = await getUserByUsername(route.params.username as string);
59-
const { learnRecord, setupLearnRecord, setQueryYear } = useLearnRecord();
60-
const { toggleYear } = useCalendarGraph();
57+
const { learnRecord, year } = useLearnRecord({ userId: user?.id });
6158
62-
function useCalendarGraph() {
63-
const data = ref<CalendarData[]>([]);
64-
const totalCount = ref<number>(0);
65-
66-
async function toggleYear(year?: number) {
67-
setQueryYear(year);
68-
setupLearnRecord();
69-
}
70-
71-
return {
72-
data,
73-
totalCount,
74-
toggleYear,
75-
};
76-
}
59+
const onToggleYear = (value?: number) => {
60+
year.value = value!;
61+
};
7762
</script>
7863

7964
<style scoped></style>

0 commit comments

Comments
 (0)