|
@@ -1,4 +1,4 @@
|
|
|
-import React from 'react'
|
|
|
+import React, { useEffect } from 'react'
|
|
|
import { RouteComponentProps, useParams, navigate } from '@reach/router'
|
|
|
import {
|
|
|
Container,
|
|
@@ -14,16 +14,43 @@ import {
|
|
|
} from './VideoView.style'
|
|
|
import { VideoGrid } from '@/components'
|
|
|
import { VideoPlayer } from '@/shared/components'
|
|
|
-import { formatDateAgo } from '@/utils/time'
|
|
|
-import { formatNumber } from '@/utils/number'
|
|
|
-import { useQuery } from '@apollo/client'
|
|
|
-import { GET_VIDEO } from '@/api/queries'
|
|
|
+import { useMutation, useQuery } from '@apollo/client'
|
|
|
+import { ADD_VIDEO_VIEW, GET_VIDEO } from '@/api/queries'
|
|
|
import { GetVideo, GetVideoVariables } from '@/api/queries/__generated__/GetVideo'
|
|
|
import routes from '@/config/routes'
|
|
|
+import { formatVideoViewsAndDate } from '@/utils/video'
|
|
|
+import { AddVideoView, AddVideoViewVariables } from '@/api/queries/__generated__/AddVideoView'
|
|
|
|
|
|
const VideoView: React.FC<RouteComponentProps> = () => {
|
|
|
const { id } = useParams()
|
|
|
- const { loading, data } = useQuery<GetVideo, GetVideoVariables>(GET_VIDEO, { variables: { id } })
|
|
|
+ const { loading, data } = useQuery<GetVideo, GetVideoVariables>(GET_VIDEO, {
|
|
|
+ variables: { id },
|
|
|
+ })
|
|
|
+ const [addVideoView] = useMutation<AddVideoView, AddVideoViewVariables>(ADD_VIDEO_VIEW)
|
|
|
+
|
|
|
+ const videoID = data?.video?.id
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (!videoID) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ addVideoView({
|
|
|
+ variables: { id: videoID },
|
|
|
+ update: (cache, mutationResult) => {
|
|
|
+ cache.modify({
|
|
|
+ id: cache.identify({
|
|
|
+ __typename: 'Video',
|
|
|
+ id: videoID,
|
|
|
+ }),
|
|
|
+ fields: {
|
|
|
+ views: () => mutationResult.data?.addVideoView.views,
|
|
|
+ },
|
|
|
+ })
|
|
|
+ },
|
|
|
+ }).catch((error) => {
|
|
|
+ console.warn('Failed to increase video views', { error })
|
|
|
+ })
|
|
|
+ }, [addVideoView, videoID])
|
|
|
|
|
|
if (loading || !data) {
|
|
|
return <p>Loading</p>
|
|
@@ -48,9 +75,7 @@ const VideoView: React.FC<RouteComponentProps> = () => {
|
|
|
<TitleActionsContainer>
|
|
|
<Title>{title}</Title>
|
|
|
</TitleActionsContainer>
|
|
|
- <Meta>
|
|
|
- {formatNumber(views)} views • {formatDateAgo(publishedOnJoystreamAt)}
|
|
|
- </Meta>
|
|
|
+ <Meta>{formatVideoViewsAndDate(views, publishedOnJoystreamAt, { fullViews: true })}</Meta>
|
|
|
<StyledChannelAvatar
|
|
|
name={channel.handle}
|
|
|
avatarUrl={channel.avatarPhotoURL}
|