added sound context, and functionality to select sighting sound
This commit is contained in:
18
src/context/SoundContext.ts
Normal file
18
src/context/SoundContext.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { createContext, useContext, type Dispatch } from "react";
|
||||
import type { SoundPayload, SoundState } from "../types/types";
|
||||
|
||||
type SoundContextType = {
|
||||
state: SoundState;
|
||||
dispatch: Dispatch<SoundPayload>;
|
||||
};
|
||||
|
||||
export const SoundContext = createContext<SoundContextType | undefined>(
|
||||
undefined
|
||||
);
|
||||
|
||||
export const useSoundContext = () => {
|
||||
const ctx = useContext(SoundContext);
|
||||
if (!ctx)
|
||||
throw new Error("useSoundContext must be used within <SoundContext>");
|
||||
return ctx;
|
||||
};
|
||||
18
src/context/providers/SoundContextProvider.tsx
Normal file
18
src/context/providers/SoundContextProvider.tsx
Normal file
@@ -0,0 +1,18 @@
|
||||
import { useReducer, type ReactNode } from "react";
|
||||
import { SoundContext } from "../SoundContext";
|
||||
import { initalState, reducer } from "../reducers/SoundContextReducer";
|
||||
|
||||
type SoundContextProviderProps = {
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
const SoundContextProvider = ({ children }: SoundContextProviderProps) => {
|
||||
const [state, dispatch] = useReducer(reducer, initalState);
|
||||
return (
|
||||
<SoundContext.Provider value={{ state, dispatch }}>
|
||||
{children}
|
||||
</SoundContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export default SoundContextProvider;
|
||||
19
src/context/reducers/SoundContextReducer.ts
Normal file
19
src/context/reducers/SoundContextReducer.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import type { SoundPayload, SoundState } from "../../types/types";
|
||||
|
||||
export const initalState: SoundState = {
|
||||
sightingSound: "switch",
|
||||
NPEDsound: "popup",
|
||||
hotlists: [],
|
||||
};
|
||||
|
||||
export function reducer(state: SoundState, action: SoundPayload) {
|
||||
switch (action.type) {
|
||||
case "UPDATE":
|
||||
return {
|
||||
...state,
|
||||
sightingSound: action.payload.sightingSound,
|
||||
NPEDsound: action.payload.NPEDsound,
|
||||
};
|
||||
}
|
||||
return state;
|
||||
}
|
||||
Reference in New Issue
Block a user