generated from vtex-apps/admin-example
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add async validation on uploading file (#150)
* feat: Update bulk-import-ui Signed-off-by: Arthur Andrade <[email protected]> * feat: Add useValidateBulkImport Signed-off-by: Arthur Andrade <[email protected]> * fix: refresh interval time Signed-off-by: Arthur Andrade <[email protected]> * feat: Add Uploading Screen async Validation Signed-off-by: Arthur Andrade <[email protected]> * fix: Import List Data filter Signed-off-by: Arthur Andrade <[email protected]> * chore: Add Changelog Signed-off-by: Arthur Andrade <[email protected]> * chore: Add Changelog Signed-off-by: Arthur Andrade <[email protected]> * fix: fix useBulkImportDetailsQuery onSuccess prop Signed-off-by: Arthur Andrade <[email protected]> * fix: Try fix ci/cd Signed-off-by: Arthur Andrade <[email protected]> * fix: Try fix ci/cd Signed-off-by: Arthur Andrade <[email protected]> * ci: update quality-egineering version Signed-off-by: Arthur Andrade <[email protected]> --------- Signed-off-by: Arthur Andrade <[email protected]>
- Loading branch information
1 parent
318d56d
commit 7a083d6
Showing
22 changed files
with
224 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { useState } from 'react' | ||
import { UploadingScreen as BulkImportUploadingScreen } from '@vtex/bulk-import-ui' | ||
|
||
import type { UploadFileData } from '../../types/BulkImport' | ||
import ValidatingScreen from './ValidatingScreen' | ||
import useValidateBulkImport from '../../hooks/useValidateBulkImport' | ||
|
||
export type UploadingScreenProps = { | ||
name: string | ||
size: number | ||
uploadFile: () => Promise<UploadFileData> | ||
onUploadFinished: (data: UploadFileData) => void | ||
} | ||
|
||
export type UploadingStep = 'UPLOADING' | 'VALIDATING' | ||
|
||
const UploadingScreen = ({ | ||
uploadFile, | ||
onUploadFinished: onUploadFinishedProp, | ||
...otherProps | ||
}: UploadingScreenProps) => { | ||
const [step, setStep] = useState<UploadingStep>('UPLOADING') | ||
|
||
const [importId, setImportId] = useState<string | undefined>(undefined) | ||
|
||
const { startBulkImportValidation } = useValidateBulkImport({ | ||
onSuccess: () => { | ||
setStep('VALIDATING') | ||
}, | ||
}) | ||
|
||
const onUploadFinished = (data: UploadFileData) => { | ||
if (data.status === 'error') { | ||
onUploadFinishedProp(data) | ||
|
||
return | ||
} | ||
|
||
startBulkImportValidation({ importId: data?.data?.fileData?.importId }) | ||
setImportId(data?.data?.fileData?.importId) | ||
} | ||
|
||
return step === 'UPLOADING' ? ( | ||
<BulkImportUploadingScreen | ||
{...otherProps} | ||
uploadFile={uploadFile} | ||
onUploadFinished={onUploadFinished} | ||
/> | ||
) : ( | ||
<ValidatingScreen | ||
{...otherProps} | ||
importId={importId} | ||
onUploadFinished={onUploadFinishedProp} | ||
/> | ||
) | ||
} | ||
|
||
export default UploadingScreen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React from 'react' | ||
import { Flex, Spinner, Text, csx } from '@vtex/admin-ui' | ||
|
||
import { useTranslate } from '../../hooks' | ||
import { bytesToSize } from '../utils/bytesToSize' | ||
import type { UploadFileData } from '../../types/BulkImport' | ||
import useBulkImportDetailsQuery from '../../hooks/useBulkImportDetailsQuery' | ||
|
||
export type ValidatingScreenProps = { | ||
importId?: string | ||
name: string | ||
size: number | ||
onUploadFinished: (data: UploadFileData) => void | ||
} | ||
|
||
const ValidatingScreen = ({ | ||
name, | ||
size, | ||
importId, | ||
onUploadFinished, | ||
}: ValidatingScreenProps) => { | ||
const { translate: t } = useTranslate() | ||
|
||
useBulkImportDetailsQuery({ | ||
importId, | ||
refreshInterval: 30 * 1000, | ||
onSuccess: data => { | ||
if (data.importState === 'ReadyToImport') { | ||
onUploadFinished({ | ||
status: 'success', | ||
data: { | ||
fileData: { | ||
...data, | ||
percentage: data.percentage.toString(), | ||
}, | ||
}, | ||
}) | ||
|
||
return | ||
} | ||
|
||
onUploadFinished({ | ||
status: 'error', | ||
showReport: data?.importState === 'ValidationFailed', | ||
data: { | ||
error: 'FieldValidationError', | ||
errorDownloadLink: data?.validationResult?.reportDownloadLink ?? '', | ||
validationResult: data?.validationResult?.validationResult ?? [], | ||
fileName: data.fileName, | ||
}, | ||
}) | ||
}, | ||
}) | ||
|
||
return ( | ||
<Flex | ||
className={csx({ backgroundColor: '$gray05', height: '100%' })} | ||
align="center" | ||
direction="column" | ||
justify="center" | ||
> | ||
<Spinner className={csx({ color: '$blue40' })} size={120} /> | ||
<Text | ||
className={csx({ marginBottom: '$space-3', marginTop: '$space-10' })} | ||
variant="pageTitle" | ||
> | ||
{t('uploading')} | ||
</Text> | ||
<div> | ||
<Text>{name}</Text> | ||
<Text tone="secondary"> | ||
{' · '} | ||
{bytesToSize(size)} | ||
</Text> | ||
</div> | ||
</Flex> | ||
) | ||
} | ||
|
||
export default ValidatingScreen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as ValidationScreen } from './UploadingScreen' | ||
export type { UploadingScreenProps } from './UploadingScreen' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const bytesToSize = (bytes: number) => { | ||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'] | ||
|
||
if (bytes === 0) return '0 Bytes' | ||
const i = Math.floor(Math.log(bytes) / Math.log(1024)) | ||
|
||
if (i === 0) return `${bytes} ${sizes[i]}` | ||
|
||
return `${Math.round(bytes / 1024 ** i)}${sizes[i]}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import useSWRMutation from 'swr/mutation' | ||
|
||
import { validateBulkImport } from '../services' | ||
|
||
const useValidateBulkImport = ({ onSuccess }: { onSuccess?: () => void }) => { | ||
const { trigger } = useSWRMutation( | ||
'/buyer-orgs/start', | ||
(_, { arg }: { arg: { importId: string } }) => | ||
validateBulkImport(arg.importId), | ||
{ | ||
onSuccess, | ||
} | ||
) | ||
|
||
return { startBulkImportValidation: trigger } | ||
} | ||
|
||
export default useValidateBulkImport |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import bulkImportClient from '.' | ||
|
||
const validateBulkImport = async (importId?: string): Promise<unknown> => { | ||
return bulkImportClient.post(`/buyer-orgs/validate/${importId}`) | ||
} | ||
|
||
export default validateBulkImport |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.