2025-08-18 16:04:03 +01:00
|
|
|
import { Form, Formik, Field } from "formik";
|
|
|
|
|
import FormGroup from "../components/FormGroup";
|
2025-08-29 10:07:59 +01:00
|
|
|
import type { NPEDErrorValues, NPEDFieldType } from "../../../types/types";
|
|
|
|
|
import { useNPEDAuth } from "../../../hooks/useNPEDAuth";
|
|
|
|
|
import { toast } from "sonner";
|
2025-09-16 14:20:38 +01:00
|
|
|
import { faEyeSlash, faEye } from "@fortawesome/free-solid-svg-icons";
|
|
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
|
|
|
import { useState } from "react";
|
2025-10-27 09:35:59 +00:00
|
|
|
import { useIntegrationsContext } from "../../../context/IntegrationsContext";
|
2025-08-18 16:04:03 +01:00
|
|
|
|
|
|
|
|
const NPEDFields = () => {
|
2025-10-27 09:35:59 +00:00
|
|
|
const { state } = useIntegrationsContext();
|
2025-09-16 14:20:38 +01:00
|
|
|
const [showPwd, setShowPwd] = useState(false);
|
2025-10-27 09:35:59 +00:00
|
|
|
const { signIn, signOut } = useNPEDAuth();
|
2025-09-12 13:28:14 +01:00
|
|
|
|
2025-11-14 15:01:01 +00:00
|
|
|
const username = state.npedUser?.propUsername?.value;
|
|
|
|
|
const password = state.npedUser?.propPassword?.value;
|
|
|
|
|
const clientId = state.npedUser?.propClientID?.value;
|
|
|
|
|
const frontId = "NPED";
|
|
|
|
|
const rearId = "NPED";
|
|
|
|
|
|
|
|
|
|
const initialValues = {
|
|
|
|
|
username: username ?? "",
|
|
|
|
|
password: password ?? "",
|
|
|
|
|
clientId: clientId ?? "",
|
|
|
|
|
frontId: frontId,
|
|
|
|
|
rearId: rearId,
|
|
|
|
|
};
|
2025-08-18 16:04:03 +01:00
|
|
|
|
2025-10-06 15:18:58 +01:00
|
|
|
const handleSubmit = async (values: NPEDFieldType) => {
|
2025-08-29 10:07:59 +01:00
|
|
|
const valuesToSend = {
|
|
|
|
|
...values,
|
|
|
|
|
};
|
2025-10-06 15:18:58 +01:00
|
|
|
await signIn(valuesToSend);
|
2025-08-29 10:07:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const validateValues = (values: NPEDFieldType) => {
|
|
|
|
|
const errors: NPEDErrorValues = {};
|
|
|
|
|
if (!values.username) errors.username = "Required";
|
|
|
|
|
if (!values.password) errors.password = "Required";
|
|
|
|
|
if (!values.clientId) errors.clientId = "Required";
|
|
|
|
|
return errors;
|
2025-08-18 16:04:03 +01:00
|
|
|
};
|
|
|
|
|
|
2025-08-29 14:55:37 +01:00
|
|
|
const handleLogoutClick = () => {
|
|
|
|
|
signOut();
|
2025-09-10 09:05:47 +01:00
|
|
|
toast.warning("logged out of NPED");
|
2025-08-29 14:55:37 +01:00
|
|
|
};
|
|
|
|
|
|
2025-08-18 16:04:03 +01:00
|
|
|
return (
|
2025-10-27 09:35:59 +00:00
|
|
|
<Formik initialValues={initialValues} onSubmit={handleSubmit} validate={validateValues} enableReinitialize>
|
2025-10-06 15:18:58 +01:00
|
|
|
{({ errors, touched, isSubmitting }) => (
|
2025-09-26 13:58:14 +01:00
|
|
|
<Form className="flex flex-col space-y-5 px-2">
|
2025-08-29 10:07:59 +01:00
|
|
|
<FormGroup>
|
|
|
|
|
<label htmlFor="username">Username</label>
|
|
|
|
|
{touched.username && errors.username && (
|
2025-10-27 09:35:59 +00:00
|
|
|
<small className="absolute right-0 -top-5 text-red-500">{errors.username}</small>
|
2025-08-29 10:07:59 +01:00
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
name="username"
|
|
|
|
|
type="text"
|
|
|
|
|
id="username"
|
|
|
|
|
placeholder="NPED username"
|
|
|
|
|
className="p-1.5 border border-gray-400 rounded-lg"
|
|
|
|
|
/>
|
|
|
|
|
</FormGroup>
|
|
|
|
|
<FormGroup>
|
|
|
|
|
<label htmlFor="password">Password</label>
|
2025-10-15 11:00:52 +01:00
|
|
|
<div className="flex gap-2 items-center relative mb-4">
|
|
|
|
|
<Field
|
|
|
|
|
name="password"
|
|
|
|
|
type={showPwd ? "text" : "password"}
|
|
|
|
|
id="password"
|
|
|
|
|
placeholder="NPED Password"
|
|
|
|
|
className="p-2 border border-gray-400 rounded-lg w-full"
|
|
|
|
|
/>
|
|
|
|
|
{touched.password && errors.password && (
|
2025-10-27 09:35:59 +00:00
|
|
|
<small className="absolute right-0 -top-5 text-red-500">{errors.password}</small>
|
2025-10-15 11:00:52 +01:00
|
|
|
)}
|
|
|
|
|
<FontAwesomeIcon
|
|
|
|
|
type="button"
|
|
|
|
|
className="absolute right-5 end-0"
|
|
|
|
|
onClick={() => setShowPwd((s) => !s)}
|
|
|
|
|
icon={showPwd ? faEyeSlash : faEye}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-08-29 10:07:59 +01:00
|
|
|
</FormGroup>
|
|
|
|
|
<FormGroup>
|
|
|
|
|
<label htmlFor="clientId">Client ID</label>
|
|
|
|
|
{touched.clientId && errors.clientId && (
|
2025-10-27 09:35:59 +00:00
|
|
|
<small className="absolute right-0 -top-5 text-red-500">{errors.clientId}</small>
|
2025-08-29 10:07:59 +01:00
|
|
|
)}
|
|
|
|
|
<Field
|
|
|
|
|
name="clientId"
|
|
|
|
|
type="text"
|
|
|
|
|
id="clientId"
|
|
|
|
|
placeholder="NPED client ID"
|
|
|
|
|
className="p-1.5 border border-gray-400 rounded-lg"
|
|
|
|
|
/>
|
|
|
|
|
</FormGroup>
|
2025-11-14 15:01:01 +00:00
|
|
|
|
2025-10-27 09:35:59 +00:00
|
|
|
{!state.npedUser?.propClientID?.value ? (
|
2025-08-29 14:55:37 +01:00
|
|
|
<button
|
|
|
|
|
type="submit"
|
2025-11-11 15:44:06 +00:00
|
|
|
className="w-full md:w-1/4 text-white bg-green-700 hover:bg-green-800 font-small rounded-lg text-sm px-2 py-2.5 hover:cursor-pointer"
|
2025-08-29 14:55:37 +01:00
|
|
|
>
|
2025-10-06 15:18:58 +01:00
|
|
|
{isSubmitting ? "Logging in..." : "Login"}
|
2025-08-29 14:55:37 +01:00
|
|
|
</button>
|
|
|
|
|
) : (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
2025-11-11 15:44:06 +00:00
|
|
|
className="w-full md:w-1/4 border-red-700 bg-red-800 text-white font-small rounded-lg text-sm px-2 py-2.5 hover:cursor-pointer"
|
2025-08-29 14:55:37 +01:00
|
|
|
onClick={handleLogoutClick}
|
|
|
|
|
>
|
|
|
|
|
Logout
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
2025-08-29 10:07:59 +01:00
|
|
|
</Form>
|
|
|
|
|
)}
|
2025-08-18 16:04:03 +01:00
|
|
|
</Formik>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default NPEDFields;
|