GithubHelp home page GithubHelp logo

alexseitsinger / redux-action-types Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 2.57 MB

Helpers for redux

License: BSD 2-Clause "Simplified" License

JavaScript 18.79% TypeScript 81.21%
react redux react-redux action-type

redux-action-types's Introduction

Redux Action Types

Helpers for redux

Installation

yarn add @alexseitsinger/redux-action-types

Exports

createActionTypes

Usage
import { createActionTypes } from "@alexseitsinger/redux-action-types"

export default createActionTypes({
  prefix: "dates",
  names: ["dates-for-week", ["dates-for-week", ["success", "failure"]],
})

// returns
{
  "DATES_FOR_WEEK": "@@dates/DATES_FOR_WEEK",
  "SUCCESS": "@@dates/DATES_FOR_WEEK_SUCCESS",
  "FAILURE": "@@dates/DATES_FOR_WEEK_FAILURE",
}

createActionTypeSections

Usage
// home/constants/sections/dates.ts

export default [
  "dates-for-week",
  ["dates-for-week", [
    "success",
    "failure",
  ]],
]

// home/constants/index.ts
import { createActionTypeSections } from "@alexseitsinger/redux-action-types"

import datesConstants from "./sections/dates"

export default createActionTypeSections({
  prefix: "home-page",
  sections: {
    dates: datesActionNames,
  },
})

// returns...

{
  home: {
    dates: {
      DATES_FOR_WEEK: "@@home-page/dates/DATES_FOR_WEEK",
      DATES_FOR_WEEK_SUCCESS: "@@home-page/dates/dates-for-week/SUCCESS",
      DATES_FOR_WEEK_FAILURE: "@@home-page/dates/dates-for-week/FAILURE",
    },
  },
}

createFlatReducer

Usage
// home/reducer/index.ts
import { createFlatReducer } from "@alexseitsinger/redux-action-types"
import actionTypes from "../constants"
import defaultState from "./defaultState.json"

export default createFlatReducer({
  defaultState,
  actionTypes,
  reducer: (action, state, setState) => {
    switch (action.type) {
      case actionTypes.ADD: {
        return setSectionState({
          items: [state.items, action.obj],
        })
      }
    }
    return state
  }
})

createSectionReducer

Usage
// home/reducer/sections/dates.ts
import { AnyAction } from "redux"

import { AnyActionType } from "src/types/actions"
import { PageReducerState, DatesSectionState } from "../../"

type ReducerReturnType = DatesSectionState | PageReducerState

export default (
  action: AnyAction,
  section: AnyActionType,
  sectionState: DatesSectionState,
  setSectionState: (o: Partial<DatesSectionState>) => DatesSectionState,
  parentState: PageReducerState,
  setParentState: (o: Partial<PageReducerState>) => PageReducerState,
): ReducerReturnType => {
  switch (action.type) {
    case section.DATES_FOR_WEEK: {
      return setSectionState({
        datesForWeek: action.data,
      })
    }
  }
}

// home/reducer/index.js
import { createSectionReducer } from "@alexseitsinger/redux-action-types"

import defaultState from "./defaultState.json"
import actionTypeSections from "./constants"
import datesReducer from "./sections/dates"

export default createSectionReducer({
  defaultState,
  sections: actionTypeSections,
  reducers: {
    dates: datesReducer,
  },
})

createMapDispatch

Usage
// home/actions/dates.ts
import actionTypeSections from "../../constants"

const section = actionTypeSections.dates

export const setDatesForWeek = (data: DatesForWeekData): DatesForWeekAction => ({
  type: section.DATES_FOR_WEEK,
  data,
})

const getDatesForWeekSuccess = (): DatesForWeekSuccessAction => ({
  type: section.DATES_FOR_WEEK_SUCCESS,
})

const getDatesForWeekFailure = (): DatesForWeekFailureAction => ({
  type: section.DATES_FOR_WEEK_FAILURE,
})

export const getDatesForWeek = (): ThunkAction => (dispatch: ThunkDispatch): void => {
  // Make some asyncronous API call, then...
  dispatch(getDatesForWeekSuccess())
  dispatch(setDatesForWeek(data))
  // Or...
  dispatch(getDatesForWeekFailure())
}

// home/mapDispatchToProps.ts
import { createMapDispatch } from "@alexseitsinger/redux-action-types"

import * as datesActions from "./actions/dates"

export default createMapDispatch({
  methods: {
    home: {
      dates: datesActions,
    },
  },
  mapDispatch: (dispatch: Dispatch) => ({
    goBack: (): void => {
      dispatch(push("/"))
    },
  }),
})

// returns (in component props)...

{
  methods: {
    home: {
      dates: {
        getDatesForWeek, setDatesForWeek,
      },
    },
    goBack,
  },
}

redux-action-types's People

Contributors

alexseitsinger avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.