Skip to content

Commit

Permalink
Deleting removed documents
Browse files Browse the repository at this point in the history
  • Loading branch information
eperedo committed Nov 20, 2023
1 parent 6d6d584 commit 34b8988
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 45 deletions.
31 changes: 18 additions & 13 deletions src/components/steps/attach-files/AttachFiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const AttachFiles: React.FC<AttachFilesProps> = props => {
href: base64,
url: undefined,
sharing: undefined,
markAsDeleted: false,
});
const prevDocuments = [...documents];
const newDocuments = [...prevDocuments, { ...newFile, base64 }];
Expand All @@ -40,7 +41,12 @@ export const AttachFiles: React.FC<AttachFilesProps> = props => {
};

const onDeleteDocument = (index: number) => {
const documentsWithoutDeleted = documents.filter((_, docIndex) => docIndex !== index);
const documentsWithoutDeleted = documents.map((document, docIndex) => {
return ProjectDocument.create({
...document,
markAsDeleted: document.markAsDeleted ? document.markAsDeleted : docIndex === index,
});
});
onChange(documentsWithoutDeleted);
setDocuments(documentsWithoutDeleted);
};
Expand All @@ -52,15 +58,14 @@ export const AttachFiles: React.FC<AttachFilesProps> = props => {
const newDocuments = documents.map((document, docIndex) => {
return docIndex !== index
? document
: {
...ProjectDocument.create({
...document,
name: file.name,
blob: file,
sizeInBytes: file.size,
href: base64,
}),
};
: ProjectDocument.create({
...document,
name: file.name,
blob: file,
sizeInBytes: file.size,
href: base64,
markAsDeleted: false,
});
});
onChange(newDocuments);
setDocuments(newDocuments);
Expand All @@ -81,7 +86,7 @@ export const AttachFiles: React.FC<AttachFilesProps> = props => {
)}
{documents.map((document, index) => {
return (
<DocumentItemContainer key={index}>
<DocumentItemContainer key={index} deleted={document.markAsDeleted}>
<DocumentDropZoneContainer>
<DropFiles
onDrop={files => updateDocument(files, index)}
Expand Down Expand Up @@ -133,9 +138,9 @@ const DocumentsItemsContainer = styled.div`
gap: 1em;
`;

const DocumentItemContainer = styled.div`
const DocumentItemContainer = styled.div<{ deleted: boolean }>`
align-items: center;
display: flex;
display: ${props => (props.deleted ? "none" : "flex")};
gap: 2em;
`;

Expand Down
5 changes: 4 additions & 1 deletion src/models/ProjectDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ export default class ProjectDb {
projectDocumentsWithSharing
);

await this.saveMERData(orgUnit.id, projectDocumentsSaved).getData();
await this.saveMERData(
orgUnit.id,
projectDocumentsSaved.filter(document => !document.markAsDeleted)
).getData();

const response = await postPayload(this.api, payload, this.project);
const savedProject = response && response.status === "OK" ? projectUpdated : this.project;
Expand Down
3 changes: 3 additions & 0 deletions src/models/ProjectDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type ProjectAttrs = {
href: DocumentUrl | undefined;
blob: Maybe<Blob>;
sharing: Maybe<Sharing>;
markAsDeleted: boolean;
};

export class ProjectDocument {
Expand All @@ -28,6 +29,7 @@ export class ProjectDocument {
public readonly sizeInBytes: number;
public readonly blob: Maybe<Blob>;
public readonly sharing: Maybe<Sharing>;
public readonly markAsDeleted: boolean;

private constructor(data: ProjectAttrs) {
this.id = data.id;
Expand All @@ -37,6 +39,7 @@ export class ProjectDocument {
this.sizeInBytes = data.sizeInBytes;
this.blob = data.blob;
this.sharing = data.sharing;
this.markAsDeleted = data.markAsDeleted;
}

static create(data: ProjectDocument): ProjectDocument {
Expand Down
77 changes: 48 additions & 29 deletions src/models/ProjectDocumentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { promiseMap } from "../migrations/utils";
import { D2Api } from "../types/d2-api";
import { getUid } from "../utils/dhis2";
import { Id, ProjectDocument } from "./ProjectDocument";
import { EntityAccess } from "./Sharing";

export class ProjectDocumentRepository {
private api: D2Api;
Expand Down Expand Up @@ -34,6 +35,7 @@ export class ProjectDocumentRepository {
href: `${this.api.apiPath}/documents/${d2Document.id}/data`,
sizeInBytes: 0,
sharing: undefined,
markAsDeleted: false,
});
});

Expand All @@ -55,45 +57,62 @@ export class ProjectDocumentRepository {
}

async saveAll(documents: ProjectDocument[]): Promise<ProjectDocument[]> {
const documentsSaved = await promiseMap(documents, async projectDocument => {
const docsToDelete = documents.filter(document => document.markAsDeleted);
const docsToSave = documents.filter(document => !document.markAsDeleted);

const documentsToDelete = await promiseMap(docsToDelete, async projectDocument => {
if (!projectDocument.id) return projectDocument;
await this.postDocuments({ id: projectDocument.id }, "DELETE");
return projectDocument;
});

const documentsSaved = await promiseMap(docsToSave, async projectDocument => {
if (!projectDocument.blob) return projectDocument;

const fileResponse = await this.api.files
.saveFileResource({
data: projectDocument.blob,
name: projectDocument.name,
})
.saveFileResource({ data: projectDocument.blob, name: projectDocument.name })
.getData();

const newDocumentId = projectDocument.id || getUid("project-documents", fileResponse);

const response = await this.api.metadata
.post({
documents: [
{
id: newDocumentId,
url: fileResponse,
name: projectDocument.name,
userAccesses: projectDocument.sharing?.userAccesses,
userGroupAccesses: projectDocument.sharing?.userGroupAccesses,
},
],
})
.getData();

if (response.status === "ERROR") {
console.log(JSON.stringify(response.typeReports, null, 4));
throw Error(`Error uploading document: ${response.status}`);
}
return {
...projectDocument,
id: newDocumentId,
url: fileResponse,
};
await this.postDocuments(
{
id: newDocumentId,
url: fileResponse,
name: projectDocument.name,
userAccesses: projectDocument.sharing?.userAccesses,
userGroupAccesses: projectDocument.sharing?.userGroupAccesses,
},
"CREATE_AND_UPDATE"
);
return { ...projectDocument, id: newDocumentId, url: fileResponse };
});
return documentsSaved;

return [...documentsSaved, ...documentsToDelete];
}

private async postDocuments(
projectDocument: D2Document,
strategy: "DELETE" | "CREATE_AND_UPDATE"
) {
const response = await this.api.metadata
.post({ documents: [projectDocument] }, { importStrategy: strategy })
.getData();
if (response.status === "ERROR") {
console.log(JSON.stringify(response.typeReports, null, 4));
throw Error(`Error uploading document: ${response.status}`);
}
}
}

type D2FileResource = {
contentLength: string;
};

type D2Document = {
id: string;
url?: string;
name?: string;
userAccesses?: EntityAccess[];
userGroupAccesses?: EntityAccess[];
};
6 changes: 4 additions & 2 deletions src/models/ProjectInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export class ProjectInfo {
};
});

const newDocuments = project.documents.filter(document => !document.url);
const newDocuments = project.documents.filter(
document => !document.url && !document.markAsDeleted
);

const newActions = newDocuments.map((document): ProjectInfoNode => {
return {
Expand All @@ -99,7 +101,7 @@ export class ProjectInfo {
}

private getDocumentAction(newDocument: Maybe<ProjectDocument>, isUpdated: boolean): Action {
if (!newDocument) {
if (!newDocument || newDocument.markAsDeleted) {
return "removed";
} else if (newDocument.url && isUpdated) {
return "updated";
Expand Down

0 comments on commit 34b8988

Please sign in to comment.