Merged in enhancement/soundsettings (pull request #13)
enhancement/soundsettings
This commit is contained in:
@@ -2,15 +2,14 @@ import { Field, FieldArray, Form, Formik } from "formik";
|
|||||||
import FormGroup from "../components/FormGroup";
|
import FormGroup from "../components/FormGroup";
|
||||||
import type { FormValues, Hotlist } from "../../../types/types";
|
import type { FormValues, Hotlist } from "../../../types/types";
|
||||||
import { useSoundContext } from "../../../context/SoundContext";
|
import { useSoundContext } from "../../../context/SoundContext";
|
||||||
|
import { useCameraBlackboard } from "../../../hooks/useCameraBlackboard";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
|
||||||
const SoundSettingsFields = () => {
|
const SoundSettingsFields = () => {
|
||||||
const { state, dispatch } = useSoundContext();
|
const { state, dispatch } = useSoundContext();
|
||||||
const hotlists: Hotlist[] = [
|
const { mutation } = useCameraBlackboard();
|
||||||
{ name: "hotlist0", sound: "" },
|
|
||||||
{ name: "hotlist1", sound: "" },
|
const hotlists: Hotlist[] = state.hotlists;
|
||||||
{ name: "hotlist2", sound: "" },
|
|
||||||
];
|
|
||||||
|
|
||||||
const soundOptions = state?.soundOptions?.map((soundOption) => ({
|
const soundOptions = state?.soundOptions?.map((soundOption) => ({
|
||||||
value: soundOption?.name,
|
value: soundOption?.name,
|
||||||
@@ -23,9 +22,18 @@ const SoundSettingsFields = () => {
|
|||||||
hotlists,
|
hotlists,
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = (values: FormValues) => {
|
const handleSubmit = async (values: FormValues) => {
|
||||||
dispatch({ type: "UPDATE", payload: values });
|
dispatch({ type: "UPDATE", payload: values });
|
||||||
toast.success("Sound settings updated");
|
const result = await mutation.mutateAsync({
|
||||||
|
operation: "INSERT",
|
||||||
|
path: "soundSettings",
|
||||||
|
value: values,
|
||||||
|
});
|
||||||
|
if (result.reason !== "OK") {
|
||||||
|
toast.error("Cannot update sound settings");
|
||||||
|
} else {
|
||||||
|
toast.success("Sound Settings successfully updated");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
<Formik initialValues={initialValues} onSubmit={handleSubmit}>
|
<Formik initialValues={initialValues} onSubmit={handleSubmit}>
|
||||||
@@ -68,8 +76,8 @@ const SoundSettingsFields = () => {
|
|||||||
name="hotlists"
|
name="hotlists"
|
||||||
render={() => (
|
render={() => (
|
||||||
<div className="w-full m-2">
|
<div className="w-full m-2">
|
||||||
{values.hotlists.length > 0 ? (
|
{values?.hotlists?.length > 0 ? (
|
||||||
values.hotlists.map((hotlist, index) => (
|
values?.hotlists?.map((hotlist, index) => (
|
||||||
<div
|
<div
|
||||||
key={hotlist.name}
|
key={hotlist.name}
|
||||||
className="flex items-center m-2 w-full justify-between"
|
className="flex items-center m-2 w-full justify-between"
|
||||||
|
|||||||
@@ -1,13 +1,25 @@
|
|||||||
import { faVolumeHigh, faVolumeXmark } from "@fortawesome/free-solid-svg-icons";
|
import { faVolumeHigh, faVolumeXmark } from "@fortawesome/free-solid-svg-icons";
|
||||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
import { useSoundEnabled } from "react-sounds";
|
import { useSoundEnabled } from "react-sounds";
|
||||||
|
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
|
||||||
const SoundBtn = () => {
|
const SoundBtn = () => {
|
||||||
|
const { mutation, query } = useCameraBlackboard();
|
||||||
const [enabled, setEnabled] = useSoundEnabled();
|
const [enabled, setEnabled] = useSoundEnabled();
|
||||||
|
|
||||||
const handleClick = () => {
|
const handleClick = async () => {
|
||||||
setEnabled(!enabled);
|
const newEnabled = !enabled;
|
||||||
|
setEnabled(newEnabled);
|
||||||
|
await mutation.mutateAsync({
|
||||||
|
operation: "INSERT",
|
||||||
|
path: "soundEnabled",
|
||||||
|
value: { enabled: newEnabled },
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
useEffect(() => {
|
||||||
|
setEnabled(query?.data?.soundEnabled?.enabled);
|
||||||
|
}, [query?.data?.soundEnabled?.enabled, setEnabled]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button onClick={handleClick}>
|
<button onClick={handleClick}>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { useMemo, useReducer, type ReactNode } from "react";
|
import { useEffect, useMemo, useReducer, type ReactNode } from "react";
|
||||||
import { SoundContext } from "../SoundContext";
|
import { SoundContext } from "../SoundContext";
|
||||||
import { initialState, reducer } from "../reducers/SoundContextReducer";
|
import { initialState, reducer } from "../reducers/SoundContextReducer";
|
||||||
|
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
|
||||||
|
|
||||||
type SoundContextProviderProps = {
|
type SoundContextProviderProps = {
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
@@ -8,6 +9,20 @@ type SoundContextProviderProps = {
|
|||||||
|
|
||||||
const SoundContextProvider = ({ children }: SoundContextProviderProps) => {
|
const SoundContextProvider = ({ children }: SoundContextProviderProps) => {
|
||||||
const [state, dispatch] = useReducer(reducer, initialState);
|
const [state, dispatch] = useReducer(reducer, initialState);
|
||||||
|
const { mutation } = useCameraBlackboard();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchSound = async () => {
|
||||||
|
const result = await mutation.mutateAsync({
|
||||||
|
operation: "VIEW",
|
||||||
|
path: "soundSettings",
|
||||||
|
});
|
||||||
|
|
||||||
|
dispatch({ type: "UPDATE", payload: result.result });
|
||||||
|
};
|
||||||
|
fetchSound();
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, []);
|
||||||
const value = useMemo(() => ({ state, dispatch }), [state, dispatch]);
|
const value = useMemo(() => ({ state, dispatch }), [state, dispatch]);
|
||||||
return (
|
return (
|
||||||
<SoundContext.Provider value={value}>{children}</SoundContext.Provider>
|
<SoundContext.Provider value={value}>{children}</SoundContext.Provider>
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export function reducer(state: SoundState, action: SoundAction): SoundState {
|
|||||||
...state,
|
...state,
|
||||||
sightingSound: action.payload.sightingSound,
|
sightingSound: action.payload.sightingSound,
|
||||||
NPEDsound: action.payload.NPEDsound,
|
NPEDsound: action.payload.NPEDsound,
|
||||||
hotlists: action.payload.hotlists.map((hotlist) => ({
|
hotlists: action.payload.hotlists?.map((hotlist) => ({
|
||||||
name: hotlist.name,
|
name: hotlist.name,
|
||||||
sound: hotlist.sound,
|
sound: hotlist.sound,
|
||||||
})),
|
})),
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const getAllBlackboardData = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const viewBlackboardData = async (options: CameraBlackBoardOptions) => {
|
const viewBlackboardData = async (options: CameraBlackBoardOptions) => {
|
||||||
const response = await fetch(`${CAM_BASE}/api/blackboard`, {
|
const response = await fetch(`/api/blackboard`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify(options),
|
body: JSON.stringify(options),
|
||||||
@@ -45,7 +45,6 @@ export const useCameraBlackboard = () => {
|
|||||||
id: "viewBlackboardData",
|
id: "viewBlackboardData",
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
onSuccess: () => toast.success("Sighting successfully added to alert list"),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ export function useSightingFeed(url: string | undefined) {
|
|||||||
return latestRef;
|
return latestRef;
|
||||||
}, [latestRef]);
|
}, [latestRef]);
|
||||||
const soundSrc = useMemo(() => {
|
const soundSrc = useMemo(() => {
|
||||||
return getSoundFileURL(state.sightingSound) ?? switchSound;
|
return getSoundFileURL(state?.sightingSound) ?? switchSound;
|
||||||
}, [state.sightingSound]);
|
}, [state.sightingSound]);
|
||||||
|
|
||||||
//use latestref instead of trigger to revert back
|
//use latestref instead of trigger to revert back
|
||||||
|
|||||||
@@ -5,6 +5,14 @@ import tailwindcss from "@tailwindcss/vite";
|
|||||||
// https://vite.dev/config/
|
// https://vite.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [react(), tailwindcss()],
|
plugins: [react(), tailwindcss()],
|
||||||
server: { host: true },
|
server: {
|
||||||
|
host: true,
|
||||||
|
proxy: {
|
||||||
|
"/api": {
|
||||||
|
target: "http://100.118.196.113:8080",
|
||||||
|
changeOrigin: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
base: "/Mobile",
|
base: "/Mobile",
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user