Skip to content

Commit 8f4d28c

Browse files
committed
Update CMS UI & API
Keep legacy/outdated actions in a lower section in the UI, just for reference. Add necessary api bits for fetching all collections. Now all buttons except for those in the outdated section work properly.
1 parent 4e91e01 commit 8f4d28c

13 files changed

Lines changed: 89 additions & 27 deletions

File tree

src/cms/api/helpers.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@ const readFileContent = path => {
1717
return readFile(path, { encoding: 'utf-8' })
1818
}
1919

20+
// TODO: find a nicer way for contentModel to serialize for cms
21+
const omitResolvedLinks = (data) => {
22+
return JSON.parse(JSON.stringify(data, (key, value) => {
23+
if (key === '__links') {
24+
return undefined
25+
}
26+
return value
27+
}))
28+
}
29+
2030
module.exports = {
2131
contentRootPath,
22-
readFileContent
32+
readFileContent,
33+
omitResolvedLinks
2334
}

src/cms/api/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const createAPI = (providers) => {
33
settings: require('./models/settings')(providers),
44
fileSystemTree: require('./models/fileSystemTree')(providers),
55
contentModel: require('./models/contentModel')(providers),
6+
collections: require('./models/collections')(providers),
67
categories: require('./models/categories')(providers),
78
category: require('./models/category')(providers),
89
posts: require('./models/posts')(providers),

src/cms/api/models/collections.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { omitResolvedLinks } = require('../helpers')
2+
3+
const createCollectionsModel = ({ getContentModel }) => {
4+
const getCollections = () => {
5+
return omitResolvedLinks(getContentModel().subtree.collections)
6+
}
7+
8+
return {
9+
get: getCollections
10+
}
11+
}
12+
13+
module.exports = createCollectionsModel

src/cms/api/models/contentModel.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1+
const { omitResolvedLinks } = require('../helpers')
2+
13
const createContentModelModel = ({ getContentModel }) => {
24
return {
35
get() {
4-
// TODO: find a nicer way for contentModel to serialize for cms
5-
return JSON.parse(JSON.stringify(getContentModel(), (key, value) => {
6-
if (key === '__links') {
7-
return undefined
8-
}
9-
return value
10-
}))
6+
return omitResolvedLinks(getContentModel())
117
}
128
}
139
}

src/cms/api/models/homepage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { writeFile } = require('fs/promises')
22
const { join } = require('path')
33
const frontMatter = require('front-matter')
4-
const { contentRootPath } = require('../helpers')
4+
const { contentRootPath, omitResolvedLinks } = require('../helpers')
55

66
const helpers = {
77
buildFrontMatter(metadata) {
@@ -37,7 +37,7 @@ const createHomepageModel = ({ getSettings, getContentModel }) => {
3737
}
3838

3939
const getHomepage = (handle) => {
40-
return getContentModel().homepage
40+
return omitResolvedLinks(getContentModel().subtree.homepage)
4141
}
4242

4343
return {

src/cms/api/models/subpage.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { writeFile, mkdir } = require('fs/promises')
22
const { join } = require('path')
33
const frontMatter = require('front-matter')
4-
const { contentRootPath } = require('../helpers')
4+
const { contentRootPath, omitResolvedLinks } = require('../helpers')
55

66
const helpers = {
77
buildFrontMatter(metadata) {
@@ -42,7 +42,8 @@ const createSubpageModel = ({ getSettings, getContentModel }) => {
4242
}
4343

4444
const getSubpage = (title) => {
45-
return getContentModel().subpages.find(p => p.title === title)
45+
const subpages = omitResolvedLinks(getContentModel().subtree.subpages)
46+
return subpages.find(p => p.title === title)
4647
}
4748

4849
return {

src/cms/api/models/subpages.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
const { omitResolvedLinks } = require('../helpers')
2+
13
const createSubpagesModel = ({ getContentModel }) => {
24
const getSubpages = () => {
3-
return getContentModel().subpages
5+
return omitResolvedLinks(getContentModel().subtree.subpages)
46
}
57

68
return {

src/cms/server/public/api.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ const api = {
3333
}
3434
},
3535

36+
collections: {
37+
get: async () => {
38+
const response = await fetch('/api/collections', {
39+
method: 'get',
40+
headers: {
41+
'content-type': 'application/json'
42+
}
43+
})
44+
return response.json()
45+
}
46+
},
47+
3648
categories: {
3749
get: async () => {
3850
const response = await fetch('/api/categories', {
@@ -88,7 +100,7 @@ const api = {
88100
},
89101
body: JSON.stringify(options)
90102
})
91-
return response.json()
103+
return response.text()
92104
}
93105
},
94106

@@ -173,7 +185,7 @@ const api = {
173185
},
174186
body: JSON.stringify(options)
175187
})
176-
return response.json()
188+
return response.text()
177189
}
178190
},
179191

src/cms/server/public/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import getSettings from './app/getSettings.js'
22
import updateSettings from './app/updateSettings.js'
33
import getFileSystemTree from './app/getFileSystemTree.js'
44
import getContentModel from './app/getContentModel.js'
5+
import getCollections from './app/getCollections.js'
56
import getCategories from './app/getCategories.js'
67
import getCategory from './app/getCategory.js'
78
import createCategory from './app/createCategory.js'
@@ -23,6 +24,7 @@ const makeButtonsWork = () => {
2324
query('#update-settings-btn').addEventListener('click', updateSettings)
2425
query('#get-file-system-tree-btn').addEventListener('click', getFileSystemTree)
2526
query('#get-content-model-btn').addEventListener('click', getContentModel)
27+
query('#get-collections-btn').addEventListener('click', getCollections)
2628
query('#get-categories-btn').addEventListener('click', getCategories)
2729
query('#get-category-btn').addEventListener('click', getCategory)
2830
query('#create-category-btn').addEventListener('click', createCategory)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import api from '../api.js'
2+
import dialog from './components/dialog.js'
3+
4+
export default async () => {
5+
dialog.textContent('Loading').show()
6+
7+
const collections = await api.collections.get()
8+
9+
dialog.textContent(JSON.stringify(collections, null, 2))
10+
}

0 commit comments

Comments
 (0)