Skip to content

Commit 914e75f

Browse files
committed
Implement Question Views (Approach #2)
1 parent a113dc3 commit 914e75f

File tree

3 files changed

+6
-42
lines changed

3 files changed

+6
-42
lines changed

app/(root)/questions/[id]/page.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ import { Preview } from "@/components/editor/Preview";
77
import Metric from "@/components/Metric";
88
import UserAvatar from "@/components/UserAvatar";
99
import ROUTES from "@/constants/routes";
10-
import { getQuestion } from "@/lib/actions/question.action";
10+
import { getQuestion, incrementViews } from "@/lib/actions/question.action";
1111
import { formatNumber, getTimeStamp } from "@/lib/utils";
1212

13-
import View from "../view";
14-
1513
const QuestionDetails = async ({ params }: RouteParams) => {
1614
const { id } = await params;
17-
const { success, data: question } = await getQuestion({ questionId: id });
15+
16+
const [_, { success, data: question }] = await Promise.all([
17+
await incrementViews({ questionId: id }),
18+
await getQuestion({ questionId: id }),
19+
]);
1820

1921
if (!success || !question) return redirect("/404");
2022

2123
const { author, createdAt, answers, views, tags, content, title } = question;
2224

2325
return (
2426
<>
25-
<View questionId={id} />
26-
2727
<div className="flex-start w-full flex-col">
2828
<div className="flex w-full flex-col-reverse justify-between">
2929
<div className="flex items-center justify-start gap-1">

app/(root)/questions/view.tsx

-32
This file was deleted.

lib/actions/question.action.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"use server";
22

33
import mongoose, { Error, FilterQuery } from "mongoose";
4-
import { revalidatePath } from "next/cache";
54

6-
import ROUTES from "@/constants/routes";
75
import Question, { IQuestionDoc } from "@/database/question.model";
86
import TagQuestion from "@/database/tag-question.model";
97
import Tag, { ITagDoc } from "@/database/tag.model";
@@ -318,8 +316,6 @@ export async function incrementViews(
318316

319317
await question.save();
320318

321-
revalidatePath(ROUTES.QUESTION(questionId));
322-
323319
return {
324320
success: true,
325321
data: { views: question.views },

0 commit comments

Comments
 (0)