11import { useEffect , useRef , useState } from 'react' ;
22import { useDispatch , useSelector } from 'react-redux' ;
33import { FileDetails , ICMSType , INewMigration } from '../../../context/app/app.interface' ;
4- import { fileValidation , getConfig } from '../../../services/api/upload.service' ;
4+ import { fileValidation } from '../../../services/api/upload.service' ;
55import { RootState } from '../../../store' ;
66import { updateNewMigrationData } from '../../../store/slice/migrationDataSlice' ;
77import { Button , CircularLoader , Paragraph } from '@contentstack/venus-components' ;
@@ -73,14 +73,14 @@ const LoadUploadFile = (props: LoadUploadFileProps) => {
7373 const newMigrationDataRef = useRef ( newMigrationData ) ;
7474 const dispatch = useDispatch ( ) ;
7575 const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
76- const [ isValidated , setIsValidated ] = useState < boolean > ( newMigrationData ?. legacy_cms ?. uploadedFile ?. isValidated ) ;
77- const [ showMessage , setShowMessage ] = useState < boolean > ( newMigrationData ?. legacy_cms ?. uploadedFile ?. isValidated ) ;
76+ const [ isValidated , setIsValidated ] = useState < boolean > ( newMigrationDataRef ?. current ?. legacy_cms ?. uploadedFile ?. isValidated ) ;
77+ const [ showMessage , setShowMessage ] = useState < boolean > ( newMigrationDataRef ?. current ?. legacy_cms ?. uploadedFile ?. isValidated ) ;
7878 const [ validationMessgae , setValidationMessage ] = useState < string > ( '' ) ;
7979 const [ isValidationAttempted , setIsValidationAttempted ] = useState < boolean > ( false ) ;
8080 const [ isDisabled , setIsDisabled ] = useState < boolean > ( newMigrationData ?. legacy_cms ?. uploadedFile ?. isValidated || isEmptyString ( newMigrationDataRef ?. current ?. legacy_cms ?. affix ) ) ;
8181 const [ isConfigLoading , setIsConfigLoading ] = useState < boolean > ( false ) ;
8282 const [ cmsType , setCmsType ] = useState ( '' ) ;
83- const [ fileDetails , setFileDetails ] = useState ( newMigrationData ?. legacy_cms ?. uploadedFile ?. file_details ) ;
83+ const [ fileDetails , setFileDetails ] = useState ( newMigrationDataRef ?. current ?. legacy_cms ?. uploadedFile ?. file_details ) ;
8484 const [ fileExtension , setFileExtension ] = useState < string > ( '' ) ;
8585 const [ progressPercentage , setProgressPercentage ] = useState < number > ( 0 ) ;
8686 const [ showProgress , setShowProgress ] = useState < boolean > ( false ) ;
@@ -135,7 +135,8 @@ const LoadUploadFile = (props: LoadUploadFileProps) => {
135135 bucketName : data ?. file_details ?. awsData ?. bucketName ,
136136 buketKey : data ?. file_details ?. awsData ?. buketKey
137137 }
138- }
138+ } ,
139+ cmsType : data ?. cmsType
139140
140141 }
141142 }
@@ -222,63 +223,34 @@ const LoadUploadFile = (props: LoadUploadFileProps) => {
222223 //function to get config details
223224 const getConfigDetails = async ( ) => {
224225 try {
225- setIsConfigLoading ( true ) ;
226- const { data, status} = await getConfig ( ) ;
226+ //setIsConfigLoading(true);
227227
228- if ( ! isEmptyString ( fileDetails ?. localPath ) && data ?. localPath !== fileDetails ?. localPath && ! isEmptyString ( newMigrationDataRef ?. current ?. legacy_cms ?. affix ) ) {
228+ if ( ! isEmptyString ( fileDetails ?. localPath ) && newMigrationData ?. legacy_cms ?. uploadedFile ?. file_details ?. localPath !== fileDetails ?. localPath && ! isEmptyString ( newMigrationDataRef ?. current ?. legacy_cms ?. affix ) ) {
229229 setIsDisabled ( false ) ;
230230 setShowMessage ( true ) ;
231231 setValidationMessage ( '' ) ;
232232
233233 }
234234
235- if ( status === 200 ) {
236- const extension = getFileExtension ( data ?. localPath ) ;
237- setCmsType ( data ?. cmsType ) ;
238- setFileDetails ( data ) ;
235+ const extension = getFileExtension ( newMigrationData ?. legacy_cms ?. uploadedFile ?. file_details ?. localPath || '' ) ;
236+ setCmsType ( newMigrationData ?. legacy_cms ?. uploadedFile ?. cmsType ) ;
237+ //setFileDetails(data);
239238 setFileExtension ( extension ) ;
240239
241- const newMigrationDataObj : INewMigration = {
242- ...newMigrationDataRef ?. current ,
243- legacy_cms : {
244- ...newMigrationDataRef ?. current ?. legacy_cms ,
245- uploadedFile : {
246- name : data ?. localPath ,
247- url : data ?. localPath ,
248- isValidated : newMigrationData ?. legacy_cms ?. uploadedFile ?. isValidated ,
249- reValidate : false ,
250- file_details : {
251- isLocalPath : data ?. isLocalPath ,
252- cmsType : data ?. cmsType ,
253- localPath : data ?. localPath ,
254- awsData : {
255- awsRegion : data ?. awsData ?. awsRegion ,
256- bucketName : data ?. awsData ?. bucketName ,
257- buketKey : data ?. awsData ?. buketKey
258- }
259- }
260- }
261- }
262- } ;
263-
264- if ( isEmptyString ( newMigrationData ?. legacy_cms ?. uploadedFile ?. file_details ?. localPath ) ) {
265- dispatch ( updateNewMigrationData ( newMigrationDataObj ) ) ;
266-
267- }
268240
269241 const { all_cms = [ ] } = migrationData ?. legacyCMSData || { } ;
270242 let filteredCmsData :ICMSType [ ] = all_cms ;
271- if ( data ?. cmsType ) {
272- filteredCmsData = all_cms ?. filter ( ( cms : ICMSType ) => cms ?. parent ?. toLowerCase ( ) === data ?. cmsType ?. toLowerCase ( ) ) ;
243+ if ( newMigrationData ?. legacy_cms ?. uploadedFile ?. cmsType ) {
244+ filteredCmsData = all_cms ?. filter ( ( cms : ICMSType ) => cms ?. parent ?. toLowerCase ( ) === newMigrationData ?. legacy_cms ?. uploadedFile ?. cmsType ?. toLowerCase ( ) ) ;
273245 }
274246
275247 const isFormatValid = filteredCmsData [ 0 ] ?. allowed_file_formats ?. some ( ( format : ICardType ) => {
276248 const isValid = format ?. fileformat_id ?. toLowerCase ( ) === extension ;
277249 return isValid ;
278250 } ) ;
279251
280- //setIsFormatValid(isFormatValid);
281-
252+
253+ //setIsFormatValid(isFormatValid);
282254 setIsDisabled ( ! isFormatValid || isEmptyString ( newMigrationDataRef ?. current ?. legacy_cms ?. affix ) ) ;
283255 if ( ! isFormatValid ) {
284256 setValidationMessage ( '' ) ;
@@ -296,17 +268,17 @@ const LoadUploadFile = (props: LoadUploadFileProps) => {
296268 }
297269
298270
299- }
300- if ( ( ! isEmptyString ( newMigrationData ?. legacy_cms ?. selectedCms ?. parent ?. toLowerCase ( ) ) &&
301- newMigrationData ?. legacy_cms ?. selectedCms ?. parent . toLowerCase ( ) !== data ?. cmsType . toLowerCase ( ) ) )
302- {
303- setIsValidated ( false ) ;
304- setValidationMessage ( 'file format is not appropriate' ) ;
305- setIsValidationAttempted ( true ) ;
306- setShowMessage ( true ) ;
307- setIsLoading ( false ) ;
308- setIsDisabled ( true ) ;
309- }
271+ // }
272+ // if((! isEmptyString(newMigrationData?.legacy_cms?.selectedCms?.parent?.toLowerCase()) &&
273+ // newMigrationData?.legacy_cms?.selectedCms?.parent.toLowerCase() !== data?.cmsType.toLowerCase()))
274+ // {
275+ // setIsValidated(false);
276+ // setValidationMessage('file format is not appropriate');
277+ // setIsValidationAttempted(true);
278+ // setShowMessage(true);
279+ // setIsLoading(false);
280+ // setIsDisabled(true);
281+ // }
310282 setIsConfigLoading ( false ) ;
311283
312284 } catch ( error ) {
@@ -319,10 +291,7 @@ const LoadUploadFile = (props: LoadUploadFileProps) => {
319291 }
320292
321293 useEffect ( ( ) => {
322- if ( isEmptyString ( newMigrationData ?. legacy_cms ?. uploadedFile ?. file_details ?. localPath ) ) {
323- getConfigDetails ( ) ;
324- }
325-
294+ getConfigDetails ( ) ;
326295 } , [ ] ) ;
327296
328297 useEffect ( ( ) => {
@@ -332,7 +301,7 @@ const LoadUploadFile = (props: LoadUploadFileProps) => {
332301 setIsConfigLoading ( savedState . isConfigLoading ) ;
333302 setIsValidated ( savedState ?. isValidated ) ;
334303 setValidationMessage ( savedState ?. validationMessage ) ;
335- setIsDisabled ( savedState ?. isDisabled ) ;
304+ // setIsDisabled(savedState?.isDisabled);
336305 setCmsType ( savedState . cmsType ) ;
337306 //setFileDetails(savedState.fileDetails);
338307 setFileExtension ( savedState . fileExtension ) ;
@@ -392,7 +361,7 @@ const LoadUploadFile = (props: LoadUploadFileProps) => {
392361 if ( newMigrationData ?. legacy_cms ?. uploadedFile ?. reValidate ) {
393362 setValidationMessage ( '' ) ;
394363 }
395- if ( ! isEmptyString ( newMigrationData ?. legacy_cms ?. affix ) && ! newMigrationData ?. legacy_cms ?. uploadedFile ?. isValidated ) {
364+ if ( ! isEmptyString ( newMigrationData ?. legacy_cms ?. affix ) && ! newMigrationData ?. legacy_cms ?. uploadedFile ?. isValidated && ! newMigrationData ?. legacy_cms ?. uploadedFile ?. reValidate ) {
396365 setIsDisabled ( false ) ;
397366 }
398367 setReValidate ( newMigrationData ?. legacy_cms ?. uploadedFile ?. reValidate || false ) ;
@@ -418,8 +387,6 @@ const LoadUploadFile = (props: LoadUploadFileProps) => {
418387 const sanitizedCmsType = cmsType ?. toLowerCase ( ) . replace ( / [ ^ \w \s - ] / g, '' ) ;
419388
420389 const documentationUrl = VALIDATION_DOCUMENTATION_URL ?. [ sanitizedCmsType ] ;
421-
422-
423390
424391 const validationClassName = isValidated ? 'success' : 'error' ;
425392
@@ -468,7 +435,7 @@ const LoadUploadFile = (props: LoadUploadFileProps) => {
468435 isLoading = { isLoading }
469436 loadingColor = "#6c5ce7"
470437 version = "v2"
471- disabled = { isDisabled || isEmptyString ( affix ) || reValidate }
438+ disabled = { ! ( reValidate || ( ! isDisabled && ! isEmptyString ( newMigrationData ?. legacy_cms ?. affix ) ) ) }
472439 >
473440 Validate File
474441 </ Button >
0 commit comments