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

generator-tfountain-redux-reducer

v0.0.1

Published

A Yeoman generator for @doches' opinionated redux reducers

Downloads

1

Readme

Generator TFountain Redux Reducer

A strongly-opinionated Yeoman generator for scaffolding out a redux reducer (and associated state slice & api calls) based on the way that @doches likes to set things up. Probably not useful for you, but tremendously useful for him.

Usage

Install Yeoman via yarn, or npm, or whatever JS package manager is hot this week:

$ yarn global add yo

Once you've got Yeoman set up, install this generator:

$ yarn global add generator-tfountain-redux-reducer

Move into the directory in your project where you want to create the new redux slice, and invoke Yeoman:

$ cd awesome-project/src/state
$ yo tfountain-redux-reducer

Tell Yeoman about your type name for this reducer, then sit back and bask in the glory of a lovely autogenerated piece of boilerplate that you didn't have to write. Finish your application. Move on with your life. Maybe write that novel you've always been dreaming about, or start learning the drums.

Go on. Live a little.

Output

By way of example, the generator will ask you for singluar (e.g. "item") and plural (e.g. "items") type names, then produce a file like this:

import { Dispatch } from "redux";
import { baseURL, headers } from "./api";

export interface IItemsState {
  fetchPending: boolean;
  fetched: boolean;
  updatePending: boolean;

  items: IItem[];
}

export const DEFAULT_ITEMS_STATE: IItemsState = {
  fetchPending: false,
  fetched: false,
  updatePending: false,
  items: [],
}

export const SET_FETCH_PENDING = "@@items/SET_FETCH_PENDING";
export const SET_UPDATE_PENDING = "@@items/SET_UPDATE_PENDING";
export const SET_ITEMS = "@@items/SET_ITEMS";
export const ADD_ITEM = "@@items/ADD_ITEM";
export const REMOVE_ITEM = "@@items/REMOVE_ITEM";
export const UPDATE_ITEM = "@@items/UPDATE_ITEM";

export function setFetchPending(pending: boolean) {
  return {
      type: SET_FETCH_PENDING,
      pending,
  };
}

export function setUpdatePending(pending: boolean) {
  return {
      type: SET_UPDATE_PENDING,
      pending,
  };
}

export function setItems(items: IItem[]) {
  return {
      type: SET_ITEMS,
      items,
  };
}

export function addItem(item: IItem) {
  return {
      type: ADD_ITEM,
      item,
  };
}

export function removeItem(itemId: number) {
  return {
      type: REMOVE_ITEM,
      id: itemId,
  };
}

function replaceItem(item: IItem) {
  return {
      type: UPDATE_ITEM,
      item,
  };
}

export function createItem(item: IItem) {
  return (dispatch: Dispatch<any>) => {
      dispatch(setUpdatePending(true));
      return fetch(`${baseURL}items/create`, {
          headers,
          method: "POST",
          body: JSON.stringify(item),
      })
      .then((response: Response) => {
          if (!response.ok) {
              throw new Error();
          }
          return response.json();
      })
      .then((created: IItem) => {
        dispatch(addItem(created));
      })
      .catch((error: any) => {
          console.error(error);
      })
      .finally(() => {
          dispatch(setUpdatePending(false));
      });
  }
}

export function listItems() {
  return (dispatch: Dispatch<any>) => {
      dispatch(setFetchPending(true));
      return fetch(`${baseURL}items/list`, {
          headers,
          method: "GET",
      })
      .then((response: Response) => {
          if (!response.ok) {
              throw new Error();
          }
          return response.json();
      })
      .then((items: IItem[]) => {
        dispatch(setItems(items));
      })
      .catch((error: any) => {
          console.error(error);
      })
      .finally(() => {
          dispatch(setFetchPending(false));
      });
  }
}

export function updateItem(item: IItem) {
  return (dispatch: Dispatch<any>) => {
      dispatch(setUpdatePending(true));
      return fetch(`${baseURL}items/update`, {
          headers,
          method: "POST",
          body: JSON.stringify(item),
      })
      .then((response: Response) => {
          if (!response.ok) {
              throw new Error();
          }

          dispatch(replaceItem(item));
      })
      .catch((error: any) => {
          console.error(error);
      })
      .finally(() => {
          dispatch(setUpdatePending(false));
      });
  }
}

export function deleteItem(itemId: number) {
  return (dispatch: Dispatch<any>) => {
      dispatch(setUpdatePending(true));
      return fetch(`${baseURL}items/${itemId}/delete`, {
          headers,
          method: "POST",
      })
      .then((response: Response) => {
          if (!response.ok) {
              throw new Error();
          }
          dispatch(removeItem(itemId));
      })
      .catch((error: any) => {
          console.error(error);
      })
      .finally(() => {
          dispatch(setUpdatePending(false));
      });
  }
}

export function items(state: IItemsState, action: any) {
  if (!state) {
    state = DEFAULT_ITEMS_STATE;
  }

  switch (action.type) {
    default: 
      return state;
    case SET_FETCH_PENDING:
      return {
        ...state,
        fetchPending: action.pending,
      };
    case SET_UPDATE_PENDING:
      return {
        ...state,
        updatePending: action.pending,
    };
    case SET_ITEMS:
      return {
        ...state,
        fetchPending: false,
        fetched: true,
        items: action.items,
      };
    case ADD_ITEM:
      return {
        ...state,
        updatePending: false,
        items: [
          ...state.items,
          action.item,
        ],
      };
    case REMOVE_ITEM:
      return {
        ...state,
        updatePending: false,
        items: state.items.filter((item: IItem) => item.id != action.item.id),
      };
    case UPDATE_ITEM:
      return {
        ...state,
        updatePending: false,
        items: state.items.map((item: IItem) => item.id === action.item.id ? action.item : item),
      };
  }
}