From 0665e28ca39a957bb96dcf2cd8b9f867317f68e4 Mon Sep 17 00:00:00 2001 From: Michael Drury Date: Tue, 3 Nov 2020 17:42:54 +0000 Subject: [PATCH] Updates to pages so that they are written to DB and retrieved correctly. --- .../server/src/api/controllers/application.js | 101 ++++++++++-------- packages/server/src/constants/pages.js | 9 +- packages/server/src/db/utils.js | 16 +-- 3 files changed, 71 insertions(+), 55 deletions(-) diff --git a/packages/server/src/api/controllers/application.js b/packages/server/src/api/controllers/application.js index 0cc9b7730f..811ccabc6d 100644 --- a/packages/server/src/api/controllers/application.js +++ b/packages/server/src/api/controllers/application.js @@ -18,12 +18,14 @@ const { SEPARATOR, getPageParams, generatePageID, + generateScreenID, } = require("../../db/utils") const { downloadExtractComponentLibraries, } = require("../../utilities/createAppPackage") -const PAGES = require("../../constants/pages") +const { MAIN, UNAUTHENTICATED, PageTypes } = require("../../constants/pages") const { HOME_SCREEN } = require("../../constants/screens") +const { cloneDeep } = require("lodash/fp") const APP_PREFIX = DocumentTypes.APP + SEPARATOR @@ -69,17 +71,24 @@ exports.fetch = async function(ctx) { exports.fetchAppPackage = async function(ctx) { const db = new CouchDB(ctx.params.appId) const application = await db.get(ctx.params.appId) - // ctx.body = await getPackageForBuilder(application) - const pages = await db.allDocs( + let pages = await db.allDocs( getPageParams(null, { include_docs: true, }) ) + pages = pages.rows.map(row => row.doc) + const mainPage = pages.filter(page => page.name === PageTypes.MAIN)[0] + const unauthPage = pages.filter( + page => page.name === PageTypes.UNAUTHENTICATED + )[0] ctx.body = { application, - pages, + pages: { + main: mainPage, + unauthenticated: unauthPage, + }, } setBuilderToken(ctx, ctx.params.appId, application.version) @@ -165,15 +174,6 @@ const createEmptyAppPackage = async (ctx, app) => { // 0o777 // ) - // TODO: write the main and unauthenticated JSON to couch - // const writes = [] - // for (let pageName in PAGES) { - // PAGES[pageName]._id = generatePageID() - // writes.push({ - - // }) - // } - await copy(templateFolder, newAppFolder) // this line allows full permission on copied files @@ -201,49 +201,58 @@ const createEmptyAppPackage = async (ctx, app) => { await copy(templatePageDefinitions, join(appsFolder, app._id, "pages")) } - const mainJson = await updateJsonFile( - join(appsFolder, app._id, "pages", "main", "page.json"), - app - ) + // const mainJson = await updateJsonFile( + // join(appsFolder, app._id, "pages", "main", "page.json"), + // app + // ) + // + // mainJson._id = generatePageID() + // await db.put(mainJson) - mainJson._id = generatePageID() - await db.put(mainJson) - - await buildPage(app._id, "main", { - page: mainJson, - screens: await loadScreens(newAppFolder, "main"), - }) - - const unauthenticatedJson = await updateJsonFile( - join(appsFolder, app._id, "pages", "unauthenticated", "page.json"), - app - ) + // const unauthenticatedJson = await updateJsonFile( + // join(appsFolder, app._id, "pages", "unauthenticated", "page.json"), + // app + // ) // Write to couch - unauthenticatedJson._id = generatePageID() - await db.put(unauthenticatedJson) + // unauthenticatedJson._id = generatePageID() + // await db.put(unauthenticatedJson) + const mainPage = cloneDeep(MAIN) + mainPage._id = generatePageID() + mainPage.title = app.name + const unauthPage = cloneDeep(UNAUTHENTICATED) + unauthPage._id = generatePageID() + unauthPage.title = app.name + const homeScreen = cloneDeep(HOME_SCREEN) + homeScreen._id = generateScreenID(mainPage._id) + await db.bulkDocs([mainPage, unauthPage, homeScreen]) + + await buildPage(app._id, "main", { + page: mainPage, + screens: [homeScreen], + }) await buildPage(app._id, "unauthenticated", { - page: unauthenticatedJson, - screens: await loadScreens(newAppFolder, "unauthenticated"), + page: unauthPage, + screens: [], }) return newAppFolder } -const loadScreens = async (appFolder, page) => { - const screensFolder = join(appFolder, "pages", page, "screens") - - const screenFiles = (await fs.readdir(screensFolder)).filter(s => - s.endsWith(".json") - ) - - let screens = [] - for (let file of screenFiles) { - screens.push(await fs.readJSON(join(screensFolder, file))) - } - return screens -} +// const loadScreens = async (appFolder, page) => { +// const screensFolder = join(appFolder, "pages", page, "screens") +// +// const screenFiles = (await fs.readdir(screensFolder)).filter(s => +// s.endsWith(".json") +// ) +// +// let screens = [] +// for (let file of screenFiles) { +// screens.push(await fs.readJSON(join(screensFolder, file))) +// } +// return screens +// } const updateJsonFile = async (filePath, app) => { const json = await readFile(filePath, "utf8") diff --git a/packages/server/src/constants/pages.js b/packages/server/src/constants/pages.js index 72d6d263a6..eb52726341 100644 --- a/packages/server/src/constants/pages.js +++ b/packages/server/src/constants/pages.js @@ -1,8 +1,14 @@ +const PageTypes = { + MAIN: "main", + UNAUTHENTICATED: "unauthenticated", +} + const MAIN = { componentLibraries: ["@budibase/standard-components"], title: "{{ name }}", favicon: "./_shared/favicon.png", stylesheets: [], + name: PageTypes.MAIN, props: { _id: "private-master-root", _component: "@budibase/standard-components/container", @@ -148,6 +154,7 @@ const UNAUTHENTICATED = { title: "{{ name }}", favicon: "./_shared/favicon.png", stylesheets: [], + name: PageTypes.UNAUTHENTICATED, props: { _id: "public-master-root", _component: "@budibase/standard-components/container", @@ -213,4 +220,4 @@ const UNAUTHENTICATED = { uiFunctions: "", } -module.exports = { MAIN, UNAUTHENTICATED } +module.exports = { MAIN, UNAUTHENTICATED, PageTypes } diff --git a/packages/server/src/db/utils.js b/packages/server/src/db/utils.js index a95d6a13be..6c5beee009 100644 --- a/packages/server/src/db/utils.js +++ b/packages/server/src/db/utils.js @@ -177,14 +177,6 @@ exports.generateWebhookID = () => { return `${DocumentTypes.WEBHOOK}${SEPARATOR}${newid()}` } -/** - * Generates a new screen ID. - * @returns {string} The new screen ID which the screen doc can be stored under. - */ -exports.generateScreenID = () => { - return `${DocumentTypes.SCREEN}${SEPARATOR}${newid()}` -} - /** * Generates a new page ID. * @returns {string} The new page ID which the page doc can be stored under. @@ -200,6 +192,14 @@ exports.getPageParams = (pageId = null, otherProps = {}) => { return getDocParams(DocumentTypes.PAGE, pageId, otherProps) } +/** + * Generates a new screen ID. + * @returns {string} The new screen ID which the screen doc can be stored under. + */ +exports.generateScreenID = pageId => { + return `${DocumentTypes.SCREEN}${SEPARATOR}${pageId}${SEPARATOR}${newid()}` +} + /** * Gets parameters for retrieving screens for a particular page, this is a utility function for the getDocParams function. */