generated from vtex-apps/admin-example
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into loc/13293
- Loading branch information
Showing
23 changed files
with
228 additions
and
24 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
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 |
Oops, something went wrong.