Remove collection fetch on document delete

This commit is contained in:
Tom Moor
2022-08-25 10:43:05 +02:00
parent 60309975e0
commit d2aea687f3
6 changed files with 53 additions and 22 deletions

View File

@@ -17,6 +17,7 @@ import {
PartialWithId, PartialWithId,
WebsocketCollectionUpdateIndexEvent, WebsocketCollectionUpdateIndexEvent,
WebsocketCollectionUserEvent, WebsocketCollectionUserEvent,
WebsocketDocumentDeletedEvent,
WebsocketEntitiesEvent, WebsocketEntitiesEvent,
WebsocketEntityDeletedEvent, WebsocketEntityDeletedEvent,
} from "~/types"; } from "~/types";
@@ -247,15 +248,23 @@ class SocketProvider extends React.Component<Props> {
} }
); );
this.socket.on("documents.delete", (event: WebsocketEntityDeletedEvent) => { this.socket.on(
const document = documents.get(event.modelId); "documents.delete",
(event: WebsocketDocumentDeletedEvent) => {
const document = documents.get(event.modelId);
const collection = collections.get(event.collectionId);
if (document) { if (collection) {
document.deletedAt = new Date().toISOString(); collection.removeDocument(event.modelId);
}
if (document) {
document.deletedAt = new Date().toISOString();
}
policies.remove(event.modelId);
} }
);
policies.remove(event.modelId);
});
this.socket.on( this.socket.on(
"documents.permanent_delete", "documents.permanent_delete",

View File

@@ -101,6 +101,12 @@ export default class Collection extends ParanoidModel {
return sortNavigationNodes(this.documents, this.sort); return sortNavigationNodes(this.documents, this.sort);
} }
/**
* Updates the document identified by the given id in the collection in memory.
* Does not update the document in the database.
*
* @param document The document properties stored in the collection
*/
@action @action
updateDocument(document: Pick<Document, "id" | "title" | "url">) { updateDocument(document: Pick<Document, "id" | "title" | "url">) {
const travelNodes = (nodes: NavigationNode[]) => const travelNodes = (nodes: NavigationNode[]) =>
@@ -116,6 +122,27 @@ export default class Collection extends ParanoidModel {
travelNodes(this.documents); travelNodes(this.documents);
} }
/**
* Removes the document identified by the given id from the collection in
* memory. Does not remove the document from the database.
*
* @param documentId The id of the document to remove.
*/
@action
removeDocument(documentId: string) {
this.documents = this.documents.filter(function f(node): boolean {
if (node.id === documentId) {
return false;
}
if (node.children) {
node.children = node.children.filter(f);
}
return true;
});
}
@action @action
updateIndex(index: string) { updateIndex(index: string) {
this.index = index; this.index = index;

View File

@@ -57,7 +57,7 @@ function DataLoader({ match, children }: Props) {
: undefined; : undefined;
const isEditRoute = match.path === matchDocumentEdit; const isEditRoute = match.path === matchDocumentEdit;
const isEditing = isEditRoute || !!auth.team?.collaborativeEditing; const isEditing = isEditRoute || !!auth.team?.collaborativeEditing;
const can = usePolicy(document); const can = usePolicy(document?.id);
const location = useLocation<LocationState>(); const location = useLocation<LocationState>();
React.useEffect(() => { React.useEffect(() => {

View File

@@ -113,8 +113,8 @@ export default abstract class BaseStore<T extends BaseModel> {
}; };
@action @action
remove(id: string): boolean { remove(id: string): void {
return this.data.delete(id); this.data.delete(id);
} }
save( save(

View File

@@ -188,6 +188,11 @@ export type WebsocketEntityDeletedEvent = {
modelId: string; modelId: string;
}; };
export type WebsocketDocumentDeletedEvent = WebsocketEntityDeletedEvent & {
modelId: string;
collectionId: string;
};
export type WebsocketEntitiesEvent = { export type WebsocketEntitiesEvent = {
documentIds: { id: string; updatedAt?: string }[]; documentIds: { id: string; updatedAt?: string }[];
collectionIds: { id: string; updatedAt?: string }[]; collectionIds: { id: string; updatedAt?: string }[];

View File

@@ -64,7 +64,7 @@ export default class WebsocketsProcessor {
return; return;
} }
socketio return socketio
.to( .to(
document.publishedAt document.publishedAt
? `collection-${document.collectionId}` ? `collection-${document.collectionId}`
@@ -72,17 +72,7 @@ export default class WebsocketsProcessor {
) )
.emit(event.name, { .emit(event.name, {
modelId: event.documentId, modelId: event.documentId,
}); collectionId: event.collectionId,
return socketio
.to(`collection-${document.collectionId}`)
.emit("entities", {
event: event.name,
collectionIds: [
{
id: document.collectionId,
},
],
}); });
} }