Skip to content

Commit

Permalink
Merge pull request #88 from enonic/#29/clean_code_for_guide
Browse files Browse the repository at this point in the history
Boilerplate code from the guide
  • Loading branch information
espen42 committed Nov 2, 2021
2 parents a50e27c + 4397145 commit 1f63a08
Show file tree
Hide file tree
Showing 20 changed files with 4,975 additions and 294 deletions.
7 changes: 6 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"extends": ["next", "next/core-web-vitals"]
"extends": ["next", "next/core-web-vitals"],
"rules": {
// Other rules
"@next/next/no-img-element": "off"
}
}

4 changes: 4 additions & 0 deletions next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
4,224 changes: 4,224 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
{
"name": "next-hmdb",
"version": "0.1.0",
"license": "Apache-2.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"dev": "npx next dev",
"build": "npx next build",
"start": "npx next start",
"lint": "npx next lint"
},
"dependencies": {
"next": "11.0.1",
"next": "^11.1.2",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"devDependencies": {
"@types/react": "17.0.15",
"eslint": "7.31.0",
"eslint-config-next": "11.0.1",
"prettier": "^2.4.1",
"typescript": "4.3.5"
}
}
File renamed without changes.
28 changes: 28 additions & 0 deletions public/images/xp-shield.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
69 changes: 69 additions & 0 deletions src/components/BasePage.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from 'react';

import typeSelector from "../selectors/typeSelector";




/////////// Inline styles

const defaultStyle = { padding: "10px" };
const errorStyle = { padding: "10px", color: "#611" };
const codeDumpStyle = { width: "100%", whiteSpace: "pre-wrap", wordWrap: "break-word" };



/////////// Default view: dump props data to screen

const DefaultPage = (props) => {
return (
<div style={defaultStyle}>
<h1>{props.displayName}</h1>
<h5>Props:</h5>
<pre style={codeDumpStyle}>
{JSON.stringify(props, null, 2)}
</pre>
</div>
)
}



////////////// Error view

const ErrorPage = (errorProps) => {
return (
<div style={errorStyle}>
<h1>Error</h1>
<pre style={codeDumpStyle}>
{JSON.stringify(errorProps, null, 2)}
</pre>
</div>
)
}


////////////// Main entry: select view by error state / content type

const BasePage = ({content, meta={}, error}) => {
if (error) {
return <ErrorPage {...error}/>;
}

if (!content) {
console.warn("No 'content' data in BasePage props");
return null;
}

const {type} = meta;
if (!type) {
console.debug("BasePage props are missing 'meta.type'. Falling back to DefaultPage component.");
}

const typeSelection = typeSelector[type]
const SelectedPage = typeSelection?.page || DefaultPage;

return <SelectedPage {...content} />;
};

export default BasePage;
28 changes: 28 additions & 0 deletions src/enonic-connection-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// XP server domain, incl. <protocol>:// and :<port>. Eg. 'http://localhost:8080' in http://localhost:8080/site/default/draft/mysite/api
const apiDomain = "http://localhost:8080";

// Which site this server communicates with: content item _name for the root site item
const siteName = 'hmdb';

/** URL to the guillotine API */
const contentApiUrl = `${apiDomain}/site/hmdb/draft/${siteName}/api`;

// full app key = appName in gradle.properties in the XP app
const appName = "com.example.myproject";

module.exports = {
apiDomain,
siteName,
contentApiUrl,
appName,

// makes appName variations used in different contexts, eg. queries:
appNameUnderscored: appName.replace(/\./g, '_'),
appNameDashed: appName.replace(/\./g, '-'),

/**
* Prefix the site-relative content path with the XP site _name and returns a full XP _path string.
* @param siteRelativeContentPath {string} The contentPath slug array from [[...contentPath]].tsx, stringified and slash-delimited
* @returns {string} Fully qualified XP content path */
getXpPath: (siteRelativeContentPath) => `/${siteName}/${siteRelativeContentPath}`,
};
Loading

0 comments on commit 1f63a08

Please sign in to comment.