0

My Redux state has the following structure:

const state = {
 transactions: {
    1: {
       TransactionId: 1, Status: "Pending", Type: "Withdrawal", ClientName: "Paul Carter", Amount: "$28.43"
    },

    2: {
      TransactionId: 2, Status: "Completed", Type: "Refill", ClientName: "Caldwell Reid", Amount: "$45.16"
  },
...
}

What is the correct way to update/delete an object from transactions given that I have an id ?

Here is how I tried to update Status property in my reducer but it didn't seem to work, my state remains unchanged.

const transactionsReducer = (state, action) => {
    case CHANGE_TRANSACTION_STATUS:
      return {
        [state.transactions[action.id].Status]: action.status
       }
}

3 Answers 3

2

You can do this:

const transactionsReducer = (state, action) => {
  switch (action.type) {
    case CHANGE_TRANSACTION_STATUS:
      return {
        ...state,
        transactions: {
          ...state.transactions,
          [action.id]: {
            ...state.transactions[action.id],
            Status: action.status
          }
        }
      }
  }
}

Also, I personally think, it is better to use small-casing here for property names to avoid mistakes. Example:

transactionId: 1, status: "Pending", type: "Withdrawal", clientName...
Sign up to request clarification or add additional context in comments.

Comments

1

Here you go, it should look something like this :

if you are getting transactions object within state directly :

return {
    ...state,
    [action.id]: {
        ...state[action.id] ,
        Status : action.status
    }
}

And if not then, you need to use this :

return {
    ...state,
    transactions : {
        ...state.transactions ,
        [action.id]: {
            ...state.transactions[action.id] ,
            Status : action.status
        }
    }
}

Comments

0

transactions should be an array (it's a collection of entities). The 'id' key is redundant, it's already represented by 'TransactionId' (single source of truth). like this:

const state = {
 transactions: [
     {
       TransactionId: 1, Status: "Pending", Type: "Withdrawal", ClientName: "Paul Carter", Amount: "$28.43"
    },

     {
      TransactionId: 2, Status: "Completed", Type: "Refill", ClientName: "Caldwell Reid", Amount: "$45.16"
  },
...
]

then you can simply use filter for delete and map for update:

return {
    ...state,
    transactions: state.transactions.filter(txn => txn.TransactionId !== action.deletedId)
}

2 Comments

That's what I did initially and it worked perfectly fine. But the task specifically says that I have to introduce a hash map to optimize transaction search. I'm no expert but since we have to copy each level of nesting it hardly looks like optimization to me
ok, the optimization is never utilized anyway, since you construct a new state object every time the reducer is invoked (return {...state}) - that's how redux works. If you wan't to mutate state, you should do higher level platforms like Java or C++

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.