mirror of
https://github.com/sanidhyy/duolingo-clone.git
synced 2025-05-17 13:55:52 +08:00
MAX_HEARTS added
This commit is contained in:
@ -4,6 +4,7 @@ import { auth } from "@clerk/nextjs";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
import { MAX_HEARTS } from "@/constants";
|
||||
import db from "@/db/drizzle";
|
||||
import { getUserProgress, getUserSubscription } from "@/db/queries";
|
||||
import { challengeProgress, challenges, userProgress } from "@/db/schema";
|
||||
@ -53,7 +54,7 @@ export const upsertChallengeProgress = async (challengeId: number) => {
|
||||
await db
|
||||
.update(userProgress)
|
||||
.set({
|
||||
hearts: Math.min(currentUserProgress.hearts + 1, 5),
|
||||
hearts: Math.min(currentUserProgress.hearts + 1, MAX_HEARTS),
|
||||
points: currentUserProgress.points + 10,
|
||||
})
|
||||
.where(eq(userProgress.userId, userId));
|
||||
|
@ -5,7 +5,7 @@ import { and, eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { POINTS_TO_REFILL } from "@/constants";
|
||||
import { MAX_HEARTS, POINTS_TO_REFILL } from "@/constants";
|
||||
import db from "@/db/drizzle";
|
||||
import {
|
||||
getCourseById,
|
||||
@ -104,7 +104,7 @@ export const refillHearts = async () => {
|
||||
const currentUserProgress = await getUserProgress();
|
||||
|
||||
if (!currentUserProgress) throw new Error("User progress not found.");
|
||||
if (currentUserProgress.hearts === 5)
|
||||
if (currentUserProgress.hearts === MAX_HEARTS)
|
||||
throw new Error("Hearts are already full.");
|
||||
if (currentUserProgress.points < POINTS_TO_REFILL)
|
||||
throw new Error("Not enough points.");
|
||||
@ -112,7 +112,7 @@ export const refillHearts = async () => {
|
||||
await db
|
||||
.update(userProgress)
|
||||
.set({
|
||||
hearts: 5,
|
||||
hearts: MAX_HEARTS,
|
||||
points: currentUserProgress.points - POINTS_TO_REFILL,
|
||||
})
|
||||
.where(eq(userProgress.userId, currentUserProgress.userId));
|
||||
|
@ -7,7 +7,7 @@ import { toast } from "sonner";
|
||||
import { refillHearts } from "@/actions/user-progress";
|
||||
import { createStripeUrl } from "@/actions/user-subscription";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { POINTS_TO_REFILL } from "@/constants";
|
||||
import { MAX_HEARTS, POINTS_TO_REFILL } from "@/constants";
|
||||
|
||||
type ItemsProps = {
|
||||
hearts: number;
|
||||
@ -23,7 +23,7 @@ export const Items = ({
|
||||
const [pending, startTransition] = useTransition();
|
||||
|
||||
const onRefillHearts = () => {
|
||||
if (pending || hearts === 5 || points < POINTS_TO_REFILL) return;
|
||||
if (pending || hearts === MAX_HEARTS || points < POINTS_TO_REFILL) return;
|
||||
|
||||
startTransition(() => {
|
||||
refillHearts().catch(() => toast.error("Something went wrong."));
|
||||
@ -54,10 +54,14 @@ export const Items = ({
|
||||
|
||||
<Button
|
||||
onClick={onRefillHearts}
|
||||
disabled={pending || hearts === 5 || points < POINTS_TO_REFILL}
|
||||
aria-disabled={pending || hearts === 5 || points < POINTS_TO_REFILL}
|
||||
disabled={
|
||||
pending || hearts === MAX_HEARTS || points < POINTS_TO_REFILL
|
||||
}
|
||||
aria-disabled={
|
||||
pending || hearts === MAX_HEARTS || points < POINTS_TO_REFILL
|
||||
}
|
||||
>
|
||||
{hearts === 5 ? (
|
||||
{hearts === MAX_HEARTS ? (
|
||||
"full"
|
||||
) : (
|
||||
<div className="flex items-center">
|
||||
|
@ -9,6 +9,7 @@ import { toast } from "sonner";
|
||||
|
||||
import { upsertChallengeProgress } from "@/actions/challenge-progress";
|
||||
import { reduceHearts } from "@/actions/user-progress";
|
||||
import { MAX_HEARTS } from "@/constants";
|
||||
import { challengeOptions, challenges, userSubscription } from "@/db/schema";
|
||||
import { useHeartsModal } from "@/store/use-hearts-modal";
|
||||
import { usePracticeModal } from "@/store/use-practice-modal";
|
||||
@ -125,7 +126,7 @@ export const Quiz = ({
|
||||
|
||||
// This is a practice
|
||||
if (initialPercentage === 100) {
|
||||
setHearts((prev) => Math.min(prev + 1, 5));
|
||||
setHearts((prev) => Math.min(prev + 1, MAX_HEARTS));
|
||||
}
|
||||
})
|
||||
.catch(() => toast.error("Something went wrong. Please try again."));
|
||||
|
@ -1 +1,3 @@
|
||||
export const POINTS_TO_REFILL = 10;
|
||||
|
||||
export const MAX_HEARTS = 5;
|
||||
|
@ -9,6 +9,8 @@ import {
|
||||
timestamp,
|
||||
} from "drizzle-orm/pg-core";
|
||||
|
||||
import { MAX_HEARTS } from "@/constants";
|
||||
|
||||
export const courses = pgTable("courses", {
|
||||
id: serial("id").primaryKey(),
|
||||
title: text("title").notNull(),
|
||||
@ -133,7 +135,7 @@ export const userProgress = pgTable("user_progress", {
|
||||
activeCourseId: integer("active_course_id").references(() => courses.id, {
|
||||
onDelete: "cascade",
|
||||
}),
|
||||
hearts: integer("hearts").notNull().default(5),
|
||||
hearts: integer("hearts").notNull().default(MAX_HEARTS),
|
||||
points: integer("points").notNull().default(0),
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user