Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/atsistemas/react-base

atSistemas React/Redux Isomorphic Platform
https://github.com/atsistemas/react-base

chai coverage enviroment enzyme immutablejs isomorphic jsdom mocha postcss react reactjs redux universal webpack3

Last synced: about 1 month ago
JSON representation

atSistemas React/Redux Isomorphic Platform

Awesome Lists containing this project

README

        

![React-Base logo](./src/app/assets/images/react-base-logo.png)

# React-Base

![Build-Status](https://travis-ci.org/atSistemas/react-base.svg?branch=master)
![Build-Status](https://ci.appveyor.com/api/projects/status/github/atSistemas/react-base?branch=master&svg=true)
[![Coverage Status](https://coveralls.io/repos/github/atSistemas/react-base/badge.svg?branch=master)](https://coveralls.io/github/atSistemas/react-base?branch=master)
![Npm-Version](https://img.shields.io/badge/npm-6.2.0-blue.svg)
![License](https://img.shields.io/badge/license-MIT-blue.svg)

**A modular platform for Redux Isomorphic applications**

This repository is a modular abstraction to build a [ReactJS](https://facebook.github.io/react/) web application based on [Redux](http://redux.js.org/) paradigm.
You can use it to quickly scaffold your React web application projects and development environments for these projects.

This seed should clarify how to wire up all the modules of your application, even when we understand that in some cases
there must be some changes needed by the structure to fit your needs correctly

## Overview

**React-Base** makes use of the latest tools to improve your workflow, and enables you to create future ready applications:

- [Redux](http://redux.js.org/) based architecture
- Isomorphic / Universal Javascript Apps
- [Webpack 3](https://webpack.github.io/) build configuration depending on enviroment
- Immutable data modeling using [ImmutableJS](https://facebook.github.io/immutable-js/)
- Store middleware to handle request actions.
- Development & Production server using [express](https://github.com/expressjs/express) and [webpack-dev-server](https://webpack.github.io/)
- Hot Reload/Live Reload support for Js & Css using [Webpack HMR](https://webpack.github.io/docs/hot-module-replacement.html)
- Container and component generators using [Yeoman](https://github.com/yeoman/yo)
- JSX and ES6 transpilation using [Babel](https://babeljs.io/)
- [Mocha](https://mochajs.org/) as testing framework
- [Enzyme/JsDom](https://github.com/airbnb/enzyme) for unit/ui testing
- [Nyc](https://github.com/bcoe/nyc) for code coverage
- [PostCSS](http://postcss.org/) processing with isomorphic support.
- [CssModules](https://github.com/css-modules/css-modules) based
- Code Linting using [Eslint](https://github.com/eslint/eslint)
- Css Linting using [CssLint](https://github.com/stylelint/stylelint)
- [Airbnb](https://github.com/airbnb/javascript/tree/master/react) React Style Guide

## Getting Started

To get you started, you need to meet the prerequisites, and then follow the installation instructions.

### Prerequisites

React-Base makes use a number of NodeJS tools to initialize and test React-Base. You must have node.js 6.2.0 at least, and its package manager (npm) installed. You can get it from [nodejs.org](node).

### Installing

You can clone our Git repository:

`$ git clone https://github.com/atSistemas/react-base.git`

This method requires Git to be installed on your computer. You can get it from
[here](http://git-scm.com).

### Wiring up your development environment

Setting up **React-Base** is as easy as running:

`$ npm install`

This command will install all the required dependencies and start your development server, which takes care of all the changes you make to your code and runs all the awesome stuff that ends with your code automagically transpiled and running on your browser.

Please note that `npm install` is only required on your first start, or in case of updated dependencies.

### Initializing development server

Once all the dependencies are installed, you can run `$ npm run start` to initialize your development server using [webpack-dev-server](https://webpack.github.io/) express middleware.

The dev server uses [HMR](https://webpack.github.io/docs/hot-module-replacement.html) (Hot module replacement) that injects updated modules into the bundle in runtime. It's like LiveReload

## Architecture

React-base is based on [Redux](http://redux.js.org/) paradigm so you can find all the typical entities of an Redux project like [reducers](http://redux.js.org/docs/basics/Reducers.html) , [store](http://redux.js.org/docs/basics/Store.html), [actions](http://redux.js.org/docs/basics/Actions.html) , etc.

There are four main folders:

* `server` contains React-Base development & production server based in express with Universal/Isomorphic support and custom middlewares like Gzip.

```javascript
server
api/ //Api mocks
lib/ //Universal rendering files
middleware/ //enviroment middleware
statics/ //definition of statics path
templates/ //universal templates
server //Server
routing //Routing middleware
```

* `webpack` contains React-Base Webpack configuration separated by enviroment that allows to use different plugins and loaders in each target enviroment.

```javascript
webpack
webpack.common.config/ //Common config
webpack.dev.config/ //Development config
webpack.prod.config/ //Production config
webpack.test.config/ //Testing config
webpack.dll.config/ //Dll config
```

* `src/base/` contains React-Base platform bootstrapping code.

```javascript
base
client/ //client bootstrap
conf/ //Configuration files and Yeoman templates
middleware/ //Redux Store middleware
components/ //base components
models/ //model index
reducers/ //reducer index
routes/ //routes index
shared/ // shared base folder
regenerators/ //index regenerators
CreateActionType //Custom action type creator
CreateReducer //Custom reducer creator
ENV //Env handler
Errors //Errors handler
FetchData //Isomorfic data handler
FileSystem //Filesystem manager
JsDomSetup //JsDom Configuration FileSystem
ModelHelper //Inmutable deserializators
Regenerate // Regenerate indexes
store/ //Store configuration and AppState definition
types/ //Action request Types
wp-plugins/ //Custom webpack plugins
...
```

* `src/app/` is the place where to put your application source code.

React-Base uses a "featured based" distribution, so all the necessary code for each page/features is located in its own folder inside containers folder as in `src/app/containers/myContainer`

A container is a React component who contains other components, Redux entities, functions and store subscriptions. Each container is self-contained and represents a feature like "clients" or "products" and it contains all the necessary stuff.
```javascript
app/
containers/
myContainer/
api/ //api calls
actionTypes/ //action types definition
actions/ //action creators
components/ //container components
models/ //containers models using immutable
reducers/ //container reducers
index.ts //container component
...
```

## Action Types
ActionTypes it's a representation using constants of your possible actions:

```javascript
import { createActionType } from 'base';

export const ActionTypes = createActionType([
'CLICK',
'MAIN_CONTAINER',
'MAIN_ERROR',
'MAIN_REQUEST',
'MAIN_SUCCESS',
'LAZY_CONTAINER',
'LOGIN',
]);

```

## Actions
Actions are payloads of information witch represent that something happend in your application and send data from your application to your store:

```javascript
clickHandler(id) {
return {
type: ActionTypes.USER_CLICK,
payload: {
id: id
}
};
}

```

React-Base include a Redux Store middleware to handle actions with service calls more easyly. You can define in the api folder of your container, an api call based in a fetch call:

```javascript

fetchUsers() {
return fetch(url)
.then(req => req.json())
.then(data => data)
.catch(err => err)
},

```

Then, in your action you can attach this service call in your action using the request param:

```javascript
export function getPosts() {
return {
type: ActionTypes.USERS_REQUEST,
request: api.fetchUsers()
};
}
```

The request middleware will resolve the request param
and dispatch a new action with "ACTION_SUCCESS" or "ACTION_ERROR" with the response of the request in the payload.

## Reducers
Reducers describe how the state of your application changes in response to a new Action. React-Base uses a custom CreateReducer that allows to use separated reducers functions instead of "switch based" reducers.

```javascript
import { createReducer } from 'base';

const click = (state, action) => {
return state.update('mainData', (value) => action.payload);
};

const request = (state, action) => {
return state;
};

const actionHandlers = {
[ActionTypes.CLICK]: click,
[ActionTypes.LOGIN]: login,
[ActionTypes.MAIN_REQUEST]: request,
[ActionTypes.MAIN_SUCCESS]: success,
};

export default CreateReducer(actionHandlers, new MainModel());

```

## Models
Represents your model data using ImmutableJS Data Types and sets its initial state using setInitialState() function.

```javascript
import { Record } from 'immutable';

const MainModel = new Record({
display:0,
operator:'',
operation:'',
prevValue: 0,
nextValue: 0,
newValue: false,
resetDisplay: false,
});

function setInitialState(initialState) {
return initialState.Maiin = new MainModel();
}

export { MainModel, setInitialState };

```
### Generating a new container

React-base uses Yeoman to generate new application containers or components.

Fist of all you need to link yo:

`$ npm run yo`

Then, you can generate a new container run:

`$ npm run generate:container`

You'll be asked to provide a name for your container. After that, React-base will create all the necessary folder and file template structures you, and will rebuild the file indexes (routes, reducers, models, etc), so you don't have to worry about including all the required imports.

After that, you can access to your container from http://localhost:8000/myContainer

### Regenerating indexes

You can rebuild the file indexes (reducers, models and routes) running `$ npm run regenerate`

### Generating a new component

As with containers, React-base can automate components creation for you. To create a new component, just type:

`$ npm run generate:component`

Same as before, you will be asked for a component name, and after that React-base will do the rest, placing a component template under `app/components`, and rebuilding all the indexes.

## Distribution

You can generate a complete distribution source ready for production enviroments.

### Building your production application

`$ npm run build:prod` will create a minified version for your application, ready for production.

### Running production server

`$ npm run start:prod` will run production enviroment of your application serving content from dist directory.

## Testing your application

React base uses - [Enzyme](https://github.com/airbnb/enzyme) a testing utillity created by [Airbnb](https://github.com/airbnb/) for unit testing and Ui testing using [Airbnb](https://github.com/tmpvar/jsdom) so you can run your ui testing without a browser.

You can write your tests normally using Mocha and Chai for assertions.

### Running your tests

`$ npm run test` will perform your unit testing, or npm test:coverage to run your tests and display a code coverage report.

### Generating code coverage

React base uses [Nyc](https://github.com/bcoe/nyc) for code coverage and you can generate reports in console or icov/html format.

`$ npm run test` will perform your code coverage, generating an html report located in coverage/ folder.

## Contributing

Anyone and everyone is welcome to contribute, however, if you decide to get involved, please take a moment to review the [guidelines](CONTRIBUTING.md):

* [Bug reports](CONTRIBUTING.md#bugs)
* [Feature requests](CONTRIBUTING.md#features)
* [Pull requests](CONTRIBUTING.md#pull-requests)

## License

**React-Base** is available under the [MIT license](LICENSE).