updated NPED code
This commit is contained in:
17
src/context/NPEDUserContext.tsx
Normal file
17
src/context/NPEDUserContext.tsx
Normal file
@@ -0,0 +1,17 @@
|
||||
import { createContext, useContext, type SetStateAction } from "react";
|
||||
import type { NPEDUser } from "../types/types";
|
||||
|
||||
type UserContextValue = {
|
||||
user: NPEDUser | null;
|
||||
setUser: React.Dispatch<SetStateAction<NPEDUser | null>>;
|
||||
};
|
||||
|
||||
export const NPEDUserContext = createContext<UserContextValue | undefined>(
|
||||
undefined
|
||||
);
|
||||
export const useNPEDContext = () => {
|
||||
const ctx = useContext(NPEDUserContext);
|
||||
if (!ctx)
|
||||
throw new Error("useNPEDContext must be used within <NPEDUserProvider>");
|
||||
return ctx;
|
||||
};
|
||||
@@ -1,6 +1,5 @@
|
||||
import { createContext, useContext, type ReactNode } from "react";
|
||||
import { createContext, useContext } from "react";
|
||||
import type { SightingWidgetType } from "../types/types";
|
||||
import { useSightingFeed } from "../hooks/useSightingFeed";
|
||||
|
||||
type SightingFeedContextType = {
|
||||
sightings: (SightingWidgetType | null | undefined)[];
|
||||
@@ -13,50 +12,10 @@ type SightingFeedContextType = {
|
||||
noSighting: boolean;
|
||||
};
|
||||
|
||||
type SightingFeedProviderProps = {
|
||||
url: string;
|
||||
children: ReactNode;
|
||||
side: string;
|
||||
};
|
||||
export const SightingFeedContext = createContext<
|
||||
SightingFeedContextType | undefined
|
||||
>(undefined);
|
||||
|
||||
const SightingFeedContext = createContext<SightingFeedContextType | undefined>(
|
||||
undefined
|
||||
);
|
||||
|
||||
export const SightingFeedProvider = ({
|
||||
children,
|
||||
url,
|
||||
side,
|
||||
}: SightingFeedProviderProps) => {
|
||||
const {
|
||||
sightings,
|
||||
selectedRef,
|
||||
setSelectedRef,
|
||||
effectiveSelected,
|
||||
mostRecent,
|
||||
isPending,
|
||||
noSighting,
|
||||
} = useSightingFeed(url);
|
||||
|
||||
return (
|
||||
<SightingFeedContext.Provider
|
||||
value={{
|
||||
sightings,
|
||||
selectedRef,
|
||||
setSelectedRef,
|
||||
effectiveSelected,
|
||||
mostRecent,
|
||||
side,
|
||||
isPending,
|
||||
noSighting,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</SightingFeedContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
// eslint-disable-next-line react-refresh/only-export-components
|
||||
export const useSightingFeedContext = () => {
|
||||
const ctx = useContext(SightingFeedContext);
|
||||
if (!ctx)
|
||||
|
||||
17
src/context/providers/NPEDUserContextProvider.tsx
Normal file
17
src/context/providers/NPEDUserContextProvider.tsx
Normal file
@@ -0,0 +1,17 @@
|
||||
import { useState, type ReactNode } from "react";
|
||||
import type { NPEDUser } from "../../types/types";
|
||||
import { NPEDUserContext } from "../NPEDUserContext";
|
||||
|
||||
type NPEDUserProviderType = {
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
export const NPEDUserProvider = ({ children }: NPEDUserProviderType) => {
|
||||
const [user, setUser] = useState<NPEDUser | null>(null);
|
||||
|
||||
return (
|
||||
<NPEDUserContext.Provider value={{ user, setUser }}>
|
||||
{children}
|
||||
</NPEDUserContext.Provider>
|
||||
);
|
||||
};
|
||||
42
src/context/providers/SightingFeedProvider.tsx
Normal file
42
src/context/providers/SightingFeedProvider.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { useSightingFeed } from "../../hooks/useSightingFeed";
|
||||
import { SightingFeedContext } from "../SightingFeedContext";
|
||||
|
||||
type SightingFeedProviderProps = {
|
||||
url: string;
|
||||
children: ReactNode;
|
||||
side: string;
|
||||
};
|
||||
|
||||
export const SightingFeedProvider = ({
|
||||
children,
|
||||
url,
|
||||
side,
|
||||
}: SightingFeedProviderProps) => {
|
||||
const {
|
||||
sightings,
|
||||
selectedRef,
|
||||
setSelectedRef,
|
||||
effectiveSelected,
|
||||
mostRecent,
|
||||
isPending,
|
||||
noSighting,
|
||||
} = useSightingFeed(url);
|
||||
|
||||
return (
|
||||
<SightingFeedContext.Provider
|
||||
value={{
|
||||
sightings,
|
||||
selectedRef,
|
||||
setSelectedRef,
|
||||
effectiveSelected,
|
||||
mostRecent,
|
||||
side,
|
||||
isPending,
|
||||
noSighting,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</SightingFeedContext.Provider>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user