Andriy-Kulak / art-arc

art - arc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

arclogo

Latest release Build Status Coverage Status Gitter chat

Universal Redux

This branch adds Server Side Rendering to the redux branch.

See the demo. (Disable javascript to see the magic)

Download

Just clone the repository and remove the .git folder:

$ git clone -b universal-redux https://github.com/diegohaz/arc my-app
$ cd my-app
$ rm -rf .git
$ npm install # or yarn

Usage

Run

Once you have installed the dependencies, you can use npm run dev to run a development server.

Deploy

Use npm run build to transpile the code into the dist folder. Then, you can deploy it everywhere.

Example on Heroku using Heroku CLI:

# start a new local git repository
git init

# create a new heroku app
heroku apps:create my-new-app

# add heroku remote reference to the local repository
heroku git:remote --app my-new-app

# commit and push the files
git add -A
git commit -m "Initial commit"
git push heroku master

# open the deployed app in the browser
heroku open

The second time you deploy, you just need to:

git add -A
git commit -m "Update code"
git push heroku master

Source code

The source code should be placed in src; public/static files should be placed in public so they can be included in the build process.

Because of webpack's config, we can import our source modules without relative paths.

import { Button, HomePage } from 'components' // src/components
import App from 'components/App' // src/components/App
import routes from 'routes' // src/routes

Clean source code

If you want to start with a clean and minimal source code without the predefined components and tests, just use the src-clean folder instead by renaming it to src (and removing or renaming the older one to something like src-example).

Also, you might want to remove unnecessary dependencies:

npm u -S react-modal # used by src/components/molecules/Modal
npm u -S normalizr # used by src/store/entities

Components

This project leverages the Atomic Design methodology to create a scalable and easy to maintain component folder structure. See why.

However, Atomic Design should be a solution, not another problem. If you want to create a component and don't know where to put it (atoms, molecules, organisms etc.), do not worry, do not think too much, just put it anywhere. After you realize what it is, just move the component folder to the right place. Everything else should work.

This is possible because all components are dynamically exported on src/components/index.js and imported in a way that Atomic Design structure doesn't matter:

import { Button, Hero, HomePage, PageTemplate } from 'components'

To better understand the Atomic Design methodology, you can refer to the src/components folder here and the Pattern Lab Demo, which this project is based on. Basically, you can think this way:

  • An atom is a native html tag or a React Component that renders an html tag (e.g Input);
  • A molecule is a group of atoms (e.g. Field);
  • An organism is a group of atoms, molecules and/or other organisms (e.g. Form);
  • A page is... a page, where you will put mostly organisms (e.g. HomePage);
  • A template is a layout to be used on pages, see why templates are good practice.

Storybook

I highly recommend you to incorporate react-storybook on your development process. It really improves productivity and the developer experience. Actually, most of the time you can just use the storybook instead of the real webapp while creating components.

This already comes with the boilerplate and you can simply use npm run storybook to get it running. But, if you don't want that, just run:

rm -rf .storybook # remove .storybook folder
npm u -S @kadira/storybook # remove storybook dependency

Containers

This project uses a very straight approach of Redux: all components should be as pure as possible and should be placed in the components folder.

If, for some reason, you need to connect a component to the store, just create a container with the same name, import the pure component and connect it. Thus having a nice separation of concerns. Do not add any extra styles or another presentational logic on containers.

You can refer to this thread on Twitter:

Dan Abramov Tweet

Example:

src/components/organisms/PostList

// just presentational logic
import React, { PropTypes } from 'react'
import styled from 'styled-components'

import { Post } from 'components'

const PostList = ({ list, loading, ...props }) => {
  return (
    <div {...props}>
      {loading && <div>Loading</div>}
      {list.map((post, i) => <Post key={i} {...post} />)}
    </div>
  )
}

PostList.propTypes = {
  list: PropTypes.array.isRequired,
  loading: PropTypes.bool
}

export default PostList

src/containers/PostList

import React, { PropTypes, Component } from 'react'
import { connect } from 'react-redux'
import { fromPost, fromStatus } from 'store/selectors'
import { postList, POST_LIST } from 'store/actions'

import { PostList } from 'components'

class PostListContainer extends Component {
  componentDidMount () {
    this.props.request()
  }

  render () {
    const { list, loading } = this.props
    return <PostList {...{ list, loading }} />
  }
}

