Code Monkey home page Code Monkey logo

react-redux-spinner's Introduction

React Redux Spinner

A simple spinner for any "long" running tasks (such as fetching data off a server).

Demo of spinner functionality

DEMO: https://storytel.github.io/react-redux-spinner

Installation

npm install react-redux-spinner --save

Usage

Import the library.

import {
  Spinner, // The React component
  pendingTasksReducer, // The redux reducer
  pendingTask, // The action key for modifying loading state
  begin, // The action value if a "long" running task begun
  end, // The action value if a "long" running task ended
  endAll // The action value if all running tasks must end
} from 'react-redux-spinner';

Install the reducer to the store. Make sure it reduces the pendingTasks key. This is best done using combineReducers from redux.

import { createStore, combineReducers } from 'redux'
const reducer = combineReducers({
  pendingTasks: pendingTasksReducer
});

const store = createStore(reducer);

Put the Spinner component anywhere in your application.

import React from 'react';
const App extends React.Component {
  render() {
    return (<Spinner />)
  }
}

Start a long running task. This will typically be when you begin fetching data from a server.

This will increase the pending tasks counter by 1. The spinner will be shown when the pending tasks is greater than 0.

store.dispatch({
  type: 'ANY_OF_YOUR_ACTION_TYPES'
  [ pendingTask ]: begin // Make sure you embrace `pendingTask` in brackets [] to evaluate it
  // Any additional key/values may be included here
});

When your long running task is done.

store.dispatch({
  type: 'ANY_OF_YOUR_ACTION_TYPES_DONE'
  [ pendingTask ]: end // Bracket [] embrace, remember?
  // Any additional key/values may be included here
});

When you want to force the finish of all pending tasks (for example: with a global error).

store.dispatch({
  type: 'ANY_OF_YOUR_ACTION_TYPES_FINISH'
  [ pendingTask ]: endAll // Bracket [] embrace, remember?
  // Any additional key/values may be included here
});

CSS

By default, no styling is included. You can either roll your own. Feel free to use the default css as boilerplate.

If you're using webpack as your bundler, you could use style-loader and css-loader to include the default css.

In webpack.config.js:

  // ...
  module: {
    rules: {
      {
        test: /\.css$/,
        loader: 'style-loader!css-loader'
      }
    }
  }

and in your css files (the tilde ~ means "look in node_modules"):

@import '~react-redux-spinner/dist/react-redux-spinner.css';

Configurable reducer

Maybe you cannot have the pendingTask in the root of your actions. For instance, if you're trying to follow the Flux Standard Actions you're not allowed to have anything in the root except type, payload, error and meta. It would then be prudent to put pendingTask in meta. To get the reducer to look here instead you need to configure it to do so with configurablePendingTasksReducer.

import { configurablePendingTasksReducer } from 'react-redux-spinner';
import { createStore, combineReducers } from 'redux'
const pendingTasks = configurablePendingTasksReducer({ actionKeyPath: [ 'meta' ] });
const reducer = combineReducers({
  pendingTasks: pendingTasksReducer
});
const store = createStore(reducer);

and then dispatch actions:

store.dispatch({
  type: 'ANY_OF_YOUR_ACTION_TYPES'
  meta: {
    [ pendingTask ]: begin
  }
});

The actionKeyPath may be as deeply nested as required. For instance, if you'd like to have the keys in meta.async, you'd provide actionKeyPath: [ 'meta', 'async' ] and then dispatch actions with { meta: { async: { [ pendingTask ]: begin } } }

Pro-tips

  • Don't want to bloat your namespace with begin or end variables?
import rrs from 'react-redux-spinner';

dispatch({
  type: 'ACTION_TYPE',
  [ rrs.pendingTask ]: rrs.begin
});
import { pendingTasksReducer as pendingTasks } from 'react-redux-spinner';
const reducer = combineReducers({ pendingTasks });
  • Modify the appearance of the spinners (example modifies the color, but any css attributes can be set)
#nprogress .bar {
  background-color: #f4590a;
}

#nprogress .spinner-icon {
  border-top-color: #f4590a;
  border-left-color: #f4590a;
}

