Work on review and make reordering predictable

This commit is contained in:
Saumya Pandey committed 2021-03-09 14:41:44 +05:30
1 parent b5921be007
commit 3c2b2c8a91
4 files changed
+101 -4

No files matched your search

-2
View File
@@ -38,7 +38,6 @@ class CollectionNew extends React.Component<Props> {
handleSubmit = async (ev: SyntheticEvent<>) => {
ev.preventDefault();
this.isSaving = true;
const { collections } = this.props;
const collection = new Collection(
{
name: this.name,
@@ -46,7 +45,6 @@ class CollectionNew extends React.Component<Props> {
icon: this.icon,
color: this.color,
private: this.private,
index: collections.indexForNew,
},
this.props.collections
);
+1 -1
View File
@@ -42,7 +42,7 @@ export default class CollectionsStore extends BaseStore<Collection> {
return collections.sort((a, b) => {
if (a.index === b.index) {
return 0;
return a.updatedAt > b.updatedAt ? -1 : 1;
}
return a.index < b.index ? -1 : 1;
});
+31 -1
View File
@@ -1,5 +1,6 @@
// @flow
import fs from "fs";
import fractionalIndex from "fractional-index";
import Router from "koa-router";
import { ValidationError } from "../errors";
import { exportCollections } from "../exporter";
@@ -25,6 +26,7 @@ import {
} from "../presenters";
import { Op } from "../sequelize";
import collectionIndexing from "../utils/collectionIndexing";
import removeIndexCollisions from "../utils/removeIndexCollisions";
import { archiveCollection, archiveCollections } from "../utils/zip";
import pagination from "./middlewares/pagination";
@@ -39,8 +41,9 @@ router.post("collections.create", auth(), async (ctx) => {
sharing,
icon,
sort = Collection.DEFAULT_SORT,
index,
} = ctx.body;
let { index } = ctx.body;
const isPrivate = ctx.body.private;
ctx.assertPresent(name, "name is required");
@@ -51,6 +54,29 @@ router.post("collections.create", auth(), async (ctx) => {
const user = ctx.state.user;
authorize(user, "create", Collection);
const collections = await Collection.findAll({
where: { teamId: user.teamId, deletedAt: null },
attributes: ["id", "index", "updatedAt"],
});
if (index) {
const allowedASCII = new RegExp(/^[\x21-\x7E]+$/);
if (!allowedASCII.test(index)) {
throw new ValidationError(
"Index characters must be between x21 to x7E ASCII"
);
}
} else {
collections.sort((a, b) => {
if (a.index === b.index) {
return a.updatedAt > b.updatedAt ? -1 : 1;
}
return a.index < b.index ? -1 : 1;
});
index = fractionalIndex(collections[collections.length - 1].index, null);
}
let collection = await Collection.create({
name,
description,
@@ -64,6 +90,8 @@ router.post("collections.create", auth(), async (ctx) => {
index,
});
removeIndexCollisions(user.teamId, index, collections);
await Event.create({
name: "collections.create",
collectionId: collection.id,
@@ -633,6 +661,8 @@ router.post("collections.move", auth(), async (ctx) => {
await collection.update({ index });
removeIndexCollisions(user.teamId);
await Event.create({
name: "collections.move",
collectionId: collection.id,
+69
View File
@@ -0,0 +1,69 @@
// @flow
import fractionalIndex from "fractional-index";
import { Collection } from "../models";
export default async function removeIndexCollisions(
teamId: string,
index?: string,
collections?: Collection[]
) {
collections = collections
? collections
: await Collection.findAll({
where: { teamId, deletedAt: null }, //no point in maintaining index of deleted collections.
attributes: ["id", "index", "updatedAt"],
});
// use updatedAt because in case of index collision we need to have a predictable order
collections.sort((a, b) => {
if (a.index === b.index) {
return a.updatedAt > b.updatedAt ? -1 : 1;
}
return a.index < b.index ? -1 : 1;
});
collections = collections.map((collection) => {
return [collection, collection.index];
});
// a set to store the index value of collections.
const indexSet = new Set();
if (index) {
indexSet.add(index);
}
let collision = false;
// make the index null, if there is index collision
collections = collections.map((collection) => {
if (indexSet.has(collection[1])) {
collision = true;
collection[1] = null;
return collection;
}
indexSet.add(collection[1]);
return collection;
});
let indexArray = Array.from(indexSet);
indexArray.sort();
if (!collision) {
return;
}
for (const [i, collection] of collections.entries()) {
if (collection[1] === null) {
const previousCollectionIndex = i - 1 < 0 ? null : indexArray[i - 1];
const nextCollectionIndex =
i === indexArray.length ? null : indexArray[i];
const newIndex = fractionalIndex(
previousCollectionIndex,
nextCollectionIndex
);
indexArray.splice(i, 0, newIndex);
await collection[0].update({ index: newIndex });
}
}
}