const mapStateToProps = (state) => ({
  list: fromPost.getList(state),
  loading: fromStatus.isLoading(state, POST_LIST)
})

const mapDispatchToProps = (dispatch, { limit }) => ({
  request: () => dispatch(postList.request(limit))
})

export default connect(mapStateToProps, mapDispatchToProps)(PostListContainer)

src/components/elsewhere

import { PostList } from 'containers'

<PostList limit={15} />

This approach makes it easier to transform any pure component into a container at any time.

Store

Here lives all the state management of the app.

  • actions are the messages dispatched throughout the application to perform state changes. Learn more;
  • reducer listens to the actions and translates the state changes to the store. Learn more;
  • selectors are used by the application to get parts of the current state. Learn more;
  • sagas listen to the actions and are responsible for performing side effects, like data fetching, caching etc. Learn more.

To add a new store, just create a new folder with actions, reducer, selectors and/or sagas. Webpack will automatically import them to your project (how? See src/store/actions.js, src/store/reducer.js, src/store/sagas.js and src/store/selectors.js).

Store naming conventions

The store on this boilerplate follows some naming conventions. You don't need to follow them, but it will work better if you do.

  • actions should start with the store name (e.g. MODAL_OPEN for modal store, POST_LIST_REQUEST for post store) and end with REQUEST, SUCCESS or FAILURE if this is an async operation;
  • action creators should have the same name of their respective actions, but in camelCase (e.g. modalOpen). Async actions should group request, success and failure in a object (e.g. postList.request, postList.success, postList.failure);
  • worker sagas should start with the operation name (e.g. openModal, requestPostList).

Universal

component &&
component[method] &&
promises.push(component[method]({ req, res, params, location, store }))

This code is present in src/server.js and it will call Component.method() for the requested Page container, where method is the name of the HTTP method used in the request (get, post etc.).

import React, { Component } from 'react'
import submit from 'redux-form-submit'
import { postList } from 'store'

import { SamplePage } from 'components'
import { config } from './PostForm'

class SamplePageContainer extends Component {
  // called when POST /sample-page
  static post ({ req, store }) {
    return Promise.all([
      this.get({ store }),
      store.dispatch(submit(config, req.body))
    ])
  }

  // called when GET /sample-page
  static get ({ store }) {
    return new Promise((resolve, reject) => {
      store.dispatch(postList.request(15, resolve, reject))
    })
  }

  render () {
    return <SamplePage />
  }
}

export default SamplePageContainer

In order to make the forms work on the server side, this is combined with redux-form and redux-form-submit.

Contributing

When submitting an issue, use the following patterns in the title for better understanding:

[v0.3.1-redux] Something wrong is not right # the v0.3.1 release of the redux branch
[redux] Something wrong is not right # the actual code of the redux branch
Something wrong is right # general, related to master or not directly related to any branch

PRs are very appreciated. For bugs/features consider creating an issue before sending a PR. But there are other things you can contribute directly:

  • I'm not a native english speaker. If you find any typo or some text that could be written in a better way, please send a PR, even if it is only a punctuation;
  • If you forked or created another boilerplate based on this one with another features (using css-modules instead of styled-components, for example), add that to the Forks section with the following pattern:

Built with ARc

Built something cool with ARc? Send a PR adding it to this list:

Contributors

Thanks goes to these wonderful people (emoji key):

Prabhat Sharma
Prabhat Sharma

πŸ’»
Sven Schmidt
Sven Schmidt

πŸ› πŸ’»
Sebastian
Sebastian

⚠️
Steven Haddix
Steven Haddix

πŸ’»
Ruslan Kyba
Ruslan Kyba

πŸ› πŸ’» πŸ“–
Abhishek Shende
Abhishek Shende

πŸ’»
Gueorgui Agapov
Gueorgui Agapov

πŸ’»
Santino
Santino

πŸ’»
Sebastian MacDonald
Sebastian MacDonald

πŸ’» ⚠️
Ryan Garant
Ryan Garant

πŸ’»
Dennis Bochen
Dennis Bochen

πŸ“–

This project follows the all-contributors specification. Contributions of any kind welcome!

License

The MIT License (MIT)

Copyright (c) 2016 Diego Haz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

art - arc


Languages

Language:JavaScript 100.0%