added nped list functionality

This commit is contained in:
2025-08-29 14:55:37 +01:00
parent d2b8827987
commit 4fd3bd4319
15 changed files with 152 additions and 39 deletions

View File

@@ -4,7 +4,6 @@ import { useQuery } from "@tanstack/react-query";
const apiUrl = import.meta.env.VITE_BASEURL;
async function fetchSnapshot(cameraSide: string) {
console.log(`${apiUrl}/${cameraSide}-preview`);
const response = await fetch(
// `http://100.116.253.81/Colour-preview`
`${apiUrl}/${cameraSide}-preview`

View File

@@ -3,6 +3,7 @@ import { useCallback, useEffect, useRef } from "react";
async function fetchSighting() {
const response = await fetch(
// `http://100.82.205.44/api`
`http://100.116.253.81/mergedHistory/sightingSummary?mostRecentRef=-1`
);
if (!response.ok) throw new Error("Failed to fetch sighting");

View File

@@ -1,15 +1,22 @@
import { useMutation } from "@tanstack/react-query";
import { useMutation, useQuery } from "@tanstack/react-query";
import type { NPEDFieldType } from "../types/types";
import { useNPEDContext } from "../context/NPEDUserContext";
import { useEffect } from "react";
const base_url = import.meta.env.VITE_OUTSIDE_BASEURL;
async function fetchNPEDDetails() {
const fetchUrl = `${base_url}/fetch-config?id=NPED`;
const response = await fetch(fetchUrl);
if (!response.ok) throw new Error("Cannot reach fetch-config endpoint");
const data = await response.json();
console.log(data);
return response.json();
}
async function signIn(loginDetails: NPEDFieldType) {
const { frontId, rearId, username, password, clientId } = loginDetails;
const NPEDLoginURLFront = `${base_url}/update-config?id=${frontId}`;
const NPEDLoginURLRear = `${base_url}/update-config?id=${rearId}`;
const frontCameraPayload = {
id: frontId,
fields: [
@@ -43,8 +50,6 @@ async function signIn(loginDetails: NPEDFieldType) {
if (!frontCameraResponse.ok) throw new Error("cannot reach NPED endpoint");
if (!rearCameraResponse.ok) throw new Error("cannot reach NPED endpoint");
console.log(frontCameraResponse);
console.log(rearCameraResponse);
return {
frontResponse: frontCameraResponse.json(),
rearResponse: rearCameraResponse.json(),
@@ -52,13 +57,28 @@ async function signIn(loginDetails: NPEDFieldType) {
}
async function signOut() {
const response = await fetch(url, { method: "POST" });
const nullPayload = {
id: "NPED",
fields: [
{ property: "propEnabled", value: false },
{ property: "propUsername", value: "" },
{ property: "propPassword", value: "" },
{ property: "propClientID", value: "" },
],
};
const NPEDLoginURLFront = `${base_url}/update-config?id=NPED`;
const response = await fetch(NPEDLoginURLFront, {
method: "POST",
body: JSON.stringify(nullPayload),
});
if (!response.ok) throw new Error("cannot reach NPED sign out endpoint");
return response.json();
}
export const useNPEDAuth = () => {
const { setUser } = useNPEDContext();
const { setUser, user } = useNPEDContext();
const signInMutation = useMutation({
mutationKey: ["NPEDSignin"],
@@ -69,9 +89,24 @@ export const useNPEDAuth = () => {
const signOutMutation = useMutation({
mutationKey: ["auth", "NPEDSignOut"],
mutationFn: signOut,
onSuccess: () => setUser(null),
});
const fetchdataQuery = useQuery({
queryKey: ["auth", "NPEDFetch"],
queryFn: fetchNPEDDetails,
});
useEffect(() => {
if (fetchdataQuery.isSuccess && fetchdataQuery.data) {
setUser(fetchdataQuery.data);
} else if (
!fetchdataQuery?.data?.propUsername?.value &&
!fetchdataQuery?.data?.propClientID?.value
) {
setUser(null);
}
}, [fetchdataQuery.data, fetchdataQuery.isSuccess, setUser]);
return {
signIn: signInMutation.mutate,
signInAsync: signInMutation.mutateAsync,
@@ -79,7 +114,8 @@ export const useNPEDAuth = () => {
isError: signInMutation.isError,
error: signInMutation.error,
data: signInMutation.data,
user,
setUser,
signOut: signOutMutation.mutate,
};
};

View File

@@ -6,6 +6,7 @@ import { useQuery } from "@tanstack/react-query";
async function fetchSighting(url: string, ref: number, signal?: AbortSignal) {
const dynamicUrl = `${url}${ref}`;
const res = await fetch(dynamicUrl, { signal });
if (!res.ok) throw new Error(String(res.status));
return (await res.json()) as SightingWidgetType;
@@ -20,12 +21,13 @@ export function useSightingFeed(url: string) {
const [mostRecent, setMostRecent] = useState<SightingWidgetType | null>(null);
const mostRecentRef = useRef<number>(-1);
const lastSeenRef = useRef<number | null>(null);
const { data, isPending } = useQuery({
queryKey: ["sighting"],
queryFn: ({ signal }) => fetchSighting(url, mostRecentRef.current, signal),
refetchInterval: 200,
refetchInterval: 2000,
refetchIntervalInBackground: true,
refetchOnWindowFocus: false,
staleTime: 0,