diff --git a/src/libs/GlobalSearchContext/Provider.tsx b/src/libs/GlobalSearchContext/Provider.tsx new file mode 100644 index 0000000..78ef069 --- /dev/null +++ b/src/libs/GlobalSearchContext/Provider.tsx @@ -0,0 +1,27 @@ +import React, { useState } from 'react'; +import { useHistory } from 'react-router-dom'; +import { SEARCH_PAGE } from '@config/routes'; + +import Context from './context'; + +export interface Props { + children?: React.ReactNode; +} + +function Provider({ children }: Props) { + const [q, setQ] = useState(''); + const history = useHistory(); + const href = SEARCH_PAGE + `?q=${encodeURIComponent(q)}`; + + const goToSearchPage = () => { + history.push(href); + }; + + return ( + + {children} + + ); +} + +export default Provider; diff --git a/src/libs/GlobalSearchContext/context.ts b/src/libs/GlobalSearchContext/context.ts new file mode 100644 index 0000000..0710364 --- /dev/null +++ b/src/libs/GlobalSearchContext/context.ts @@ -0,0 +1,11 @@ +import { createContext } from 'react'; +import { ContextValue } from './types'; + +const ctx = createContext({ + href: '', + q: '', + setQ: (v: string) => {}, + goToSearchPage: () => {}, +}); + +export default ctx; diff --git a/src/libs/GlobalSearchContext/types.ts b/src/libs/GlobalSearchContext/types.ts new file mode 100644 index 0000000..436d741 --- /dev/null +++ b/src/libs/GlobalSearchContext/types.ts @@ -0,0 +1,6 @@ +export type ContextValue = { + href: string; + q: string; + setQ: (v: string) => void; + goToSearchPage: () => void; +}; diff --git a/src/libs/GlobalSearchContext/useGlobalSearch.ts b/src/libs/GlobalSearchContext/useGlobalSearch.ts new file mode 100644 index 0000000..76e1231 --- /dev/null +++ b/src/libs/GlobalSearchContext/useGlobalSearch.ts @@ -0,0 +1,9 @@ +import { useContext } from 'react'; +import ctx from './context'; +import { ContextValue } from './types'; + +const useGlobalSearch = (): ContextValue => { + return useContext(ctx); +}; + +export default useGlobalSearch;