Skip to content

Commit

Permalink
feat: CalendarGraph data follow username
Browse files Browse the repository at this point in the history
  • Loading branch information
baboon-king committed May 24, 2024
1 parent 781ee68 commit d345aa3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 49 deletions.
9 changes: 9 additions & 0 deletions apps/client/api/userLearnRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ export async function fetchLearnRecord(params: UserLearnRecord) {
},
);
}

export async function fetchLearnRecordByUserId(params: UserLearnRecord & { userId: string }) {
return await http.get<unknown, UserLearnRecordResponse>(
`/user-learn-record/finishCountByUserId`,
{
params,
},
);
}
56 changes: 27 additions & 29 deletions apps/client/composables/learnRecord.ts
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,
};
}
25 changes: 5 additions & 20 deletions apps/client/pages/User/[username].vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,25 @@
class="mt-10"
:data="learnRecord.list"
:totalCount="learnRecord.totalCount"
@toggleYear="toggleYear"
@toggleYear="onToggleYear"
/>
</div>
</div>
</template>

<script setup lang="ts">
import { useRoute } from "#app";
import { ref } from "vue";
import { getUserByUsername } from "~/api/user";
import { useLearnRecord } from "~/composables/learnRecord";
import { type CalendarData } from "~/composables/user/calendarGraph";
const route = useRoute();
const user = await getUserByUsername(route.params.username as string);
const { learnRecord, setupLearnRecord, setQueryYear } = useLearnRecord();
const { toggleYear } = useCalendarGraph();
const { learnRecord, year } = useLearnRecord({ userId: user?.id });
function useCalendarGraph() {
const data = ref<CalendarData[]>([]);
const totalCount = ref<number>(0);
async function toggleYear(year?: number) {
setQueryYear(year);
setupLearnRecord();
}
return {
data,
totalCount,
toggleYear,
};
}
const onToggleYear = (value?: number) => {
year.value = value!;
};
</script>

<style scoped></style>

0 comments on commit d345aa3

Please sign in to comment.