Skip to content

Commit

Permalink
Merge pull request #495 from safwanyp/fix-5476-user-unable-to-update-…
Browse files Browse the repository at this point in the history
…document-after-array-value-deletion

fixed issue that didn't allow a user to update a document when deleting elements in an array attribute
  • Loading branch information
christyjacob4 authored Aug 11, 2023
2 parents f2b170c + 5ad0349 commit 2a0628a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
11 changes: 11 additions & 0 deletions src/lib/helpers/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,14 @@ export function deepEqual<T>(obj1: T, obj2: T): boolean {

return true;
}

/**
* Creates a deep clone of the given object. This function uses the JSON methods for cloning,
* so it may not be suitable for objects with functions, symbols, or other non-JSON-safe data.
*
* @param obj the object to be cloned
* @returns a deep clone of the provided object
*/
export function deepClone<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,38 @@
import { collection, type Attributes } from '../../store';
import { Container } from '$lib/layout';
import AttributeItem from '../attributeItem.svelte';
import { difference, symmetricDifference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
import { isRelationship, isRelationshipToMany } from '../attributes/store';
import { deepClone } from '$lib/helpers/object';
const databaseId = $page.params.database;
const collectionId = $page.params.collection;
const documentId = $page.params.document;
const editing = true;
const work = writable(
Object.keys($doc)
.filter((key) => {
return ![
'$id',
'$collection',
'$collectionId',
'$databaseId',
'$createdAt',
'$updatedAt'
].includes(key);
})
.reduce((obj, key) => {
obj[key] = $doc[key];
return obj;
}, {}) as Models.Document
);
function initWork() {
const prohibitedKeys = [
'$id',
'$collection',
'$collectionId',
'$databaseId',
'$createdAt',
'$updatedAt'
];
const filteredKeys = Object.keys($doc).filter((key) => {
return !prohibitedKeys.includes(key);
});
const result = filteredKeys.reduce((obj, key) => {
obj[key] = $doc[key];
return obj;
}, {});
return writable(deepClone(result as Models.Document));
}
const work = initWork();
async function updateData() {
try {
Expand Down Expand Up @@ -77,7 +84,7 @@
const docAttribute = $doc?.[attribute.key];
if (attribute.array) {
return !difference(Array.from(workAttribute), Array.from(docAttribute)).length;
return !symmetricDifference(Array.from(workAttribute), Array.from(docAttribute)).length;
}
if (isRelationship(attribute)) {
Expand Down

3 comments on commit 2a0628a

@vercel
Copy link

@vercel vercel bot commented on 2a0628a Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2a0628a Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

console-cloud – ./

console-cloud.vercel.app
console-cloud-appwrite.vercel.app
console-cloud-git-main-appwrite.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 2a0628a Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

console-next – ./

console-next-git-main-appwrite.vercel.app
console-next-appwrite.vercel.app
console-next.vercel.app

Please sign in to comment.