attachments_ui: Avoid modifying the attachment data sent from server.

We should avoid adding extra fields directly on the server data because
it makes it hard to infer the types for the functions such as
`format_attachment_data`.
This commit is contained in:
Lalit Kumar Singh
2023-08-30 17:17:35 +05:30
committed by Tim Abbott
parent 18b0e58831
commit 02e0537248

View File

@@ -124,11 +124,11 @@ function render_attachments_ui() {
} }
function format_attachment_data(new_attachments) { function format_attachment_data(new_attachments) {
for (const attachment of new_attachments) { return new_attachments.map((attachment) => ({
const time = new Date(attachment.create_time); ...attachment,
attachment.create_time_str = timerender.render_now(time).time_str; create_time_str: timerender.render_now(new Date(attachment.create_time)).time_str,
attachment.size_str = bytes_to_size(attachment.size); size_str: bytes_to_size(attachment.size),
} }));
} }
export function update_attachments(event) { export function update_attachments(event) {
@@ -140,8 +140,7 @@ export function update_attachments(event) {
attachments = attachments.filter((a) => a.id !== event.attachment.id); attachments = attachments.filter((a) => a.id !== event.attachment.id);
} }
if (event.op === "add" || event.op === "update") { if (event.op === "add" || event.op === "update") {
format_attachment_data([event.attachment]); attachments.push(format_attachment_data([event.attachment])[0]);
attachments.push(event.attachment);
} }
upload_space_used = event.upload_space_used; upload_space_used = event.upload_space_used;
// TODO: This is inefficient and we should be able to do some sort // TODO: This is inefficient and we should be able to do some sort
@@ -169,8 +168,7 @@ export function set_up_attachments() {
url: "/json/attachments", url: "/json/attachments",
success(data) { success(data) {
loading.destroy_indicator($("#attachments_loading_indicator")); loading.destroy_indicator($("#attachments_loading_indicator"));
format_attachment_data(data.attachments); attachments = format_attachment_data(data.attachments);
attachments = data.attachments;
upload_space_used = data.upload_space_used; upload_space_used = data.upload_space_used;
render_attachments_ui(); render_attachments_ui();
}, },