-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Janderson Souza Matias
authored and
Janderson Souza Matias
committed
Feb 2, 2024
1 parent
493636a
commit 0491dc0
Showing
5 changed files
with
197 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import RegionService, { RegionBatchResponse } from '@/services/region'; | ||
import { | ||
Box, | ||
Button, | ||
HStack, | ||
Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalOverlay, | ||
Spinner, | ||
Text, | ||
VStack, | ||
} from '@chakra-ui/react'; | ||
import Papa from 'papaparse'; | ||
import { useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
type Props = { | ||
isOpen: boolean; | ||
onClose: (reload?: boolean) => void; | ||
}; | ||
|
||
const RegionImportModal: React.FC<Props> = ({ isOpen, onClose }) => { | ||
const { t } = useTranslation(); | ||
const [header, setHeader] = useState<string[]>([]); | ||
const [arrayData, setArrayData] = useState<string[][]>([]); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [response, setResponse] = useState<RegionBatchResponse>(); | ||
|
||
const handleFileUpload = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const file = event.target.files?.[0]; | ||
if (file) { | ||
Papa.parse<string[]>(file, { | ||
header: false, | ||
complete: function (results) { | ||
const [header, ...data] = results.data; | ||
|
||
setArrayData(data); | ||
setHeader(header); | ||
}, | ||
}); | ||
} | ||
}; | ||
|
||
const startImport = async () => { | ||
setIsLoading(true); | ||
const response = await RegionService.createRegionBatch(arrayData); | ||
setResponse(response); | ||
setIsLoading(false); | ||
}; | ||
|
||
const onCancel = () => { | ||
onClose(); | ||
setArrayData([]); | ||
setIsLoading(false); | ||
setResponse(undefined); | ||
}; | ||
|
||
const onFinish = () => { | ||
onClose(); | ||
setArrayData([]); | ||
setIsLoading(false); | ||
setResponse(undefined); | ||
}; | ||
|
||
return ( | ||
<Modal isOpen={isOpen} onClose={onClose} scrollBehavior="inside" size="5xl"> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<ModalHeader>{t('settings.tabs.region.import-title')}</ModalHeader> | ||
<ModalCloseButton /> | ||
{isLoading ? ( | ||
<ModalBody> | ||
<Spinner /> | ||
</ModalBody> | ||
) : response ? ( | ||
<ModalBody> | ||
<VStack w="full"> | ||
<Text fontSize={28} mb={8}> | ||
{!!response.failItems?.length ? 'Items that failed to import' : 'All items was imported'} | ||
</Text> | ||
<HStack w="full" px={4} borderBottom="1px solid"> | ||
<Box fontWeight="bold" w="50px"></Box> | ||
{header.map((_, index) => ( | ||
<Box flex={index === 1 ? 2 : 1} fontWeight="black"> | ||
{index === 0 ? 'Code' : index === 1 ? 'Name' : 'Region-' + (index - 2)} | ||
</Box> | ||
))} | ||
</HStack> | ||
{response?.failItems?.map((row, index) => ( | ||
<HStack w="full" bg={index % 2 ? '#f5f5f5' : '#ffffff'} px={4}> | ||
<Box fontWeight="bold" w="50px"> | ||
{index + 1 + '.'} | ||
</Box> | ||
{row.map((item: string, itemIndex) => ( | ||
<Box flex={itemIndex === 1 ? 2 : 1}> {item}</Box> | ||
))} | ||
</HStack> | ||
))} | ||
</VStack> | ||
</ModalBody> | ||
) : ( | ||
<ModalBody> | ||
{arrayData.length === 0 ? ( | ||
<input type="file" accept=".csv" onChange={handleFileUpload} /> | ||
) : ( | ||
<VStack w="full"> | ||
<HStack w="full" px={4} borderBottom="1px solid"> | ||
<Box fontWeight="bold" w="50px"></Box> | ||
{header.map((_, index) => ( | ||
<Box flex={index === 1 ? 2 : 1} fontWeight="black"> | ||
{'Region-' + index} | ||
</Box> | ||
))} | ||
</HStack> | ||
{arrayData.map((row, index) => ( | ||
<HStack w="full" bg={index % 2 ? '#f5f5f5' : '#ffffff'} px={4}> | ||
<Box fontWeight="bold" w="50px"> | ||
{index + 1 + '.'} | ||
</Box> | ||
{row.map((item: string, itemIndex) => ( | ||
<Box flex={itemIndex === 1 ? 2 : 1}> {item}</Box> | ||
))} | ||
</HStack> | ||
))} | ||
</VStack> | ||
)} | ||
</ModalBody> | ||
)} | ||
|
||
<ModalFooter> | ||
{!!arrayData.length && <Text mr="auto">{'Total items: ' + arrayData.length}</Text>} | ||
{!response && ( | ||
<Button variant="ghost" onClick={onCancel} isDisabled={isLoading}> | ||
{t('common.cancel')} | ||
</Button> | ||
)} | ||
{!isLoading && !response && ( | ||
<Button colorScheme="blue" mr={3} isDisabled={!arrayData.length} onClick={startImport}> | ||
{t('common.import')} | ||
</Button> | ||
)} | ||
|
||
{!!response && ( | ||
<Button colorScheme="blue" mr={3} isDisabled={!arrayData.length} onClick={onFinish}> | ||
Finish | ||
</Button> | ||
)} | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default RegionImportModal; |
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 |
---|---|---|
@@ -1,12 +1,19 @@ | ||
import _axios from '..'; | ||
import { IRegion } from '../../types'; | ||
|
||
export type RegionBatchResponse = { | ||
successCount?: number; | ||
failItems?: string[][]; | ||
}; | ||
|
||
export const RegionService = { | ||
deleteRegion: async (regionId: string): Promise<IRegion[]> => (await _axios.delete(`region/${regionId}`)).data, | ||
getRegions: async (): Promise<IRegion[]> => (await _axios.get('region')).data, | ||
getRegionsTree: async (): Promise<IRegion[]> => (await _axios.get('region/tree')).data, | ||
getRegion: async (id: string): Promise<IRegion> => (await _axios.get(`region/${id}`)).data, | ||
saveRegion: async (region: Partial<IRegion>): Promise<IRegion> => (await _axios.post('region', region)).data, | ||
createRegionBatch: async (batch: string[][]): Promise<RegionBatchResponse> => | ||
(await _axios.post(`region/batch`, { batch })).data, | ||
}; | ||
|
||
export default RegionService; |