GithubHelp home page GithubHelp logo

splunkdevabhi / sample-site-react Goto Github PK

View Code? Open in Web Editor NEW

This project forked from acoustic-content-samples/sample-site-react

0.0 1.0 0.0 2.05 MB

Acoustic Content Single Page Application website implemented in React

License: Apache License 2.0

JavaScript 82.58% HTML 0.58% CSS 2.51% SCSS 14.33%

sample-site-react's Introduction

sample-site-react

As part of the Sites feature in Acoustic Content (formerly Watson Content Hub or WCH), a single page application was implemented to serve web sites. The Single Page Application is hosted on WCH and stored as unmanaged assets. It leverages a set of content types, pages, categories, ... to render the Oslo sample application. The default single page application was implemented with Angular. The artifacts stored in WCH for Oslo are independent of the single page application. This sample contains the Single Page Site Application implemented with react instead of the default angular site application. You can find more information about the default site application implemented with angular here: Angular site application. You can replace the default single page application with the react implementations in this repository. Note that this will overlay the default Angular based application with the vue.js or react implementation. See the topic on Restoring the default Angular based application in case you want to switch back. Updates: At the beginning of July we have added the capabilty to use inline editing as well as being able to host the site outside of Watson Content Hub, still leveraging the abilities to manage the content and pages of the site via the site manager. At the beginning of April we have added Search functionality to the React Site SPA and also support now the ability to hide a page from the navigation.

react sample SPA

Getting started

These are the instructions for setting up and deploying the React SPA.

Running Locally

  1. After cloning this repository, run npm install at the root directory.
  2. In src/Constants.js, replace the following two values with the corresponding values from WCH Hub information modal. (User display name > Hub information)
    DOMAIN_NAME: 'your-domain-name.com'
    CONTENT_HUB_ID: '0000000-0000-0000-0000-000000000000'
    
  3. In src/Constants.js, replace the siteID with the corresponding value from Website > YOUR SITE NAME > Detail button(->) > ID. The default siteID is default.
SITE_ID: 'default'
  1. Make sure that CORS is enabled on your Watson Content Hub tenant to allow localhost.
  2. Start the local server:

Layout gallery layout tools

Use the npm run start-dev-tools command to load the developer tools UI. Read more here.

developer tools UI

CLI Layout Generation

Example usage to add CONTENT-TYPE-NAME:

  1. create a CONTENT-TYPE-NAME content type on wch

  2. run npm run create-layout -- --type "CONTENT-TYPE-NAME"

    the npm command simply runs the following:

  • wchtools pull -tlmIv --dir src/wchLayouts which pulls the layout info (layout, layout-mapping, and type json files)
  • ibm-wch-sdk-cli create layout --data src/wchLayouts --scss --react --src . "--type" "CONTENT-TYPE-NAME" which creates the CONTENT-TYPE-NAME layout files (layout and layout-mapping json files, boilerplate jsx template, blank scss file, updated registration.js for registerComponent() calls and updated src/index.js for exporting)
  1. push the layout and layout-mapping json files: wchtools push -tlmIv --dir src/wchLayouts (perhaps I should add this to either the create-layout command or build-deploy, imo it should be in create-layout, but angular repo has the behavior in build-deploy) UPDATE: Added this step to npm run build-deploy for now to make it consistent with angular project

  2. make any customizations to the jsx/scss and then npm run build-deploy

  3. add desired content on wch

Deploying to Content Hub

  1. Install the latest version of wchtools-cli.
    Windows: npm install -g wchtools-cli
    Linux/Mac: sudo npm install -g wchtools-cli
    • Note: make sure that you have initialized wchtools with your user and tenant API URL. You will be prompted for your user password when deploying to the tenant. For more information, refer to wchtools-cli.
  2. In case you updated src/Constants.js with the tenant information for running locally comment the change again.
  3. From the root directory, run npm run build-deploy.

Changing the default (home) route

The default route to load is set to /home. In order to change this:

  1. Open src/app.jsx and find the line of code that specifies the routes in the router:
<Route render={(props) => (<SiteHeader {...props} />)} />
<Route exact path='/' render={() => (<Redirect to="/home"/>)} />
<Route path='/*' component={WchPage} />
<SiteFooter />
  1. Change <Route exact path='/' render={() => (<Redirect to="/home"/>)} /> to redirect to the new route.

Adding inline-editing capabilities

