diff --git a/server/api/documents.test.js b/server/api/documents.test.js index 50905057..60751644 100644 --- a/server/api/documents.test.js +++ b/server/api/documents.test.js @@ -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 () => { diff --git a/server/models/Collection.js b/server/models/Collection.js index 2b32e91e..001dd212 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -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, diff --git a/server/pages/Api.js b/server/pages/Api.js index 6a12775f..e6a996bb 100644 --- a/server/pages/Api.js +++ b/server/pages/Api.js @@ -362,9 +362,9 @@ export default function Pricing() { 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. - diff --git a/server/presenters/collection.js b/server/presenters/collection.js index d9f3b05e..89dfe11e 100644 --- a/server/presenters/collection.js +++ b/server/presenters/collection.js @@ -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) {