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

@ovidb/redux-dryer

v1.0.2

Published

Redux utilities to keep your reducers small and DRY

Downloads

3

Readme

Redux-dryer

Coverage Status

This library uses the strategy pattern and removes the boilerplate from your workflow.

What do I mean by that?

Instead of having to write a lot of boilerplate code you basically only have to define your reducers (strategies) for updating your store.

The actions are automatically generated for you by the library when you initialize the reducer.

The library also uses Immer, which makes state updates cleaner and easier to reason about because you can mutate the objects directly instead of having the spread them.

Demo Playground

Installing

npm install @ovidb/redux-dryer

Usage

Generating actions creators and reducer

wallet-dryer.ts

import { ActionPayload as AP, reduxDryer } from 'redux-dryer';

interface WalletState {
  balance: number;
  btc: number;
  loading: boolean;
}

const initialState: WalletState = {
  loading: false,
  balance: 0,
  btc: 0,
};

export type Payloads = {
  SetBalance: number;
  Amount: number;
  SetBTCRate: number;
  Loading: boolean;
};

const { reducer: walletReducer, actions } = reduxDryer({
  initialState,
  namespace: 'wallet',
  reducers: {
    // the name of the action will become the action type
    setBalance: (state, action: AP<Payloads['SetBalance']>) => {
      // Even though this looks like we are mutating we are not because
      // we can update state directly with [Immer](https://github.com/immerjs/immer)
      state.balance = action.payload;
    },
    depositAmount: (state, action: AP<Payloads['Amount']>) => {
      state.balance += action.payload;
    },
    withdrawAmount: (state, action: AP<Payloads['Amount']>) => {
      state.balance -= action.payload;
    },
    convertToBTC: (state, action: AP<Payloads['SetBTCRate']>) => {
      state.btc = state.balance / action.payload;
    },
    setIsLoading: (state, action: AP<Payloads['Loading']>) => {
      state.loading = action.payload;
    },
  },
});

export { walletReducer, actions };

Works with redux-thunk

bitcoin-thunks.ts

import { actions } from './wallet-dryer';
import { AppState } from './reducers';
import { ThunkAction } from 'redux-thunk';
import { Action } from 'redux';

export interface CoinDeskCurrentPriceResponse {
  bpi: { USD: { rate: string } };
}

const getRate = (json: CoinDeskCurrentPriceResponse) =>
  parseFloat(parseInt(json.bpi.USD.rate.split(',').join(''), 10).toFixed(6));

export const toBTC = (): ThunkAction<
  Promise<>,
  AppState,
  number,
  Action<string>
> => async (dispatch, getState) => {
  dispatch(actions.setIsLoading(true));
  fetch('https://api.coindesk.com/v1/bpi/currentprice.json')
    .then(r => r.json())
    .then(json => {
      dispatch(actions.setIsLoading(false));
      dispatch(actions.convertToBTC(getRate(json)));
    });
};

Adding the reducer to root reducer

root-reducer.ts

import { combineReducers } from 'redux';
import { walletReducer } from './wallet-dryer';

const rootReducer = combineReducers({
  wallet: walletReducer,
});

export type AppState = ReturnType<typeof rootReducer>;

export default rootReducer;

That's all you need to do, and now your reducer will listen and respond to your actions

The app

App.tsx

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import rootReducer from './root-reducer';
import Wallet from './wallet-connected';

const store = createStore(reducers, applyMiddleware(...[thunk, logger]));

ReactDOM.render(
  <Provider store={store}>
    <Wallet />
  </Provider>,
  document.getElementById('root')
);

Trigger an action

wallet.tsx

import React from 'react';
import { connect } from 'react-redux';
import { actions, WalletState } from './wallet-dryer';
import { fetchBTCRate } from './bitcoin-thunks';
import { AppState } from './reducers';

const mapStateToProps = ({ wallet }: AppState) => ({
  ...wallet,
});

const mapDispatchToProps = {};

interface Wallet {
  loading: boolean;
  balance: number;
  btc: number;
  actions: ReturnType;
}

export const Wallet: FC<Wallet> = ({ balance, loading, btc, ...actions }) => {
  return (
    <div>
      <div>USD Balance: {balance}</div>
      <div>Bitcoin Balance: {btc}</div>
      <button onClick={() => actions.depositAmount(200)}>+200</button>
      <button onClick={() => actions.withdrawAmount(100)}>-100</button>
      <button onClick={() => actions.setBalance(10000000000)}>
        I want to be Billionaire
      </button>
      <button onClick={() => actions.toBTC(balance)}>To BTC</button>
      {loading && 'Loading...'}
    </div>
  );
};

export default connect(
  ({ wallet, bitcoin }) => ({ wallet, bitcoin }),
  {
    ...actions,
    ...thunks,
  }
)(Wallet);