-
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.
- Loading branch information
1 parent
aa9c2a4
commit 9ee1d89
Showing
11 changed files
with
199 additions
and
81 deletions.
There are no files selected for viewing
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
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,24 @@ | ||
import React, {useRef} from 'react' | ||
import useDate from "../hooks/useDate"; | ||
|
||
|
||
// todo : to be replaced by a proper range component. | ||
const Wip = () => { | ||
|
||
const {setDate} = useDate() | ||
|
||
const sinceInput = useRef<HTMLInputElement>(null); | ||
const untilInput = useRef<HTMLInputElement>(null); | ||
|
||
return ( | ||
<> | ||
<input type="text" ref={sinceInput}/> | ||
<input type="text" ref={untilInput}/> | ||
<button onClick={() => { | ||
// todo : typescript error missing value property on the HTMLInputElement | ||
setDate([sinceInput.current?.value, untilInput.current?.value]); | ||
}}>wip</button> | ||
</> | ||
) | ||
} | ||
export default Wip; |
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,20 @@ | ||
import React, {createContext, Dispatch, SetStateAction, useState} from 'react' | ||
|
||
|
||
export interface DateContextType { | ||
date: [string, string]; | ||
setDate: Dispatch<SetStateAction<[string, string]>>, | ||
} | ||
|
||
const DateContext = createContext<DateContextType | null>(null) | ||
|
||
export const DateProvider: React.FC<{ children: any }> = ({ children }) => { | ||
const [date, setDate] = useState<[string, string]>(['', '']) | ||
return ( | ||
<DateContext.Provider value={{date, setDate}}> | ||
{children} | ||
</DateContext.Provider> | ||
) | ||
} | ||
|
||
export default DateContext |
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,14 @@ | ||
import { useContext } from 'react' | ||
import DateContext, {DateContextType} from "../contexts/dateContext"; | ||
|
||
|
||
// todo : refactor in the contexts/dateContext.tsx file ? | ||
const useDate = (): DateContextType => { | ||
const value = useContext(DateContext) | ||
if (!value) { | ||
throw new Error(`Missing wrapping Provider for Store DateContextType`); | ||
} | ||
return value; | ||
} | ||
|
||
export default useDate |
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,6 @@ | ||
import Axios from "axios"; | ||
|
||
|
||
export const axios = Axios.create({ | ||
baseURL: `${process.env.NEXT_PUBLIC_WARREN_BACKEND_ROOT_URL}/api/v1/`, | ||
}); |
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,10 @@ | ||
import {DefaultOptions, QueryClient} from "@tanstack/react-query"; | ||
|
||
|
||
const queryConfig: DefaultOptions = { | ||
queries: { | ||
refetchOnWindowFocus: false, | ||
}, | ||
}; | ||
|
||
export const queryClient = new QueryClient({defaultOptions:queryConfig}) |
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,19 @@ | ||
import {DateProvider} from "../contexts/dateContext"; | ||
import {ReactQueryDevtools} from "@tanstack/react-query-devtools"; | ||
import {QueryClientProvider} from "@tanstack/react-query"; | ||
import {queryClient} from "../libs/react-query"; | ||
|
||
|
||
type AppProviderProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
const AppProvider = ({ children }: AppProviderProps) => ( | ||
<DateProvider> | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
<ReactQueryDevtools initialIsOpen /> | ||
</QueryClientProvider> | ||
</DateProvider> | ||
) | ||
export default AppProvider; |
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 {useQueries} from "@tanstack/react-query"; | ||
import {DailyViewsResponseItem, VideoViewsResponse} from "../types"; | ||
import {axios} from "../../libs/axios"; | ||
|
||
|
||
const getVideoViews = (videoId: string, since: string, until: string) => { | ||
return axios.get(`video/${videoId}/views`, {params: {...since && {since: since}, ...until && {until: until}}}) | ||
.then((res) => res.data); | ||
}; | ||
|
||
type UseVideosViewsOptions = { | ||
videoIds: Array<string>; | ||
since: string; | ||
until: string; | ||
onSuccess: (id: string, data: Array<DailyViewsResponseItem>) => void | ||
}; | ||
|
||
export const useVideosViews = ({videoIds, since, until, onSuccess}: UseVideosViewsOptions) => { | ||
return useQueries({ | ||
queries: videoIds.map((videoId) => { | ||
return { | ||
queryKey: ["videoViews", videoId, since, until], | ||
queryFn: () => getVideoViews(videoId, since, until), | ||
onSuccess: (data: VideoViewsResponse) => { | ||
onSuccess(videoId, data.daily_views); | ||
return data; | ||
}, | ||
}; | ||
}), | ||
}); | ||
} |
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,11 @@ | ||
|
||
|
||
export type DailyViewsResponseItem = { | ||
day: string; | ||
views: number; | ||
}; | ||
|
||
export type VideoViewsResponse = { | ||
total: number; | ||
daily_views: Array<DailyViewsResponseItem>; | ||
}; |