-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add course list item active style (#180)
- Loading branch information
1 parent
240dbd6
commit b5b5d88
Showing
23 changed files
with
458 additions
and
173 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Controller, Get, UseGuards } from '@nestjs/common'; | ||
import { CourseHistoryService } from './course-history.service'; | ||
import { AuthGuard } from '../auth/auth.guard'; | ||
import { User, UserEntity } from '../user/user.decorators'; | ||
|
||
@Controller('course-history') | ||
export class CourseHistoryController { | ||
constructor(private readonly courseHistoryService: CourseHistoryService) {} | ||
|
||
@UseGuards(AuthGuard) | ||
@Get('') | ||
courseCompletionCount(@User() user: UserEntity) { | ||
return this.courseHistoryService.findAll(user); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CourseHistoryService } from './course-history.service'; | ||
import { CourseHistoryController } from './course-history.controller'; | ||
|
||
@Module({ | ||
controllers: [CourseHistoryController], | ||
providers: [CourseHistoryService], | ||
exports: [CourseHistoryService], | ||
}) | ||
export class CourseHistoryModule {} |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Injectable, Inject } from '@nestjs/common'; | ||
import { DB, DbType } from '../global/providers/db.provider'; | ||
import { courseHistory } from '@earthworm/shared'; | ||
import { eq, and } from 'drizzle-orm'; | ||
import { UserEntity } from 'src/user/user.decorators'; | ||
|
||
@Injectable() | ||
export class CourseHistoryService { | ||
constructor(@Inject(DB) private db: DbType) {} | ||
|
||
async findOne(userId: number, courseId: number) { | ||
return await this.db | ||
.select() | ||
.from(courseHistory) | ||
.where( | ||
and( | ||
eq(courseHistory.userId, userId), | ||
eq(courseHistory.courseId, courseId), | ||
), | ||
); | ||
} | ||
|
||
async create(userId: number, courseId: number) { | ||
await this.db.insert(courseHistory).values({ | ||
courseId, | ||
userId, | ||
completionCount: 1, | ||
}); | ||
} | ||
|
||
async updateCompletionCount(userId: number, courseId: number, count: number) { | ||
await this.db | ||
.update(courseHistory) | ||
.set({ | ||
completionCount: count + 1, | ||
}) | ||
.where( | ||
and( | ||
eq(courseHistory.userId, userId), | ||
eq(courseHistory.courseId, courseId), | ||
), | ||
); | ||
} | ||
|
||
async setCompletionCount(userId: number, courseId: number) { | ||
const result = await this.findOne(userId, courseId); | ||
if (result && result.length) { | ||
this.updateCompletionCount(userId, courseId, result[0].completionCount); | ||
} else { | ||
this.create(userId, courseId); | ||
} | ||
} | ||
|
||
async findAll(user: UserEntity) { | ||
return await this.db | ||
.select() | ||
.from(courseHistory) | ||
.where(eq(courseHistory.userId, user.userId)); | ||
} | ||
} |
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
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
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { http } from "./http"; | ||
|
||
export interface CourseHistory { | ||
courseId: number; | ||
completionCount: string; | ||
} | ||
|
||
export async function fetchCourseHistory() { | ||
return await http.get<CourseHistory[], CourseHistory[]>("/course-history"); | ||
} |
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,31 +1,86 @@ | ||
<template> | ||
<div class=" | ||
flex | ||
flex-col | ||
w-[360px] | ||
h-[160px] | ||
sm:w-[500px] | ||
md:w-[340px] | ||
lg:w-[280px] | ||
xl:w-[260px] | ||
p-4 | ||
pb-6 | ||
border | ||
border-slate-400 | ||
rounded-xl | ||
hover:shadow-lg | ||
hover:shadow-purple-400/50 | ||
hover:border-purple-400 | ||
transition-all | ||
duration-500 | ||
"> | ||
<h3 class="text-xl font-bold dark:text-gray-100">{{ title }}</h3> | ||
<p class="mt-4 truncate">this is the course's description</p> | ||
<div | ||
class="course-card" | ||
:class="{ | ||
'state-finished': hasFinished, | ||
'current-card': currentCourse, | ||
}" | ||
> | ||
<h3 class="text-base font-bold dark:text-gray-100">{{ title }}</h3> | ||
<p | ||
class="text-sm mt-4 truncate" | ||
:class=" | ||
currentCourse | ||
? 'text-[rgba(255,255,255,0.8)]' | ||
: 'text-[rgba(136,136,136,1)]' | ||
" | ||
> | ||
this is the course's description | ||
</p> | ||
|
||
<div | ||
v-if="hasFinished" | ||
class="tooltip count" | ||
:class="{ | ||
'state-finished-count': hasFinished, | ||
'current-count': currentCourse, | ||
}" | ||
:data-tip="dataTip" | ||
> | ||
{{ count }} | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
defineProps<{ | ||
title: string | ||
}>() | ||
import { computed } from "vue"; | ||
import { useActiveCourseId } from "~/composables/courses/activeCourse"; | ||
const props = defineProps<{ | ||
title: string; | ||
id: number; | ||
count: number | undefined; | ||
}>(); | ||
const { activeCourseId } = useActiveCourseId(); | ||
const currentCourse = computed(() => activeCourseId.value == props.id); | ||
const dataTip = computed( | ||
() => `Congratulations! you've completed the course ${props.count} times.` | ||
); | ||
const hasFinished = computed(() => !!props.count); | ||
</script> | ||
|
||
<style scoped> | ||
.course-card { | ||
@apply flex flex-col w-[360px] h-[160px] sm:w-[500px] md:w-[340px] lg:w-[280px] xl:w-[260px] p-4 pb-6 border border-[rgba(164,175,191,1)] hover:shadow-lg hover:shadow-[rgba(164,175,191,0.5)] hover:border-[rgba(164,175,191,1)] rounded-xl transition-all duration-500 relative; | ||
} | ||
.count { | ||
@apply absolute bottom-1.5 right-2 text-xs w-7 h-5 leading-5 text-center text-white bg-[rgba(164,175,191,1)] rounded-md; | ||
} | ||
.state-underway { | ||
@apply border-[rgba(242,100,25,1)] hover:shadow-[rgba(242,100,25,0.5)] hover:border-[rgba(242,100,25,1)]; | ||
} | ||
.state-underway-count { | ||
@apply bg-[rgba(242,100,25,1)]; | ||
} | ||
.state-finished { | ||
@apply border-[rgba(151,71,255,1)] hover:shadow-[rgba(151,71,255,0.5)] hover:border-[rgba(151,71,255,1)]; | ||
} | ||
.state-finished-count { | ||
@apply bg-[rgba(151,71,255,1)]; | ||
} | ||
.current-card { | ||
@apply bg-[rgba(242,100,25,1)] border-[rgba(242,100,25,1)] text-white hover:shadow-[rgba(242,100,25,0.5)] hover:border-[rgba(242,100,25,1)]; | ||
} | ||
.current-count { | ||
@apply bg-white text-[rgba(242,100,25,1)]; | ||
} | ||
</style> |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ref } from "vue"; | ||
|
||
export const ACTIVE_COURSE_ID = "activeCourseId"; | ||
|
||
export function useActiveCourseId() { | ||
const activeCourseId = ref<number>( | ||
Number(localStorage.getItem(ACTIVE_COURSE_ID)) | ||
); | ||
|
||
function updateActiveCourseId(id: number) { | ||
activeCourseId.value = id; | ||
localStorage.setItem(ACTIVE_COURSE_ID, String(id)); | ||
} | ||
|
||
function restActiveCourseId() { | ||
localStorage.removeItem(ACTIVE_COURSE_ID); | ||
} | ||
|
||
return { | ||
activeCourseId, | ||
restActiveCourseId, | ||
updateActiveCourseId, | ||
}; | ||
} |
Oops, something went wrong.