Erdal SATIK
Erdal SATIK

Reputation: 670

Immutable js - Redux-Saga find item by key and update value

I am using redux-saga and immutable.js in my react app. I have notifications array holds the data of every notification created by redux actions for whole app.

My immutable redux store and notifications array is like this :

global : 
  notifications: 
   0:
    key: 21339298 // random key to use like id
    show: true
    type: success
    message: Operation was successfull
   1: {key..., show...}
   ...

I want to find a single notification by "key" and update it's "show" value to false.

I read immutable.js documentation but it is very difficult to understand. So i tried below code snippets. But i didn't get a result.

return state
    .updateIn(['notifications', 
        ns => ns.findIndex(function (item) {
            return item.get("key") === action.notificationId;}
        ), 'show'], false
    );

return state
    .update(
        list.findIndex(function (item) {
            return item.get("key") === action.notificationId;
        }), function (item) {
            return item.set("show", false);
        }
    );

How can i find an item and update its some value ?

Upvotes: 0

Views: 189

Answers (1)

Erdal SATIK
Erdal SATIK

Reputation: 670

Solution is to seperate find index and update, first finding index and then using it with setIn. Below is the solution.

case NOTIFY_HIDE:
        let notifications = state.get('notifications');
        let notificationIdx = notifications.findIndex(function (item) {
            return item.get('key') === action.notificationId;
        });
        return state
            .setIn(['notifications', notificationIdx, 'show'], false);

Upvotes: 2

Related Questions