diff --git a/.example.txt b/.example.txt new file mode 100644 index 0000000..4b73e25 --- /dev/null +++ b/.example.txt @@ -0,0 +1 @@ +NEXT_PUBLIC_API_URL= \ No newline at end of file diff --git a/app/(private)/Impresiones/page.tsx b/app/(private)/Impresiones/page.tsx index e168e19..becda74 100644 --- a/app/(private)/Impresiones/page.tsx +++ b/app/(private)/Impresiones/page.tsx @@ -1,24 +1,38 @@ -'use client'; - import SearchUser from "../../Components/SearchUser/searchUser"; import Receipt from "../../Components/Receipt/Receipt"; import Impressions from "../../Components/Impressions/impressions"; import Toggle from "../../Components/Toggle/Toggle"; - -import "@/app/globals.css"; import Information from "../../Components/Information/information"; -export default function Page() { +import "@/app/globals.css"; +import { GetStudent } from "@/app/lib/getStudent"; + +export default async function Page({ + searchParams, +}: { + searchParams: { numAccount?: number }; +}) { + + const params = await searchParams; + const numAccount = params.numAccount + ? Number(params.numAccount) + : null; + + let student: any = null; + if (numAccount) { + student = await GetStudent(numAccount); + } + return (

IMPRESIONES Y PLOTEO

, + content: ( + + ), }, { key: "3", label: "Impresiones color", - content: , + content: ( + + ), }, { key: "4", label: "Plotter", - content: , + content: ( + + ), }, { key: "5", label: "Escaner", - content: , + content: ( + + ), }, ]} />
); } -//IO \ No newline at end of file +//IO diff --git a/app/(private)/Reportes/page.tsx b/app/(private)/Reportes/page.tsx index 83ec4bd..9624bec 100644 --- a/app/(private)/Reportes/page.tsx +++ b/app/(private)/Reportes/page.tsx @@ -35,7 +35,7 @@ export default function Page() { label: "Por recibo", content: ( <> - +
diff --git a/app/Components/Impressions/impressions.tsx b/app/Components/Impressions/impressions.tsx index c8bc9c3..f0906f6 100644 --- a/app/Components/Impressions/impressions.tsx +++ b/app/Components/Impressions/impressions.tsx @@ -1,7 +1,11 @@ -'use client' +"use client"; import { useState } from "react"; -import './Impressions.css' +import axios from "axios"; +import { useRouter } from "next/navigation"; +import Cookies from "js-cookie"; +import { envConfig } from "@/app/lib/config"; +import "./Impressions.css"; interface CostOption { value: number; @@ -9,117 +13,140 @@ interface CostOption { interface ImpressionsProps { costs: CostOption[]; + numAccount: number | null; } -function Impressions({ costs }:ImpressionsProps) { - const [pages, setPages] = useState(""); - const [cost, setCost] = useState(""); +function Impressions({ costs, numAccount }: ImpressionsProps) { + const [pages, setPages] = useState(""); + const [cost, setCost] = useState(""); - const [error, setError] = useState(''); - const [alert, setAlert] = useState(''); - const [showError, setShowError] = useState(false); - const [showAlert, setShowAlert] = useState(false); + const [error, setError] = useState(""); + const [alert, setAlert] = useState(""); + const [showError, setShowError] = useState(false); + const [showAlert, setShowAlert] = useState(false); - // const handlePayment = async () => { - // setAlert('') - // setError('') - // if (!studentData) { - // setError('Error busca denuevo al estudiante'); - // return; - // } - // if (!pages) { - // setError('Ingresa el numero de hojas a imprimir') - // return; - // } - // try { - // await axios.post( - // `${url}/impressions`, - // { - // numAccount: numAccount, - // pages: parseInt(pages), - // cost: parseInt(pages), - // }, - // { headers } - // ); - // handleSearch() - // setAlert('Cobro realizado correctamente'); - // setPages('') - // } catch (error) { - // const errorMessage = error.response?.data?.error || 'No se encontró el estudiante'; + const router = useRouter(); - // if (errorMessage === 'Token inválido') { - // handleLogout(); - // } + const handleLogout = () => { + Cookies.remove("token"); + Cookies.remove("session"); + router.push("/Login"); + }; - // setError(errorMessage); - // } - // }; + const handlePayment = async () => { + setAlert(""); + setError(""); - return ( - { - e.preventDefault(); - // handlePayment(); - }}> -
+ if (!numAccount) { + setError("Error busca denuevo al estudiante"); + return; + } -
- - -
+ if (!pages) { + setError("Ingresa el numero de hojas a imprimir"); + return; + } -
- - { - const value = e.target.value; - if (/^\d*$/.test(value)) { - setPages(value); - } - }} - placeholder='Numero de hojas a imprimir...' - inputMode='numeric' - pattern='[0-9]*' - /> -
+ if(!cost){ + setError("Selecciona un costo") + return; + } -
- -
+ const token = Cookies.get("token"); + if (!token) { + handleLogout(); + return; + } -
- + try { + await axios.post( + `${envConfig.apiUrl}/impressions`, + { + numAccount: numAccount, + pages: parseInt(pages), + cost: parseInt(cost) * parseInt(pages), + }, + { + headers: { + Authorization: `Bearer ${token}`, + }, + } + ); - {error && -
- {error} -
- } - {alert && -
- {alert} -
- } -
+ setAlert("Cobro realizado correctamente"); + setPages(""); + } catch (error: any) { + const errorMessage = + error.response?.data?.error || "No se encontró el estudiante"; + + if (errorMessage === "Token inválido") { + handleLogout(); + } + + setError(errorMessage); + } + }; + + return ( + { + e.preventDefault(); + handlePayment(); + }} + > +
+
+ + +
+ +
+ + { + const value = e.target.value; + if (/^\d*$/.test(value)) { + setPages(value); + } + }} + placeholder="Numero de hojas a imprimir..." + inputMode="numeric" + pattern="[0-9]*" + /> +
+ +
+ +
+ +
+ + + {error && ( +
+ {error}
- - ) + )} + {alert && ( +
+ {alert} +
+ )} +
+
+ + ); } -export default Impressions -//IO \ No newline at end of file +export default Impressions; +//IO diff --git a/app/Components/Receipt/Receipt.tsx b/app/Components/Receipt/Receipt.tsx index 606f4db..817af7c 100644 --- a/app/Components/Receipt/Receipt.tsx +++ b/app/Components/Receipt/Receipt.tsx @@ -1,6 +1,7 @@ 'use client' import { useEffect, useState } from "react"; import "./Receipt.css" +import { PostReceipt } from "@/app/lib/receipt"; function Receipt() { const [folio, setFolio] = useState(''); @@ -46,11 +47,15 @@ function Receipt() { }, [error]); //fadeOut alert and error// + const handleSaveReceipt = ( )=>{ + //PostReceipt(data{folio,amount,date}) + } + return (
{ e.preventDefault(); - // handleSaveReceipt(); + handleSaveReceipt(); }} > diff --git a/app/Components/SearchUser/searchUser.tsx b/app/Components/SearchUser/searchUser.tsx index 592912d..05da3d5 100644 --- a/app/Components/SearchUser/searchUser.tsx +++ b/app/Components/SearchUser/searchUser.tsx @@ -1,34 +1,44 @@ 'use client' + +import { useRouter } from "next/navigation"; import { useState } from "react"; function SearchUser() { - const [numAccount, setNumAccount] = useState(''); - return ( - <> - - -
- { - const value = e.target.value; - if (/^\d*$/.test(value) && value.length <= 9) { - setNumAccount(value); - } - }} - placeholder='Coloca un número de cuenta...' - inputMode='numeric' - pattern='[0-9]*' - /> - -
- - - ) + const [numAccount, setNumAccount] = useState(""); + const router = useRouter(); + + const handleSubmit = (e: React.FormEvent) => { + e.preventDefault(); + if (numAccount) { + router.push(`/Impresiones?numAccount=${numAccount}`); + } + }; + + return ( + <> +
+ +
+ { + const value = e.target.value; + if (/^\d*$/.test(value) && value.length <= 9) { + setNumAccount(value); + } + }} + placeholder="Coloca un número de cuenta..." + inputMode="numeric" + pattern="[0-9]*" + /> + +
+ + + ); } -export default SearchUser \ No newline at end of file +export default SearchUser; diff --git a/app/lib/getStudent.ts b/app/lib/getStudent.ts new file mode 100644 index 0000000..172bcff --- /dev/null +++ b/app/lib/getStudent.ts @@ -0,0 +1,15 @@ +import axios from "axios"; +import { envConfig } from "./config"; + +export async function GetStudent(numAccount: number) { + try { + const response = await axios.get(`${envConfig.apiUrl}/student/${numAccount}`); + return response.data; + } catch (error: any) { + const msg = + error.response?.data?.message || + error.message || + "Error desconocido al obtener estudiante"; + return { error: msg }; + } +} diff --git a/app/lib/receipt.ts b/app/lib/receipt.ts new file mode 100644 index 0000000..6385567 --- /dev/null +++ b/app/lib/receipt.ts @@ -0,0 +1,18 @@ +import axios from "axios"; +import { envConfig } from "./config"; + +export async function PostReceipt(usuario: string, password: string) { + try { + const response = await axios.post(`${envConfig.apiUrl}/operations`, { + usuario, + password, + }); + return response.data; + } catch (error: any) { + const msg = + error.response?.data?.message || + error.message || + "Error desconocido en el guardado del Recibo"; + return { error: msg }; + } +} diff --git a/package-lock.json b/package-lock.json index 5128bf1..b03311e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,12 +9,14 @@ "version": "0.1.0", "dependencies": { "axios": "^1.11.0", + "js-cookie": "^3.0.5", "next": "^15.5.3", "react": "19.1.0", "react-dom": "19.1.0", "react-icons": "^5.5.0" }, "devDependencies": { + "@types/js-cookie": "^3.0.6", "@types/node": "^20", "@types/react": "^19", "@types/react-dom": "^19", @@ -592,6 +594,13 @@ "tslib": "^2.8.0" } }, + "node_modules/@types/js-cookie": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/@types/js-cookie/-/js-cookie-3.0.6.tgz", + "integrity": "sha512-wkw9yd1kEXOPnvEeEV1Go1MmxtBJL0RR79aOTAApecWFVu7w0NNXNqhcWgvw2YgZDYadliXkl14pa3WXw5jlCQ==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/node": { "version": "20.19.11", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.11.tgz", @@ -960,6 +969,15 @@ "license": "MIT", "optional": true }, + "node_modules/js-cookie": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz", + "integrity": "sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==", + "license": "MIT", + "engines": { + "node": ">=14" + } + }, "node_modules/math-intrinsics": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", diff --git a/package.json b/package.json index 45a3de1..4c53cc4 100644 --- a/package.json +++ b/package.json @@ -10,12 +10,14 @@ }, "dependencies": { "axios": "^1.11.0", + "js-cookie": "^3.0.5", "next": "^15.5.3", "react": "19.1.0", "react-dom": "19.1.0", "react-icons": "^5.5.0" }, "devDependencies": { + "@types/js-cookie": "^3.0.6", "@types/node": "^20", "@types/react": "^19", "@types/react-dom": "^19",