MAX_HEARTS added

This commit is contained in:
Sanidhya Kumar Verma
2024-03-22 06:52:56 +00:00
parent 31a205fd46
commit e0d238dbea
6 changed files with 21 additions and 11 deletions

View File

@ -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));

View File

@ -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));

View File

@ -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">

View File

@ -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."));

View File

@ -1 +1,3 @@
export const POINTS_TO_REFILL = 10;
export const MAX_HEARTS = 5;

View File

@ -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),
});