The SPA leverages the higher-order component (HOC) pattern to provide the basic inline editing capabilities. When using the registerComponent function of the SDK the component will be wrapped in the HOC. If a layout component is explicitly called it will need to be wrapped manually, using the following steps:

  1. Import the layoutHOC:

    import { layoutHOC } from 'wch-flux-sdk/react';

  2. Wrap the component

    const DesignArticleSummaryWithLayout = layoutHOC(DesignArticleSummary);

  3. Use the wrapped component in the render function

    return (<DesignArticleSummaryWithLayout contentId={this.props.renderingContext.id}/>);

In addition to using the layoutHOC the layout component will also need to be decorated with data attribute elements to indicate the elements that are editable.

  • The root element of the component should contain a data-renderingcontext-id attribute. This is used to scope the renderingContext of the element to this DOM node. This attribute is required and inline editing will not work without it. The value should the id of the content item and this can be obtained from id property of the renderingContext props.

    	```<div data-renderingcontext-id={this.props.renderingContext.id}>```
    
  • Each editable element will also need to be decorated with a data-wch-inline-edit attribute. The value is the accessor used to access that elements value from the rendering context.

    	```<h1 data-wch-inline-edit="elements.heading.value">{heading}</h1>```
    

Alternate SDK files of note

index.js

Set of javascript functions primarily for calling WCH APIs. Also includes some helper functions for working with images & videos, and fetching content IDs associated with routes, and vice versa. All functions are individually exported to take advantage of tree shaking. Data from WCH is now stored in a centralized store that can be read from different components. This has the advantage of de-coupling the components from the data loading, allowing for things like live updates when data changes.

wchPage.jsx

A React component that gets served by all routes. Before each route gets set, it determines the content ID of the associated page, then sets its template to be the component that corresponds with the content's selected layout, or content type respectively.

wchContent.jsx

A React component extremely similar to that of the wchPage, although not plugged into the router. Takes a content ID as a prop, fetches the corresponding content and sets its template to be the component that matches the content's selected layout or content type respectively.

Check which SPA framework is deployed

To verify if the site is running the React or Vue SPA, load the live site and search the browser console for "SPA framework".

Sample Content type and layout addition

We have shipped another sample you can install on top of this one to install a sample youtube rendering content type, layout and react component to see how you would create your own content types and components. Youtube React component for WCH

Configure WCH and the sample SPA to be used as an external preview host

  1. Get the latest sample react SPA
    • Download latest source code from this branch
  2. configureExternalSPA function in your app;
    1. Edit src/Constants.js file and replace the following two values with the corresponding values from WCH Hub information modal. (User display name > Hub information)
    DOMAIN_NAME: 'your-domain-name.com'
    CONTENT_HUB_ID: '0000000-0000-0000-0000-000000000000'
    
    1. Edit src/app.jsx and uncomment the lines of code:
    // import { Constants } from "./Constants";
    // import { configExternalSPA } from "wch-flux-sdk";
    // configExternalSPA(Constants.DOMAIN_NAME, Constants.CONTENT_HUB_ID);
    
  3. Build the app and deploy it to the external hosting site
    1. Run npm run build
    2. Deploy the dist/assets folder generated by the command to the external host
  4. Add externalPreviewURL and externalURL to default.json
    1. run wchtools init and configure the values to your target tenant
    2. run wchtools pull -s to get the site data
    3. Add these properties to the default.json file, replacing the values with your own:
    "externalPreviewURL": "https://my-external-site-preview.com/",
    "externalURL": "https://my-external-site.com/"
    
    • Note: externalPreviewURL and externalURL require https values. These should be the full link to the root application folder
    1. run wchtools push -s to deploy the updated site data to your tenant
  5. Add the externalPreviewURL and externalURL domains as trusted domains for CORS

Limitations and Disclaimers

  • This sample is intended primarily to demonstrate how the Content Hub delivery APIs can be used with any SPA framework.
  • This SPA sample and the small Flux SDK (https://github.com/acoustic-content-samples/wch-flux-sdk) it uses are available as-is and do not necessarily represent recommended coding best practices. Also note that this sample may not be updated to use the latest versions dependency libraries.
  • The CLI layout generation tool does not support the 'multiple' option for elements.

Restoring the default Angular based application

Clone the Angular site application , trigger a build and push to your tenant to overlay the react site application sample.

sample-site-react's People

Contributors

thomashurek avatar tianchez avatar will-izard-ac avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.