Versent / redux-crud

A set of standard actions and reducers for Redux CRUD Applications

Repository from Github https://github.comVersent/redux-crudRepository from Github https://github.comVersent/redux-crud

Add pending state for FETCH_START

hampusohlsson opened this issue · comments

It would be great if you could add a pendingFetch state for when data is getting loaded, just like there is for create, update and delete :)

There is a fetchStart action. At this point records are not there so there is nowhere to add this pendingFetch state to. You would need a special property in the state just for tracking this. Can you elaborate more?

Hmm, true... I guess I have to dispatch my own pendingFetch state from the fetchStart action. Although it would be nice to have the state look something like this (if I have a reducer called 'clients')

{
    clients: {
        pendingFetch: true,
        records: [records]
    }
}

instead of the current

{
    clients: [records]
}

You can have a reducer that stores this anywhere you want e.g.:

{
    clients: [records],
    fetches: {
        clients: 'busy'
    }
}

In this way you can still use redux-crud for storing the records.

Redux-crud doesn't have an opinion on this. The way we are handling this is by using this other lib https://github.com/Versent/redux-loader

Thanks! Although, that lib seems a little overkill for me, I solved it with the duck below. Including the code example for anyone else that might have the same question as me

import { combineReducers } from 'redux';
import crud from 'redux-crud';

const type = crud.actionTypesFor('clients');
const data = crud.reducersFor('clients');

const fetchReducer = (state = false, action = {}) => {  
    switch (action.type) {
    case type.fetchStart:
        return true;
    case type.fetchSuccess:
    case type.fetchError:
        return false;
    default:
        return state;
    }
}

// ... some action creators

export default combineReducers({ 
    pending: fetchReducer, 
    records: data
});

Thanks for posting that snippet, nice and succinct solution. Closing the issue now.