-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement a soft delete with a bin (#106)
Co-authored-by: Reza Rahemtola <[email protected]>
- Loading branch information
1 parent
aa606cb
commit 434c5eb
Showing
12 changed files
with
272 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import {Button, Text, useColorModeValue, useDisclosure, useToast} from "@chakra-ui/react"; | ||
import {useState} from "react"; | ||
import {IPCFile, IPCFolder} from "../../types/types"; | ||
import Modal from "../Modal"; | ||
import {useUserContext} from "../../contexts/user"; | ||
import {useDriveContext} from "../../contexts/drive"; | ||
|
||
type DeleteBinProps = { | ||
files: IPCFile[]; | ||
folders: IPCFolder[]; | ||
concernedFiles: IPCFile[]; | ||
}; | ||
|
||
const DeleteBin = ({ files, folders, concernedFiles }: DeleteBinProps): JSX.Element => { | ||
const colorText = useColorModeValue('gray.800', 'white'); | ||
const toast = useToast({ duration: 2000, isClosable: true }); | ||
|
||
const { user } = useUserContext(); | ||
const { setFiles } = useDriveContext() | ||
|
||
const [isLoading, setIsLoading] = useState(false); | ||
const { isOpen, onOpen, onClose } = useDisclosure(); | ||
|
||
const deleteAllFiles = async () => { | ||
setIsLoading(true); | ||
if (user.account) { | ||
const deleted = await user.drive.delete(files.map((file) => file.hash)); | ||
if (deleted.success) { | ||
const removed = await user.contact.deleteFiles(files.map((file) => file.id), concernedFiles); | ||
|
||
if (!removed.success) { | ||
toast({ title: removed.message, status: 'error' }); | ||
} else { | ||
setFiles(user.drive.files); | ||
toast( { title: "All the files in your bin have been deleted.", status: 'success' }) | ||
} | ||
} | ||
} else { | ||
toast({ title: 'Failed to load account', status: 'error' }); | ||
} | ||
setIsLoading(false); | ||
onClose(); | ||
} | ||
|
||
if (files.length === 0 && folders.length === 0) { | ||
return ( | ||
<Text fontSize="24" textColor={colorText}> | ||
Your bin is empty | ||
</Text> | ||
) | ||
} | ||
|
||
return ( | ||
<> | ||
<Button | ||
variant="inline" | ||
onClick={() => onOpen()} | ||
> | ||
Delete all files | ||
</Button> | ||
<Modal | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
title="Delete the bin" | ||
CTA={ | ||
<Button | ||
variant="inline" | ||
w="100%" | ||
mb="16px" | ||
onClick={async () => deleteAllFiles()} | ||
isLoading={isLoading} | ||
id="ipc-dashboard-delete-bin-button" | ||
> | ||
Delete | ||
</Button> | ||
} | ||
> | ||
<Text>Are you sure you want to delete all the files in your bin?</Text> | ||
</Modal> | ||
</> | ||
) | ||
} | ||
|
||
export default DeleteBin |
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,65 @@ | ||
import { Button, HStack, PopoverFooter, useColorModeValue, useToast } from '@chakra-ui/react'; | ||
import {FcRedo} from 'react-icons/fc'; | ||
|
||
import type { IPCFile } from 'types/types'; | ||
|
||
import { useConfigContext } from 'contexts/config'; | ||
import { useDriveContext } from 'contexts/drive'; | ||
import { useUserContext } from 'contexts/user'; | ||
import { useState } from 'react'; | ||
|
||
type DeleteFileProps = { | ||
file: IPCFile; | ||
concernedFiles: IPCFile[]; | ||
}; | ||
|
||
const DeleteFile = ({ file, concernedFiles }: DeleteFileProps): JSX.Element => { | ||
const { user } = useUserContext(); | ||
const { files, setFiles } = useDriveContext(); | ||
const toast = useToast({ duration: 2000, isClosable: true }); | ||
const { config } = useConfigContext(); | ||
const colorText = useColorModeValue('gray.800', 'white'); | ||
|
||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const restoreFile = async () => { | ||
setIsLoading(true); | ||
if (user.account) { | ||
const moved = await user.contact.moveFileToBin(file, null, concernedFiles) | ||
toast({ title: moved.message, status: moved.success ? 'success' : 'error' }); | ||
|
||
const index = files.indexOf(file); | ||
if (index !== -1) { | ||
files[index].deletedAt = null; | ||
setFiles([...files]); | ||
} | ||
} else { | ||
toast({ title: 'Failed to load account', status: 'error' }); | ||
} | ||
setIsLoading(false); | ||
}; | ||
|
||
if (!['owner', 'editor'].includes(file.permission)) return <></>; | ||
|
||
return ( | ||
<PopoverFooter> | ||
<HStack> | ||
<FcRedo size="30"></FcRedo> | ||
<Button | ||
backgroundColor={config?.theme ?? 'white'} | ||
textColor={colorText} | ||
w="100%" | ||
p="0px" | ||
mx="4px" | ||
onClick={async () => restoreFile()} | ||
isLoading={isLoading} | ||
id="ipc-dashboard-restore-file-button" | ||
> | ||
Restore | ||
</Button> | ||
</HStack> | ||
</PopoverFooter> | ||
); | ||
}; | ||
|
||
export default DeleteFile; |
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
Oops, something went wrong.