GithubHelp home page GithubHelp logo

zarkopernar / redux-store-list Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 93 KB

Set of utilities for quickly creating lists of entities, along with reducers, types, and action creators

License: MIT License

TypeScript 95.57% JavaScript 4.43%
redux ngrx

redux-store-list's Introduction

Redux Store List

Set of utilities for quickly creating lists of entities, along with reducers, types, and action creators

Installation

npm i redux-store-list

Get Started

// example.js
import { createStoreList } from 'redux-store-list'

export const exampleList = createStoreList('example')
// appStore.js
import { createStore } from 'redux'
import { exampleList } from './example.js'

const store = createStore(combineReducers({
    example: exampleList.reducer,
}))

store.dispatch(exampleList.actionCreators.loadList())
// { byId: {}, allIds: [], loading: true }

const response = await fetch('/api/list').then(res => res.json()) // { data: [{id: 1, name: 'Example Entity 1'}] }
store.dispatch(exampleList.actionCreators.loadListSuccess(response))
// {
//    byId: {
//        1: {
//            id: 1,
//            name: 'Example Entity 1',
//        }
//    },
//    loading: false,
//    allIds: [1]
// }

With Epic

// example.js
import { createStoreList } from 'redux-store-list'
import { createEpic } from 'redux-store-list/epic'

export const exampleList = createStoreList('example')

export const exampleEpic = createEpic(exampleList, {
    loadList() {
        const res = await fetch('/api/list').then(res => res.json())
        // { data: [{id: 1, name: 'Example Entity 1'}] }
        return res
    }
})
// appStore.js
import { createStore, applyMiddleware } from 'redux'
import { combineEpics, createEpicMiddleware } from 'redux-observable'
import { exampleList, exampleEpic } from './example.js'

const epics = combineEpics(
    exampleEpic,
    // ...other epics
)
const epicMiddleware = createEpicMiddleware(epics)
const reducers = combineReducers({
    example: exampleList.reducer,
})
const middleware = applyMiddleware(epicMiddleware)
const store = createStore(reducers, {}, middleware)

store.dispatch(exampleList.actionCreators.loadList())
// { byId: {}, allIds: [], loading: true }

// Some time later
// {
//    byId: {
//        1: {
//            id: 1,
//            name: 'Example Entity 1',
//        }
//    },
//    loading: false,
//    allIds: [1]
// }

Options

    createStoreList('name', {
        getEntityId: '_id' || (entity) => entity.some_id_field,
        api: {
            loadList: (params) => api.getlist(params),
            add: (data) => api.add(data),
            update: (data) => api.update(data),
            remove: (data) => api.remove(data),
        },
        pages: [
            'somePageListName',
            {
                type: 'single' || 'list',
                name: 'someOtherPageName',
            }
        ]
    })

redux-store-list's People

Contributors

zarkopernar avatar

Watchers

 avatar  avatar

redux-store-list's Issues

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.