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-ac-creator

v1.0.10

Published

The purpose of this package is to provide a uniform-interface across the app and an easy way to handle dispatching actions in redux, especially the ones that are asynchronous and/or include side-effects like making API requests. Nevertheless, it is NOT ma

Downloads

3

Readme

Redux Action Creator

The purpose of this package is to provide a uniform-interface across the app and an easy way to handle dispatching actions in redux, especially the ones that are asynchronous and/or include side-effects like making API requests. Nevertheless, it is NOT mandatory to make API requests if you don't want to. In fact, almost all the properties that are set in the dataObject are optional, except for the "dispatch" method and the "actionType". Yes, you are right ! you can use this package to also dispatch plain synchronous actions. The other properties on the dataObject are there to use at will, if you need them of course.

This actionCreator utility is designed to handle even the most complex and intricate of redux actions' scenarios or architectures as well as nested and chained actions.

You can dispatch multiple actions or even multiple action-creators within a single action-creator.

This action creator utility handles setting the state in the redux store automatically so that you don't need to worry about it; this is handled when you pass the "actionType" that you have specified in the reducer.

IMPORTANT : Make sure to use thunk middleware in your redux store config

SOON !

  • I will provide a better documentation as well as clearer explanation of how to best utilize this toolkit in your projects.
  • I will link a video tutorial on how to use this tool.
  • More features will be added as well.

Example:

Complete Example

import axios from "axios";
import { useDispatch } from "react-redux";
import { actionCreator, mutationCreator } from "redux-ac-creator";
const handleApiSuccess = props => {};
const handleApiErrors = e => {};
const dispatch = useDispatch();

// action creator with API calls

const dataObject = {
	axios,
	handleApiSuccess,
	handleApiErrors,
	method: ``,
	url: ``,
	config: {},
	body: {},
	actionType: "",
	payload: {},
	beforeFnSync: props => {},
	beforeFnAsync: async props => {},
	afterFnSync: props => {},
	afterFnAsync: async props => {}
};
const exampleFunction = async () => {
	const response = await dispatch(actionCreator(dataObject));
};


// pure action with NO API calls
const dataObject = {
	handleApiErrors,
	actionType: "",
	dispatch, // pass the redux dispatch function
	payload: {},
	message: "",
	fn: () => {/* define/call any sync function you want */}
};

const result = mutationCreator(dataObject)

Imports

import axios from "axios";
import { useDispatch } from "react-redux";
import { Kit } from "redux-ac-creator";

/*
handleApiSuccess()
handleApiErrors()

you can either:

1. Define them headers
2. Import them from another module
3. Omit them both and not include
	 any of them in the dataObject
*/

// if defined or imported

const handleApiSuccess = props => {
	console.log(props); // return or do what you want
};
const handleApiErrors = e => {
	console.log(e); // return or do what you want
};

dataObject

// Add the properties that you need ONLY

const dataObject = {
	axios,
	handleApiSuccess,
	handleApiErrors,
	// axios related data:
	method: "get" /* any valid http method; eg. "get" "post" */,
	url: "/route",
	config: {
		/* add any valid axios config you like */
		headers: { "Content-Type": "application/json" }
	},
	body: {} /* body object to send with the request */,
	// redux action related data:
	actionType: "",
	payload: {},
	/*
	  payload to be fed to any of the functions below
	  NOT action payload
	*/
	// functions to be executed if defined here
	beforeFnSync: props => {
		// synchronous function that runs Before the API call
		// its return is "res1"
		/*
			has access to all the props
			defined in the dataObject
		 */
		console.log(props); // to see all props
	},
	beforeFnAsync: async props => {
		// asynchronous function that runs Before the API call
		// its return is "res2"
		/*
			has access to all the props
			defined in the dataObject and
			the return of 'beforeFnSync'
		 */
		console.log(props); // to see all props
	},
	afterFnSync: props => {
		// synchronous function that runs After the API call
		// its return is "res3"
		/*
			has access to all the props
			in the dataObject and the return of
			'beforeFnSync', 'beforeFnAsync' and 'API request'
		 */
		console.log(props); // to see all props
	},
	afterFnAsync: async props => {
		// asynchronous function that runs After the API call
		// its return is "res4"
		/*
		 * has access to all the props
		 * in the dataObject and the return of
		 * 'beforeFnSync', 'beforeFnAsync', 'API request',
		 * and 'afterFnSync'
		 */
		console.log(props); // to see all props
	}
};

Dispatching Action Creator

// *** calling the action creator in an async environment
const exampleFunction = async () => {
	// If you don't need the return value
	await dispatch(actionCreator(dataObject));
	/*
		if handleApiSuccess() handleApiErrors()
		are defined: the return of the dispatch will
		be their return values defined above
	*/
	//  if you need the return object
	const response = await dispatch(actionCreator(dataObject));
};
// *** calling the action creator in a sync environment
dispatch(actionCreator(dataObject));