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

relike-redux-middleware

v0.1.6

Published

Redux middleware for interacting with the ReLike universal liking service.

Downloads

18

Readme

ReLikeMiddleware

npm version

Redux middleware for interfacing with ReLike, the decentralized public liking service, powered by Ethereum.

Documentation

This middleware uses ReLikeUtils under the hood to send transactions to the ReLike smart contract on Ethereum. Please see documentation for ReLikeUtils on GitHub.

Installation

To install the middleware, simply import it and add it to your list of middlewares.

import { applyMiddleware, createStore } from 'redux';
import ReLikeMiddleware from 'relike-redux-middleware';

import rootReducer from '../reducers/index';

const store = createStore(
  rootReducer,
  applyMiddleware(ReLikeMiddleware),
);

The middleware will dispatch actions that are prefixed with @@RELIKE/.

Meta Actions

This library exports a set of "meta actions" that you will use to trigger the middleware to cause a transaction on the ReLike service and dispatch a series of start and success/error actions.

Here's an example meta action, which uses Date.now() to add a timestamp when it creates the action, so you can identify it later:

const action = {
  type: '@@RELIKE/LIKE',
  payload: {
    entityId: 'ReLike',
    timestamp: 1501802668772,
  },
}

The middleware swallows the meta action and dispatches a "start" action, like so:

const startAction = {
  type: '@@RELIKE/LIKE_START',
  payload: {
    entityId: 'ReLike',
    timestamp: 1501802668772,
  },
};

This is followed by a success or error action, depending on if the transaction is successful or not:

const successAction = {
  type: '@@RELIKE/LIKE_SUCCESS',
  meta: {
    entityId: 'ReLike',
    timestamp: 1501802668772,
  },
  payload: {
    result: { /* transaction object */ },
  },
};

// or

const errorAction = {
  error: true,
  type: '@@RELIKE/LIKE_ERROR',
  meta: {
    entityId: 'ReLike',
    timestamp: 1501802668772,
  },
  payload: { /* Error object */ },
};

Passing actions to components

These meta actions can be passed to components via a connected container, in idiomatic Redux style:

// MyContainer.js
import { connect } from 'react-redux';
import { ReLikeMetaActions } from 'relike-redux-middleware;'

import MyComponent from '../components/MyComponent';

const mapDispatchToProps = {
  dislike: ReLikeMetaActions.dislike,
  getLikeCount: ReLikeMetaActions.getLikeCount,
  getMyRating: ReLikeMetaActions.getMyRating,
  like: ReLikeMetaActions.like,
  unDislike: ReLikeMetaActions.unDislike,
  unLike: ReLikeMetaActions.unLike,
};

export default connect(null, mapDispatchToProps)(MyComponent);

For a full list of meta actions please see ReLikeMetaActions.js.

Handling the actions dispatched by the middelware

To handle the individual actions in your reducers, This library exports a ReLikeActionTypes object. You can import this into your reducers and manipulate your state as you see fit.

import { Map } from 'immutable';
import { ReLikeActionTypes } from 'relike-redux-middleware';

export default function pendingLikes(state = Map(), action) {
  switch (action.type) {
    case ReLikeActionTypes.LIKE_START:
      return state.set(action.payload.entityId, true);
    case ReLikeActionTypes.LIKE_ERROR:
    case ReLikeActionTypes.LIKE_SUCCESS:
      return state.set(action.payload.entityId, false);
    default:
      return state;
  }
}

For a full list of action types please see actionTypes.js.

Listening for events

When the middleware is instantiated, it will start listening and dispatch actions for two kinds of events:

  1. The primary account of the user has been switched:

    const accountChangedEvent = {
      type: '@@RELIKE/ACCOUNT_CHANGED_EVENT',
      payload: {
        newAccount: 0x0123456789abcdef0123456789abcdef01234567,
      },
    };
  2. A new "like" has been placed on the service:

    const newLikeEvent = {
      type: '@@RELIKE/NEW_LIKE_EVENT',
      payload: {
        dislikes: 2,
        entityId: 'ReLike',
        likes: 7,
        rating: 1,
        user: 0x0123456789abcdef0123456789abcdef01234567,
      },
    };
    

    The possible ratings are:

    0 = UNRATED
    1 = LIKE
    2 = DISLIKE