#nprogress .peg {
  box-shadow: 0 0 10px #f4590a, 0 0 5px #f4590a;
}
  render() {
    return (<Spinner config={{ trickleRate: 0.02 }} />)
  }

react-redux-spinner's People

Contributors

arturocr avatar boelensman1 avatar dependabot[bot] avatar domszyn avatar iakovosvo avatar noseglid avatar pikel618 avatar rakshans1 avatar semionpar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-redux-spinner's Issues

loading bar stays in view even after dispatching 'end'

even after dispatching [ pendingTask ]: end loader stays in view. loader only leaves view once the horizontal bar reaches the right side of the screen. doesn't seem to coordinate with actual dispatch of end action.

Server Side Rendering error

Hello!
There is server side rendering when try to use react-redux-spinner. The problem with window object:

ReferenceError: window is not defined
/Users/admin/mt_front/node_modules/react-redux-spinner/dist/index.js:200
            return /msie [6-9]\b/.test(window.navigator.userAgent.toLowerCase());
Stack:
  at /Users/admin/mt_front/node_modules/react-redux-spinner/dist/index.js:200:31
    at memoize (/Users/admin/mt_front/node_modules/react-redux-spinner/dist/index.js:195:48)
    at module.exports (/Users/admin/mt_front/node_modules/react-redux-spinner/dist/index.js:217:69)
    at Object.<anonymous> (/Users/admin/mt_front/node_modules/react-redux-spinner/dist/index.js:97:37)
    at __webpack_require__ (/Users/admin/mt_front/node_modules/react-redux-spinner/dist/index.js:30:30)
    at Object.<anonymous> (/Users/admin/mt_front/node_modules/react-redux-spinner/dist/index.js:71:2)
    at __webpack_require__ (/Users/admin/mt_front/node_modules/react-redux-spinner/dist/index.js:30:30)
    at Object.defineProperty.value (/Users/admin/mt_front/node_modules/react-redux-spinner/dist/index.js:57:19)
    at __webpack_require__ (/Users/admin/mt_front/node_modules/react-redux-spinner/dist/index.js:30:30)
    at /Users/admin/mt_front/node_modules/react-redux-spinner/dist/index.js:50:18

New version for redux 4.0 dependency

Master has been updated to add support for redux 4.0. Is it possible to release a new version (1.1.4)? Yarn currently complains about unmet peer dependency.

bundle file is too big

hi and thanks for this repo
the build file is almost 47kb
is there any way to reduce that ?

capture

thanks

How to use with combineReducers

Hi there,

I try to use your spinner on my application, and, I have more than ten reducers, and I used combineReducers. So, my question is, how we use it with multi reducers?

Thanks,

Getting CSS error

I am getting the following error:

/home/andrey/Projects/Datarisk/app-frontend/node_modules/react-redux-spinner/dist/nprogress.css:2
#nprogress {
^
SyntaxError: Invalid or unexpected token
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Module._extensions..js (module.js:579:10)
    at Object.require.extensions.(anonymous function) [as .js] (/home/andrey/Projects/Datarisk/app-frontend/node_modules/b
abel-register/lib/node.js:152:7)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/andrey/Projects/Datarisk/app-frontend/node_modules/react-redux-spinner/dist/index.js:8:1)
error Command failed with exit code 1.

I have set up everything correctly. I use webpack and I have a loader for CSS.

What are my options?

multiple redux spinners

Is there any way to have multiple instance of this by using the curry design? or any design?

What I am trying to do is, basically have a bar per bootstrap card, vs 1 bar across the entire page. I want to have the ability to start n stop each one individually based on that reducer, etc...

Is this at all possible with the current design?

Create a new release

The current 0.4.0 release is missing documented features (like config).

Release a new version and publish to npm.

How to customize position (e.g. top/bottom of a <div>)?

How do I change the location of the progress bar and the spinner? Let's say I have a query <form>, and I want to have the spinner and the progress bar appear at the bottom of the form after user presses <button>. How to achieve this?

Spinners with Id's

Hi ,
Is there a way to assign an Id to a spinner ?
For instance if I do not want to start a nother spinner if one with that Id is already running ?
The same goes for ending the spinner, do not end if the id count is 0.
Tx

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.