@@ -21,7 +21,7 @@ import { Dropzone } from "@mantine/dropzone";
2121import "@mantine/dropzone/styles.css" ;
2222import { useForm } from "@mantine/form" ;
2323import { useDisclosure } from "@mantine/hooks" ;
24- import { RootItem } from "@prisma/client" ;
24+ import { AcademicYear , RootItem } from "@prisma/client" ;
2525import React , { useState , useTransition } from "react" ;
2626import {
2727 FaCircleCheck ,
@@ -34,11 +34,19 @@ import {
3434import useSWR from "swr" ;
3535
3636interface CSVImportFormProp {
37- /** Undefiend treated as loading UI */
3837 productsByAcademicYear : Record < string , RootItem [ ] > ;
38+ /** Current academic year */
39+ academicYear : string ;
40+ /** Valid academic years */
41+ validAcademicYears : string [ ] ;
3942}
4043
41- const CSVImportForm : React . FC < CSVImportFormProp > = ( { productsByAcademicYear } ) => {
44+ // TODO: Pass in academic years explicitly, default to current year
45+ const CSVImportForm : React . FC < CSVImportFormProp > = ( {
46+ productsByAcademicYear,
47+ academicYear,
48+ validAcademicYears,
49+ } ) => {
4250 const [ isPending , startTransition ] = useTransition ( ) ;
4351 const [ formState , setFormState ] = useState < StatusReturn > ( {
4452 status : "pending" ,
@@ -49,7 +57,7 @@ const CSVImportForm: React.FC<CSVImportFormProp> = ({ productsByAcademicYear })
4957 productId :
5058 productsByAcademicYear [ Object . keys ( productsByAcademicYear ) [ 0 ] ] [ 0 ] . id . toString ( 10 ) ,
5159 csv : [ ] ,
52- academicYear : Object . keys ( productsByAcademicYear ) [ 0 ] ,
60+ academicYear : academicYear ,
5361 } ,
5462 validate : {
5563 productId : ( value : string ) => {
@@ -120,7 +128,7 @@ const CSVImportForm: React.FC<CSVImportFormProp> = ({ productsByAcademicYear })
120128 name = "academicYear"
121129 key = { form . key ( "academicYear" ) }
122130 description = "Import into this academic year"
123- data = { Object . keys ( productsByAcademicYear ) }
131+ data = { validAcademicYears }
124132 required
125133 { ...form . getInputProps ( "academicYear" ) }
126134 />
@@ -219,7 +227,13 @@ const fetcher = async (...args: Parameters<typeof fetch>) => {
219227 return res . json ( ) ;
220228} ;
221229
222- export const CSVImport = ( ) => {
230+ export const CSVImport = ( {
231+ academicYears,
232+ currentAcademicYear,
233+ } : {
234+ academicYears : string [ ] ;
235+ currentAcademicYear : string ;
236+ } ) => {
223237 const [ opened , { open, close } ] = useDisclosure ( false ) ;
224238
225239 const { data } = useSWR < Record < string , RootItem [ ] > > ( "/api/products" , fetcher ) ;
@@ -244,7 +258,11 @@ export const CSVImport = () => {
244258 </ Center >
245259 </ Stack >
246260 ) : (
247- < CSVImportForm productsByAcademicYear = { data } />
261+ < CSVImportForm
262+ academicYear = { currentAcademicYear }
263+ validAcademicYears = { academicYears }
264+ productsByAcademicYear = { data }
265+ />
248266 ) }
249267 </ Stack >
250268 </ Modal >
0 commit comments