-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding negative comments fetching
- Loading branch information
Showing
5 changed files
with
144 additions
and
21 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
gptube/app/dashboard/videos/[videoId]/negative-comments/page.tsx
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,31 @@ | ||
'use client' | ||
|
||
import { useParams } from 'next/navigation' | ||
|
||
import { useNegativeComments } from '@/hooks/use-negative-comments' | ||
import { VideoTopActions } from '@/components/videos/VideoTopActions' | ||
import { MainStatistics } from '@/components/videos/MainStatistics' | ||
|
||
// TODO: Set this page as protected route for new users | ||
function Video() { | ||
const { videoId } = useParams<{ videoId: string }>()! | ||
const { | ||
comments, | ||
isLoading: isLoadingVideos, | ||
isError: isErrorVideos, | ||
handleChangePage, | ||
page, | ||
totalPages, | ||
isFetching, | ||
} = useNegativeComments(videoId) | ||
|
||
console.log(comments) | ||
|
||
return ( | ||
<main className="w-full h-screen max-w-screen-lg px-6 py-6 mx-auto space-y-6"> | ||
<p>Negative comments</p> | ||
</main> | ||
) | ||
} | ||
|
||
export default Video |
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,55 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { useState, useEffect } from "react"; | ||
import toast from "react-hot-toast"; | ||
|
||
import { getVideoNegativeComments } from "@/services/get-video-negative-comments.service"; | ||
import { | ||
INITIAL_PAGE_VIDEOS_ANALYZED, | ||
PAGE_SIZE_VIDEOS_ANALYZED, | ||
} from "@/constants/videos-analyzed.constants"; | ||
import { videoQueryKeys } from "./video-query-keys"; | ||
import { useAuth } from "./use-auth"; | ||
|
||
export function useNegativeComments(videoId: string) { | ||
const { user } = useAuth(); | ||
const [page, setPage] = useState(INITIAL_PAGE_VIDEOS_ANALYZED); | ||
const [pageSize, setPageSize] = useState(PAGE_SIZE_VIDEOS_ANALYZED); | ||
|
||
const query = useQuery({ | ||
queryFn: () => getVideoNegativeComments({ userId: "1", videoId, page, pageSize }), | ||
queryKey: videoQueryKeys.videoNegativeComments({ | ||
userId: "1", | ||
videoId, | ||
page, | ||
pageSize, | ||
}), | ||
enabled: user !== null, | ||
keepPreviousData: true, | ||
}); | ||
|
||
const totalComments = query.data?.count ?? 0; | ||
const totalPages = totalComments / pageSize; | ||
|
||
useEffect(() => { | ||
if (query.error instanceof Error) { | ||
toast.error(query.error.message); | ||
} | ||
}, [query.error, query.isError]); | ||
|
||
const handleChangePage = (currentPage: number) => { | ||
setPage(+currentPage); | ||
// if (!query.data?.next) return | ||
|
||
// const { page: pageSearch, page_size } = extractSearchParamsVideo(query.data.next) | ||
|
||
// setPageSize(+page_size) | ||
}; | ||
|
||
return { | ||
comments: query.data?.results, | ||
handleChangePage, | ||
totalPages, | ||
page, | ||
...query, | ||
}; | ||
} |
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,56 +1,55 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { useEffect, useState } from "react"; | ||
import toast from "react-hot-toast"; | ||
import { useQuery } from '@tanstack/react-query' | ||
import { useEffect, useState } from 'react' | ||
import toast from 'react-hot-toast' | ||
|
||
import { getVideosAnalyzed } from "@/services/get-videos-analyzed.service"; | ||
import { getVideosAnalyzed } from '@/services/get-videos-analyzed.service' | ||
import { | ||
INITIAL_PAGE_VIDEOS_ANALYZED, | ||
PAGE_SIZE_VIDEOS_ANALYZED, | ||
} from "@/constants/videos-analyzed.constants"; | ||
import { extractSearchParamsVideo } from "@/utils/video.utils"; | ||
} from '@/constants/videos-analyzed.constants' | ||
|
||
import { videoQueryKeys } from "./video-query-keys"; | ||
import { useAuth } from "./use-auth"; | ||
import { videoQueryKeys } from './video-query-keys' | ||
import { useAuth } from './use-auth' | ||
|
||
export function useVideosAnalyzed() { | ||
const { user } = useAuth(); | ||
const [page, setPage] = useState(INITIAL_PAGE_VIDEOS_ANALYZED); | ||
const [pageSize, setPageSize] = useState(PAGE_SIZE_VIDEOS_ANALYZED); | ||
const { user } = useAuth() | ||
const [page, setPage] = useState(INITIAL_PAGE_VIDEOS_ANALYZED) | ||
const [pageSize, setPageSize] = useState(PAGE_SIZE_VIDEOS_ANALYZED) | ||
|
||
const query = useQuery({ | ||
queryFn: () => getVideosAnalyzed({ userId: "1", page, pageSize }), | ||
queryFn: () => getVideosAnalyzed({ userId: '1', page, pageSize }), | ||
queryKey: videoQueryKeys.videosAnalyzed({ | ||
userId: "1", | ||
userId: '1', | ||
page, | ||
pageSize, | ||
}), | ||
enabled: user !== null, | ||
keepPreviousData: true, | ||
}); | ||
}) | ||
|
||
const totalVideos = query.data?.count ?? 0; | ||
const totalPages = totalVideos / pageSize; | ||
const totalVideos = query.data?.count ?? 0 | ||
const totalPages = totalVideos / pageSize | ||
|
||
useEffect(() => { | ||
if (query.error instanceof Error) { | ||
toast.error(query.error.message); | ||
toast.error(query.error.message) | ||
} | ||
}, [query.error, query.isError]); | ||
}, [query.error, query.isError]) | ||
|
||
const handleChangePage = (currentPage: number) => { | ||
setPage(+currentPage); | ||
setPage(+currentPage) | ||
// if (!query.data?.next) return | ||
|
||
// const { page: pageSearch, page_size } = extractSearchParamsVideo(query.data.next) | ||
|
||
// setPageSize(+page_size) | ||
}; | ||
} | ||
|
||
return { | ||
videos: query.data?.results, | ||
handleChangePage, | ||
totalPages, | ||
page, | ||
...query, | ||
}; | ||
} | ||
} |
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,32 @@ | ||
import type { ApiYoutubeVideosVideoIdNegativeCommentsGetRequest } from '@/gptube-api' | ||
|
||
import { apiClient } from '@/gptube-api' | ||
import { handleError } from '@/utils/errors/handle-error' | ||
|
||
export async function getVideoNegativeComments({ | ||
userId, | ||
videoId, | ||
page, | ||
pageSize, | ||
}: { | ||
userId: string | ||
videoId: string | ||
page?: number | ||
pageSize?: number | ||
}) { | ||
try { | ||
const params: ApiYoutubeVideosVideoIdNegativeCommentsGetRequest = { | ||
userId: userId, | ||
videoId: videoId, | ||
} | ||
|
||
if (page) params.page = page | ||
if (pageSize) params.pageSize = pageSize | ||
|
||
const data = await apiClient.apiYoutubeVideosVideoIdNegativeCommentsGet(params) | ||
|
||
return data | ||
} catch (error) { | ||
handleError(error) | ||
} | ||
} |