5.1.2 • Published 11 months ago

relient-admin v5.1.2

Weekly downloads
40
License
MIT
Repository
github
Last release
11 months ago

Relient

Relient is an util set working closely with Relient-CLI. It will be useful if your project is built on React, Redux and of course Relient-CLI.

Install

$ npm install relient --save

TOC

Redux utils

Relient provides action, middleware, reducer utils to help you handle API request and merge the payload. Check the API document

Create action

actions/account.js

import { createAction, actionTypeCreator, post, read, put, del } from 'relient/actions';

const actionType = actionTypeCreator(__filename);

export const READ_ALL = actionType('READ_ALL');
export const CREATE = actionType('CREATE');
export const UPDATE = actionType('UPDATE');
export const REMOVE = actionType('REMOVE');

export const readAll = createAction(
  READ_ALL,
  () => read('/account/all'),
);

export const create = createAction(
  CREATE,
  ({ username, password }) => post('/account', { username, password }),
);

export const update = createAction(
  UPDATE,
  ({ id, username, password }) => put(`/account/${id}`, { username, password }),
);

export const remove = createAction(REMOVE, ({ id }) => del(`/account/${id}`));

Create reducer

reducers/account.js

import { merge, remove, handleActions, combineActions } from 'relient/reducers';
import { account } from '../schema';
import { UPDATE, READ_ALL, CREATE, REMOVE } from '../actions/account';

export default {
  account: handleActions({
    [combineActions(
      UPDATE,
      CREATE,
    )]: merge({ schema: account }),

    [READ_ALL]: merge({ schema: account }),

    [REMOVE]: remove(account),

  }, {}),
};

reducers/index.ts

import { createEntitiesReducer, history, serverError } from 'relient/reducers';
import { combineReducers } from 'redux';
import { account } from './account'

export default combineReducers({
  ...createEntitiesReducer([ account ]),
  history,
  serverError,
});

Create store

import { fetch, history, serverError } from 'relient/middleware'
import { createStore, applyMiddleware } from 'redux';
import fetchInstance from 'isomorphic-fetch/fetch-npm-browserify' // or 'isomorphic-fetch/fetch-npm-node' in server side
import reducers from './reducers';

const initialState = {};

export default createStore(
  reducers,
  initialState,
  applyMiddleware({
    fetch({
      fetch: fetchInstance,
      apiDomain: 'https://your-api-domain',
      getDefaultHeader: ({ getState, withoutAuth }) => {
        // Return your default header object
      },
    })
    serverError({
      onUnauthorized: ({ payload, dispatch, getState }) => {
        // Handle 401 and 403 error
      },
      onGlobalWarning: ({ payload, dispatch, getState }) => {
        // Handle global API error
      }
    }),
  }),
);

Config

Relient config provide consistent configuration between server side and client side based on node-config.

You need to inject the global configs in client side:

html.js

import React from 'react';
import getClientConfig from 'relient/config/client-config';

export default () => (
  <html className="no-js" lang="en">
    <head>
    	...
    </head>
    <body>
      <script dangerouslySetInnerHTML={{ __html: getClientConfig(['slogan']) }} />
      ...
    </body>
  </html>
)

Then you can use getConfig in your code:

header.js

import getConfig from 'relient/config';

export default () => (
  <div>{getConfig('slogan')}</div>
)

Formatters

Relient provides common formatters to display date, time, price and percentage. Check the API document

import { date, time, price, percentage } from 'relient/formatters'

const originalDate = new Date('2018-01-01');
const formattedDate = date('YYYY-M-D')(originalDate); // 2018-1-1

const originalTime = new Date('2018-01-01 01:01:01');
const formattedDate = date('YYYY-M-D H:m:s')(originalTime); // 2018-1-1 1:1:1

const originalPrice = 100;
const formattedPrice = price()(originalPrice); // 100.00

const originalPercentage = 0.2;
const formattedPercentage = percentage()(originalPercentage); // 20%

I18N

Relient provides a simple i18n mechanism, which compatible with React: Check the API document

import React from 'react';
import i18n from 'relient/i18n';

const messages = {
  title: 'The total mount is {total}, and the top one is {top}',
}

export default () => (
  <div>{i18n(messages)('total', { total: 1, top: <b>Alex</b> })}</div>
)

DOM

Relient provides a simple head elements maintenance utils: Check the API document

import { updateMeta, updateCustomMeta, updateLink } from 'relient/dom';

updateMeta('description', 'web site description');
updateCustomMeta('og:url', 'canonical url');
updateLink('canonical', 'canonical url');
5.1.2

11 months ago

5.1.1

11 months ago

5.1.0

11 months ago

5.0.12

11 months ago

5.0.11

1 year ago

5.0.9

2 years ago

5.0.8

2 years ago

5.0.7

2 years ago

5.0.6

2 years ago

5.0.4

2 years ago

5.0.3

2 years ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.10

2 years ago

5.0.0

2 years ago

4.8.5

2 years ago

4.8.4

2 years ago

4.8.1

2 years ago

4.8.3

2 years ago

4.8.2

2 years ago

4.7.1

2 years ago

4.8.0

2 years ago

4.6.0

3 years ago

4.5.0

3 years ago

4.7.0

3 years ago

4.5.2

3 years ago

4.5.1

3 years ago

4.4.2

3 years ago

4.4.1

3 years ago

4.4.0

3 years ago

4.3.1

3 years ago

4.3.0

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.0.11

3 years ago

4.1.0

3 years ago

4.1.2

3 years ago

4.1.1

3 years ago

4.0.10

3 years ago

4.0.9

3 years ago

4.0.8

3 years ago

4.0.5

3 years ago

4.0.4

3 years ago

4.0.7

3 years ago

4.0.6

3 years ago

4.0.3

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.5.0

3 years ago

3.4.0

3 years ago

3.4.1

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.1

3 years ago

3.2.0

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.3

3 years ago

3.0.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.16

4 years ago

0.0.17

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago