1- import { useState } from "react"
1+ import { useCallback , useState } from "react"
22import { Progress , useToast } from "@chakra-ui/react"
33import type XLSX from "xlsx"
44import { UploadStep } from "./UploadStep/UploadStep"
@@ -49,6 +49,19 @@ export const UploadFlow = ({ nextStep }: Props) => {
4949 const [ state , setState ] = useState < StepState > ( initialStepState || { type : StepType . upload } )
5050 const { maxRecords, translations, uploadStepHook, selectHeaderStepHook, matchColumnsStepHook } = useRsi ( )
5151 const toast = useToast ( )
52+ const errorToast = useCallback (
53+ ( description : string ) => {
54+ toast ( {
55+ status : "error" ,
56+ variant : "left-accent" ,
57+ position : "bottom-left" ,
58+ title : `${ translations . alerts . toast . error } ` ,
59+ description,
60+ isClosable : true ,
61+ } )
62+ } ,
63+ [ toast , translations ] ,
64+ )
5265
5366 switch ( state . type ) {
5467 case StepType . upload :
@@ -58,21 +71,19 @@ export const UploadFlow = ({ nextStep }: Props) => {
5871 const isSingleSheet = workbook . SheetNames . length === 1
5972 if ( isSingleSheet ) {
6073 if ( maxRecords && exceedsMaxRecords ( workbook . Sheets [ workbook . SheetNames [ 0 ] ] , maxRecords ) ) {
61- toast ( {
62- status : "error" ,
63- variant : "left-accent" ,
64- position : "bottom-left" ,
65- title : `${ translations . uploadStep . maxRecordsExceeded ( maxRecords . toString ( ) ) } ` ,
66- isClosable : true ,
67- } )
74+ errorToast ( translations . uploadStep . maxRecordsExceeded ( maxRecords . toString ( ) ) )
6875 return
6976 }
70- const mappedWorkbook = await uploadStepHook ( mapWorkbook ( workbook ) )
71- setState ( {
72- type : StepType . selectHeader ,
73- data : mappedWorkbook ,
74- } )
75- nextStep ( )
77+ try {
78+ const mappedWorkbook = await uploadStepHook ( mapWorkbook ( workbook ) )
79+ setState ( {
80+ type : StepType . selectHeader ,
81+ data : mappedWorkbook ,
82+ } )
83+ nextStep ( )
84+ } catch ( e ) {
85+ errorToast ( ( e as Error ) . message )
86+ }
7687 } else {
7788 setState ( { type : StepType . selectSheet , workbook } )
7889 }
@@ -85,21 +96,19 @@ export const UploadFlow = ({ nextStep }: Props) => {
8596 sheetNames = { state . workbook . SheetNames }
8697 onContinue = { async ( sheetName ) => {
8798 if ( maxRecords && exceedsMaxRecords ( state . workbook . Sheets [ sheetName ] , maxRecords ) ) {
88- toast ( {
89- status : "error" ,
90- variant : "left-accent" ,
91- position : "bottom-left" ,
92- title : `${ translations . uploadStep . maxRecordsExceeded ( maxRecords . toString ( ) ) } ` ,
93- isClosable : true ,
94- } )
99+ errorToast ( translations . uploadStep . maxRecordsExceeded ( maxRecords . toString ( ) ) )
95100 return
96101 }
97- const mappedWorkbook = await uploadStepHook ( mapWorkbook ( state . workbook , sheetName ) )
98- setState ( {
99- type : StepType . selectHeader ,
100- data : mappedWorkbook ,
101- } )
102- nextStep ( )
102+ try {
103+ const mappedWorkbook = await uploadStepHook ( mapWorkbook ( state . workbook , sheetName ) )
104+ setState ( {
105+ type : StepType . selectHeader ,
106+ data : mappedWorkbook ,
107+ } )
108+ nextStep ( )
109+ } catch ( e ) {
110+ errorToast ( ( e as Error ) . message )
111+ }
103112 } }
104113 />
105114 )
@@ -108,13 +117,17 @@ export const UploadFlow = ({ nextStep }: Props) => {
108117 < SelectHeaderStep
109118 data = { state . data }
110119 onContinue = { async ( ...args ) => {
111- const { data, headerValues } = await selectHeaderStepHook ( ...args )
112- setState ( {
113- type : StepType . matchColumns ,
114- data,
115- headerValues,
116- } )
117- nextStep ( )
120+ try {
121+ const { data, headerValues } = await selectHeaderStepHook ( ...args )
122+ setState ( {
123+ type : StepType . matchColumns ,
124+ data,
125+ headerValues,
126+ } )
127+ nextStep ( )
128+ } catch ( e ) {
129+ errorToast ( ( e as Error ) . message )
130+ }
118131 } }
119132 />
120133 )
@@ -124,12 +137,16 @@ export const UploadFlow = ({ nextStep }: Props) => {
124137 data = { state . data }
125138 headerValues = { state . headerValues }
126139 onContinue = { async ( values , rawData , columns ) => {
127- const data = await matchColumnsStepHook ( values , rawData , columns )
128- setState ( {
129- type : StepType . validateData ,
130- data,
131- } )
132- nextStep ( )
140+ try {
141+ const data = await matchColumnsStepHook ( values , rawData , columns )
142+ setState ( {
143+ type : StepType . validateData ,
144+ data,
145+ } )
146+ nextStep ( )
147+ } catch ( e ) {
148+ errorToast ( ( e as Error ) . message )
149+ }
133150 } }
134151 />
135152 )
0 commit comments