119 lines
3.4 KiB
TypeScript
119 lines
3.4 KiB
TypeScript
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
import type { NPEDFieldType } from "../types/types";
|
|
import { useNPEDContext } from "../context/NPEDUserContext";
|
|
import { useEffect } from "react";
|
|
import { CAM_BASE } from "../utils/config";
|
|
|
|
async function fetchNPEDDetails() {
|
|
const fetchUrl = `${CAM_BASE}/api/fetch-config?id=NPED`;
|
|
const response = await fetch(fetchUrl);
|
|
if (!response.ok) throw new Error("Cannot reach fetch-config endpoint");
|
|
|
|
return response.json();
|
|
}
|
|
|
|
async function signIn(loginDetails: NPEDFieldType) {
|
|
const { frontId, rearId, username, password, clientId } = loginDetails;
|
|
const NPEDLoginURLFront = `${CAM_BASE}/api/update-config?id=${frontId}`;
|
|
const NPEDLoginURLRear = `${CAM_BASE}/api/update-config?id=${rearId}`;
|
|
const frontCameraPayload = {
|
|
id: frontId,
|
|
fields: [
|
|
{ property: "propEnabled", value: true },
|
|
{ property: "propUsername", value: username },
|
|
{ property: "propPassword", value: password },
|
|
{ property: "propClientID", value: clientId },
|
|
],
|
|
};
|
|
|
|
const rearCameraPayload = {
|
|
id: rearId,
|
|
fields: [
|
|
{ property: "propEnabled", value: true },
|
|
{ property: "propUsername", value: username },
|
|
{ property: "propPassword", value: password },
|
|
{ property: "propClientID", value: clientId },
|
|
],
|
|
};
|
|
const frontCameraResponse = await fetch(NPEDLoginURLFront, {
|
|
method: "POST",
|
|
body: JSON.stringify(frontCameraPayload),
|
|
});
|
|
|
|
const rearCameraResponse = await fetch(NPEDLoginURLRear, {
|
|
method: "POST",
|
|
body: JSON.stringify(rearCameraPayload),
|
|
});
|
|
|
|
if (!frontCameraResponse.ok) throw new Error("cannot reach NPED endpoint");
|
|
if (!rearCameraResponse.ok) throw new Error("cannot reach NPED endpoint");
|
|
|
|
const data = await frontCameraResponse.json();
|
|
console.log(data);
|
|
return {
|
|
frontResponse: frontCameraResponse.json(),
|
|
rearResponse: rearCameraResponse.json(),
|
|
};
|
|
}
|
|
|
|
async function signOut() {
|
|
const nullPayload = {
|
|
id: "NPED",
|
|
fields: [
|
|
{ property: "propEnabled", value: false },
|
|
{ property: "propUsername", value: "" },
|
|
{ property: "propPassword", value: "" },
|
|
{ property: "propClientID", value: "" },
|
|
],
|
|
};
|
|
const NPEDLoginURLFront = `${CAM_BASE}/api/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, user } = useNPEDContext();
|
|
|
|
const signInMutation = useMutation({
|
|
mutationKey: ["NPEDSignin"],
|
|
mutationFn: signIn,
|
|
onSuccess: async (data) => setUser(await data.frontResponse),
|
|
});
|
|
|
|
const signOutMutation = useMutation({
|
|
mutationKey: ["auth", "NPEDSignOut"],
|
|
mutationFn: signOut,
|
|
});
|
|
|
|
const fetchdataQuery = useQuery({
|
|
queryKey: ["auth", "NPEDFetch"],
|
|
queryFn: fetchNPEDDetails,
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (fetchdataQuery.isSuccess && fetchdataQuery.data) {
|
|
setUser(fetchdataQuery.data);
|
|
} else {
|
|
setUser(null);
|
|
}
|
|
}, [fetchdataQuery.data, fetchdataQuery.isSuccess, setUser]);
|
|
|
|
return {
|
|
signIn: signInMutation.mutate,
|
|
signInAsync: signInMutation.mutateAsync,
|
|
isPending: signInMutation.isPending,
|
|
isError: signInMutation.isError,
|
|
error: signInMutation.error,
|
|
data: signInMutation.data,
|
|
user,
|
|
setUser,
|
|
signOut: signOutMutation.mutate,
|
|
};
|
|
};
|