From 80d73faad4f89ed2440d90607efa7dc7374a96f0 Mon Sep 17 00:00:00 2001 From: Atash03 Date: Mon, 23 Jun 2025 13:14:17 +0500 Subject: [PATCH] fix: added view functionality to videos --- app/(main)/treasury/[video_id]/page.tsx | 8 ------ components/InfoBlock.tsx | 38 ++++++++++++++++++++++--- notes.txt | 0 package.json | 2 +- 4 files changed, 35 insertions(+), 13 deletions(-) delete mode 100644 notes.txt diff --git a/app/(main)/treasury/[video_id]/page.tsx b/app/(main)/treasury/[video_id]/page.tsx index f8d42bb..5c97467 100644 --- a/app/(main)/treasury/[video_id]/page.tsx +++ b/app/(main)/treasury/[video_id]/page.tsx @@ -18,15 +18,7 @@ interface IParams { const VideoItem = async ({ params }: IParams) => { const queryClient = getQueryClient(); - // queryClient.prefetchQuery({ - // queryKey: ['video', `video:${params.video_id}`], - // queryFn: () => Queries.getVideo(params.video_id), - // }); - // queryClient.prefetchQuery({ - // queryKey: ['video', 'all'], - // queryFn: () => Queries.getVideos(''), - // }); await queryClient.prefetchQuery({ queryKey: ['video', `video:${params.video_id}`], queryFn: () => Queries.getVideo(params.video_id), diff --git a/components/InfoBlock.tsx b/components/InfoBlock.tsx index 729be49..65a6bf8 100644 --- a/components/InfoBlock.tsx +++ b/components/InfoBlock.tsx @@ -16,6 +16,8 @@ import horjun from "@/public/horjun.png"; import belet from "@/public/belet.jpg"; import ReactionsBlock from "./treasury/ReactionsBlock"; +import { useCallback, useEffect, useState } from "react"; +import axios from "axios"; interface IProps { video_id: number; @@ -23,9 +25,37 @@ interface IProps { const InfoBlock = ({ video_id }: IProps) => { const { data, isFetching, error } = useQuery({ - queryKey: ["video", video_id], + queryKey: ["video", `video:${video_id}`], queryFn: () => Queries.getVideo(video_id), }); + const [view, setView] = useState(data?.data?.view || 0); + + const addView = useCallback(async () => { + const materials = localStorage.getItem('MHB_MATERIALS_ID'); + if (materials) { + const materialsArray = materials.split(','); + if (!materialsArray.includes(video_id.toString())) { + const res = await axios.post(`https://turkmentv.gov.tm/v2/api/material/${video_id}/views/increment`); + if (res.status === 200) { + materialsArray.push(video_id.toString()); + setView(view + 1); + } + } + localStorage.setItem('MHB_MATERIALS_ID', materialsArray.join(',')); + } else { + const res = await axios.post(`https://turkmentv.gov.tm/v2/api/material/${video_id}/views/increment`); + if (res.status === 200) { + localStorage.setItem('MHB_MATERIALS_ID', [video_id.toString()].join(',')); + setView(view + 1); + } + } + + }, [video_id]) + + useEffect(() => { + addView(); + }, []); + if (isFetching) return ( @@ -52,7 +82,7 @@ const InfoBlock = ({ video_id }: IProps) => { className="w-[30px] h-[18px]" /> - {data!.data.view} + {view} @@ -76,8 +106,8 @@ const InfoBlock = ({ video_id }: IProps) => { {data?.data.aydym_com_url || - data?.data.horjun_content_url || - data?.data.belet_url ? ( + data?.data.horjun_content_url || + data?.data.belet_url ? (

Beýleki platformalarda seret: diff --git a/notes.txt b/notes.txt deleted file mode 100644 index e69de29..0000000 diff --git a/package.json b/package.json index 6f92a42..0099ac6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-mhb", - "version": "0.1.0", + "version": "1.0.0", "private": true, "scripts": { "dev": "next dev -p 4000",