Skip to content

Commit

Permalink
feat: Delete shared files and tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Sep 16, 2022
1 parent 5a26a2c commit 34ec209
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 23 deletions.
4 changes: 2 additions & 2 deletions cypress/e2e/features/files.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ describe('Upload a file modal in Dashboard', () => {
it('Good number of buttons after upload', () => {
cy.get('#ipc-dashboard-upload-file').attachFile(fixtureFile);
cy.get('#ipc-dashboard-upload-file-modal-button').click();
cy.wait(2000);
cy.wait(20000);
cy.get('button').should('have.length', 18);
});

it('Good number of buttons after failed upload', () => {
cy.get('#ipc-dashboard-upload-file').attachFile(emptyFixtureFile, { allowEmpty: true });
cy.get('#ipc-dashboard-upload-file-modal-button').click();
cy.wait(2000);
cy.wait(20000);
cy.get('button').should('have.length', 20);
});
});
Expand Down
4 changes: 2 additions & 2 deletions cypress/e2e/features/programs.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe('Deploy a program modal for Dashboard', () => {
it('Good number of buttons after deployment', () => {
cy.get('#ipc-dashboard-deploy-program').attachFile(fixtureFile);
cy.get('#ipc-dashboard-deploy-program-modal-button').click();
cy.wait(2000);
cy.get('button').should('have.length', 12);
cy.wait(20000);
cy.get('button').should('have.length', 13);
});

it('Good number of buttons after closing modal', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/DriveCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const DriveCards = ({ files, folders }: DriveCardsProps): JSX.Element => {
<MoveFile file={file} />
<RenameFile file={file} concernedFiles={files} />
<UpdateContentFile file={file} />
<DeleteFile file={file} />
<DeleteFile file={file} concernedFiles={files} />
</PopoverContent>
</Portal>
</Popover>
Expand Down
10 changes: 8 additions & 2 deletions src/components/file/DeleteFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ import type { IPCFile } from 'types/types';

import { useDriveContext } from 'contexts/drive';
import { useUserContext } from 'contexts/user';
import { useState } from 'react';

type DeleteFileProps = {
file: IPCFile;
concernedFiles: IPCFile[];
};

const DeleteFile = ({ file }: DeleteFileProps): JSX.Element => {
const DeleteFile = ({ file, concernedFiles }: DeleteFileProps): JSX.Element => {
const { user } = useUserContext();
const { setFiles } = useDriveContext();
const toast = useToast({ duration: 2000, isClosable: true });

const [isLoading, setIsLoading] = useState(false);
const { isOpen, onOpen, onClose } = useDisclosure();

const deleteFile = async () => {
setIsLoading(true);
if (user.account) {
const deleted = await user.drive.delete([file.hash]);

toast({ title: deleted.message, status: deleted.success ? 'success' : 'error' });
if (deleted.success) {
const removed = await user.contact.removeFilesFromContact(user.account.address, [file.id]);
const removed = await user.contact.deleteFiles([file.id], concernedFiles);

if (!removed.success) {
toast({ title: removed.message, status: 'error' });
Expand All @@ -35,6 +39,7 @@ const DeleteFile = ({ file }: DeleteFileProps): JSX.Element => {
} else {
toast({ title: 'Failed to load account', status: 'error' });
}
setIsLoading(false);
onClose();
};

Expand Down Expand Up @@ -66,6 +71,7 @@ const DeleteFile = ({ file }: DeleteFileProps): JSX.Element => {
w="100%"
mb="16px"
onClick={async () => deleteFile()}
isLoading={isLoading}
id="ipc-dashboard-delete-file-button"
>
Delete
Expand Down
4 changes: 2 additions & 2 deletions src/components/folder/DeleteFolder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ const DeleteFolder = ({ folder }: DeleteFolderProps): JSX.Element => {
const filesToDelete = user.drive.files.filter((file) => file.path.startsWith(fullPath));
if (filesToDelete.length > 0) {
const filesResponse = await user.drive.delete(filesToDelete.map((file) => file.hash));
await user.contact.removeFilesFromContact(
user.account.address,
await user.contact.deleteFiles(
filesToDelete.map((file) => file.id),
[],
);
foldersResponse.success = filesResponse.success;
}
Expand Down
41 changes: 27 additions & 14 deletions src/lib/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,17 @@ class Contact {
if (foundFile) {
if (type === 'rename') {
foundFile.name = file.name;
}
if (type === 'update') {
} else if (type === 'update') {
foundFile.hash = file.hash;
foundFile.size = file.size;
const newKey = await encryptWithPublicKey(
c.publicKey.slice(2),
await decryptWithPrivateKey(this.private_key, file.key),
);
foundFile.key = { ...newKey };
} else if (type === 'delete') {
const owner = this.contacts.find((co) => co.address === c.address)!;
owner.files = owner.files.filter((f) => f.id !== fileId);
}
}
});
Expand Down Expand Up @@ -295,7 +297,7 @@ class Contact {
};

this.contacts[index].files.push(newFile);
await this.publishAggregate();
this.publishAggregate();
return { success: true, message: 'File shared with the contact' };
}
return { success: false, message: 'Contact does not exist' };
Expand All @@ -307,20 +309,31 @@ class Contact {
}
}

public async removeFilesFromContact(address: string, ids: string[]): Promise<ResponseType> {
public async deleteFiles(ids: string[], sharedFiles: IPCFile[]): Promise<ResponseType> {
try {
if (this.account) {
const index = this.contacts.findIndex((contact) => contact.address === address);
ids.forEach(async (id) => {
const me = this.contacts.find((c) => c.address === this.account?.address)!;
const file = me.files.find((f) => f.id === id);

if (index !== -1) {
this.contacts[index].files = this.contacts[index].files.filter((f) => !ids.includes(f.id));

await this.publishAggregate();
return { success: true, message: 'File deleted from the contact' };
if (file) {
me.files = me.files.filter((f) => f.id !== id);
} else {
const sharedFile = sharedFiles.find((f) => f.id === id);
if (sharedFile) {
post.Publish({
account: this.account!,
postType: 'InterPlanetaryCloud',
content: { file, tags: ['delete', id] },
channel: 'TEST',
APIServer: DEFAULT_API_V2,
inlineRequested: true,
storageEngine: ItemType.ipfs,
});
}
}
return { success: false, message: 'Contact does not exist' };
}
return { success: false, message: 'Failed to load account' };
});
this.publishAggregate();
return { success: true, message: 'File deleted from the contact' };
} catch (err) {
console.error(err);
return { success: false, message: 'Failed to delete the file from the contact' };
Expand Down

0 comments on commit 34ec209

Please sign in to comment.