Order document structure alphabetically

This commit is contained in:
Jori Lallo
2018-02-06 23:26:14 -08:00
parent 5588897271
commit b47771c56f
4 changed files with 25 additions and 8 deletions

View File

@ -279,7 +279,7 @@ describe('#documents.create', async () => {
expect(res.status).toEqual(200);
expect(body.data.title).toBe('new document');
expect(body.data.collection.documents.length).toBe(2);
expect(body.data.collection.documents[1].children[0].id).toBe(body.data.id);
expect(body.data.collection.documents[0].children[0].id).toBe(body.data.id);
});
it('should create as a child', async () => {
@ -318,7 +318,7 @@ describe('#documents.update', async () => {
expect(res.status).toEqual(200);
expect(body.data.title).toBe('Updated title');
expect(body.data.text).toBe('Updated text');
expect(body.data.collection.documents[1].title).toBe('Updated title');
expect(body.data.collection.documents[0].title).toBe('Updated title');
});
it('should fallback to a default title', async () => {
@ -338,7 +338,7 @@ describe('#documents.update', async () => {
expect(res.status).toEqual(200);
expect(body.data.title).toBe('Untitled document');
expect(body.data.text).toBe('# Untitled document');
expect(body.data.collection.documents[1].title).toBe('Untitled document');
expect(body.data.collection.documents[0].title).toBe('Untitled document');
});
it('should fail if document lastRevision does not match', async () => {

View File

@ -147,6 +147,8 @@ Collection.prototype.addDocumentToStructure = async function(
};
if (!document.parentDocumentId) {
// Note: Index is supported on DB level but it's being ignored
// by the API presentation until we build product support for it.
this.documentStructure.splice(
index !== undefined ? index : this.documentStructure.length,
0,

View File

@ -362,9 +362,9 @@ export default function Pricing() {
<Method method="documents.move" label="Move document in a collection">
<Description>
Move a document into a new location inside the collection. This is
easily done by defining the parent document ID and optional index.
If no parent document is provided, the document will be moved to
the collection root.
easily done by defining the parent document ID. If no parent
document is provided, the document will be moved to the collection
root.
</Description>
<Arguments>
<Argument
@ -376,7 +376,6 @@ export default function Pricing() {
id="parentDocument"
description="ID of the new parent document (if any)"
/>
<Argument id="index" description="Index of the new location" />
</Arguments>
</Method>

View File

@ -3,6 +3,21 @@ import _ from 'lodash';
import { Collection } from '../models';
import presentDocument from './document';
type Document = {
children: Document[],
id: string,
title: string,
url: string,
};
const sortDocuments = (documents: Document[]) => {
const orderedDocs = _.sortBy(documents, ['title']);
return orderedDocs.map(document => ({
...document,
children: sortDocuments(document.children),
}));
};
async function present(ctx: Object, collection: Collection) {
ctx.cache.set(collection.id, collection);
@ -20,7 +35,8 @@ async function present(ctx: Object, collection: Collection) {
};
if (collection.type === 'atlas') {
data.documents = collection.documentStructure;
// Force alphabetical sorting
data.documents = sortDocuments(collection.documentStructure);
}
if (collection.documents) {