npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-models

v1.3.1

Published

Models layer for Redux

Downloads

53

Readme

Redux models

Build Test Coverage

Models layer for Redux. redux-models simplifies working with remote data (well.. not only remote) and helps to organize your code.

Installation

npm install --save redux redux-models

Usage

models/User.js
import { createModel } from 'redux-models';

export default createModel({
  name: 'User',
  methods: {
    findByUsername(username) {
      return fetch(`https://api.github.com/users/${username}`).then(res =>
        res.json()
      );
    }
  }
});
store.js
import { combineReducers, applyMiddleware, createStore } from 'redux';
import thunk from 'redux-thunk';
import User from './models/User';

export default createStore(
  combineReducers({
    ...User.reducers
  }),
  applyMiddleware(thunk)
);
app.js
import React from 'react';
import { connect } from 'react-redux';
import User from './models/User';

class UserAvatar extends React.Component {
  componentDidMount() {
    const { fetchUser } = this.props;
    fetchUser();
  }

  render() {
    const { user } = this.props;

    if (!user) {
      return <div>Loading...</div>;
    }

    return <img src={user.avatar_url} alt="avatar" />;
  }
}

export default connect(
  (state, { username }) => ({
    user: User(state).findByUsername(username)
  }),
  (dispatch, { username }) => ({
    fetchUser: () => dispatch(User.findByUsername(username))
  })
)(UserAvatar);

Live demo

API

createModel(options)

Arguments

options:

  • options.name: (String): Name of a model
  • options.mixins: (Array): Array of mixins
  • options.methods: (Object): Model's methods
  • options.reducer: (Function [optional]): Model reducer.
  • options.typePrefix: (String [optional]): Prefix of actions types. Default @@redux-models.
  • options.modelState: (Function [optional]): Function to map state to model state. Default state => state[{ model name }].

Returns

Newly created model with defined methods. Each model method creates action to dispatch.

Model reducer

Additional data processing from the methods can be done in the model reducer.

Model reducer arguments are same as redux reducers, except the last argument types. It contains all action types strings your model can dispatch (including mixins action types). In following example model User has one method find and it can dispatch actions with types: @@redux-models/USER/FIND, @@redux-models/USER/FIND_SUCCESS, @@redux-models/USER/FIND_ERROR, @@redux-models/USER/FIND_RESET, so types contains object:

{
  FIND: '@@redux-models/USER/FIND',
  find: '@@redux-models/USER/FIND',
  FIND_SUCCESS: '@@redux-models/USER/FIND_SUCCESS',
  findSuccess: '@@redux-models/USER/FIND_SUCCESS',
  FIND_ERROR: '@@redux-models/USER/FIND_ERROR',
  findError: '@@redux-models/USER/FIND_ERROR',
  FIND_RESET: '@@redux-models/USER/FIND_RESET',
  findReset: '@@redux-models/USER/FIND_RESET'
}

After processing, the data will be available in state.{ model name }.model.

Example

import { createModel } from 'redux-models';

const defaultState = {
  byId: {}
};

export default createModel({
  name: 'User',
  methods: {
    find(query) {
      // async request
    }
  },
  reducer(state = defaultState, action, { findSuccess }) {
    const { type, payload: { result } = {} } = action;

    if (type === findSuccess) {
      return {
        ...state,
        byId: {
          ...state.byId,
          ...(result || []).reduce(
            (byId, user) => ({
              ...byId,
              [user.id]: user
            }),
            {}
          )
        }
      };
    }

    return state;
  }
});

Then:

import { connect } from 'react-redux';
// ...

export default connect((state, { id }) => ({
  user: state.User.model.byId[id]
}))(UserCard);

Mixins

Mixins allow you to add method sets to multiple models. For example mixin crud adds methods: create, updateById, deleteById, find, findById.

crud.js
import createMixin from 'redux-models-mixin-crud';

export default function crudMixin(path) {
  return createMixin({ 
    methods: {
      create() { /*...*/ },
      updateById() { /*...*/ },
      deleteById() { /*...*/ },
      find() { /*...*/ },
      findById() { /*...*/ }
    } 
  });
}
book.js
import { createModel } from 'redux-models';
import crudMixin from './crud';

export default createModel({
  name: 'Book',
  mixins: [crudMixin('/books')]
});

Contributing

See the Contributors Guide

License MIT