@@ -4,6 +4,7 @@ import { LuCircleX } from "react-icons/lu"
44import { Removable } from "@/components/custom-ui/Removable"
55import { Button } from "@/components/ui/button"
66import { Input } from "@/components/ui/input"
7+ import { useStudentId } from "@/hooks/use-student-id"
78// import { MdDownload } from "react-icons/md";
89// import { FilterBtn } from "./FilterBtn";
910// import { enrollStatusOpts, enrollAllowedOpts } from "./columns";
@@ -22,83 +23,85 @@ export function Toolbar({ has, onCsvClick: _, table }: Props) {
2223 // const enrollStatusCol = table.getColumn("enrollStatus");
2324 // const enrollAllowedCol = table.getColumn("enrollAllowed");
2425
26+ const [ savedId ] = useStudentId ( )
2527 const [ matricolaFilter , setMatricolaFilter ] = useState < string > ( "" )
2628 const [ matricolaFilterSubmitted , setMatricolaFilterSubmitted ] =
2729 useState < boolean > ( false )
28- const { rows : filteredRows } = table . getFilteredRowModel ( )
2930
30- function filterTableMatricolaCol ( value ?: string ) {
31- idCol ?. setFilterValue ( value )
32- }
33-
34- function handleClearMatricolaFilter ( ) {
31+ function reset ( ) {
3532 setMatricolaFilter ( "" )
36- filterTableMatricolaCol ( )
33+ idCol ?. setFilterValue ( undefined )
3734 setMatricolaFilterSubmitted ( false )
3835 }
3936
40- function handleMatricolaFilterChange (
41- event : React . ChangeEvent < HTMLInputElement >
42- ) {
43- if ( matricolaFilterSubmitted ) filterTableMatricolaCol ( )
44- const input = event . target . value
45- setMatricolaFilter ( input )
46- setMatricolaFilterSubmitted ( false )
47- }
48-
49- async function handleMatricolaFilterSubmit (
50- e : React . FormEvent < HTMLFormElement >
51- ) {
52- e . preventDefault ( )
53-
54- if ( matricolaFilter . length === 0 ) handleClearMatricolaFilter ( )
55- else {
56- const hash = await sha256 ( matricolaFilter )
57- filterTableMatricolaCol ( hash )
58- setMatricolaFilterSubmitted ( true )
37+ async function submit ( matricola : string ) {
38+ if ( matricola . length === 0 ) {
39+ reset ( )
40+ return
5941 }
42+
43+ const hash = await sha256 ( matricola )
44+ idCol ?. setFilterValue ( hash )
45+ setMatricolaFilterSubmitted ( true )
6046 }
6147
6248 return (
6349 < div className = "flex w-full flex-wrap items-start justify-start gap-6 max-2xs:flex-col" >
6450 { has . id && idCol && (
65- < div className = "grid grid-cols-[auto_220px] grid-rows-[auto_auto] gap-x-4 gap-y-1" >
51+ < div className = "flex flex-row items-center gap-x-4 gap-y-1" >
6652 < p className = "self-center text-sm" > Matricola</ p >
67- { filteredRows . length > 0 && matricolaFilterSubmitted ? (
68- < Removable
69- onRemove = { handleClearMatricolaFilter }
70- className = "justify-between"
71- >
53+ { matricolaFilterSubmitted &&
54+ table . getFilteredRowModel ( ) . rows . length !== 0 ? (
55+ < Removable onRemove = { reset } className = "justify-between" >
7256 { matricolaFilter }
7357 </ Removable >
7458 ) : (
7559 < >
7660 < form
7761 className = "relative w-full"
78- onSubmit = { handleMatricolaFilterSubmit }
62+ onSubmit = { async ( e ) => {
63+ e . preventDefault ( )
64+ await submit ( matricolaFilter )
65+ } }
7966 >
8067 < Input
68+ className = {
69+ matricolaFilterSubmitted
70+ ? "border-red-600 dark:border-red-800"
71+ : ""
72+ }
8173 placeholder = "AX1234"
8274 value = { matricolaFilter }
83- onChange = { handleMatricolaFilterChange }
75+ onChange = { ( e ) => {
76+ setMatricolaFilterSubmitted ( false )
77+ setMatricolaFilter ( e . target . value )
78+ } }
8479 />
8580 { matricolaFilter . length > 0 && (
8681 < Button
8782 type = "button"
8883 variant = "ghost"
8984 size = "icon"
90- className = "-translate-y-1/2 absolute top-1/2 right-1 h-7 w-7 text-gray-500 hover:bg-transparent hover:text-gray-900 dark:text-gray-400 dark:hover:bg-transparent dark:hover:text-gray-100"
91- onClick = { handleClearMatricolaFilter }
85+ className = "absolute top-1 right-1 h-7 w-7 text-gray-500 hover:bg-transparent hover:text-gray-900 dark:text-gray-400 dark:hover:bg-transparent dark:hover:text-gray-100"
86+ onClick = { reset }
9287 >
9388 < LuCircleX className = "h-5 w-5" />
9489 < span className = "sr-only" > Clear</ span >
9590 </ Button >
9691 ) }
9792 </ form >
98- { matricolaFilterSubmitted && (
99- < p className = "col-start-2 text-red-600 text-sm dark:text-red-400" >
100- Matricola non trovata, ricontrolla.
101- </ p >
93+ { savedId && (
94+ < Button
95+ aria-label = "Seleziona ricerca recente"
96+ variant = "outline"
97+ className = "whitespace-nowrap text-center"
98+ onClick = { async ( ) => {
99+ await submit ( savedId )
100+ setMatricolaFilter ( savedId )
101+ } }
102+ >
103+ 📋 { savedId }
104+ </ Button >
102105 ) }
103106 </ >
104107 ) }
0 commit comments