Problem to update list item with flutter redux? - redux

I am working with cart list and wants to update my cart after choosing. So i need to update list item. What shall be the process to do so?
I have tried with redux. The list data will be removed after app refresh/reopen. I can add and remove items. but I am facing problem while updating list item.
For adding I have done something like this -
List <Item> addItemReducer(List<Item> state, action) {
return []
..addAll(state)
..add(Item(id: action.id, body: action.item, cost: action.price));
}
To remove the process I have used -
List <Item> removeItemReducer(List<Item> state, action) {
return List.unmodifiable(List.from(state)..remove(action.item));
}
Keyword 'add' for insert and 'remove' for delete is available 'update' keyword is not available. If I try to type 'update' it shows error.

Related

Create item if not found and return old one if existing in dynamodb, in one call

In DynamoDb, is it possible to do a conditional put and return the old item if there already was a matching item?
I would like something like the following to create the row if the user does not already exist, and if it does exist, I want it to return the old item (with whatever name was there before). I.e., insert item if new, else just read existing item.
await documentClient.put({
TableName: 'table',
Item: {
userId: 'user0',
name: 'smith',
},
ConditionExpression: 'attribute_not_exists(userId)',
});
Is this possible to do in one call?
Sort of.
If you use UpdateItem it will create or update the item. It only updates the fields that have changed however, so if your fields have not changed, then no update will be made. It will also create the item if you do not have it.
Using Conditional Items for this will return an ErrorCode if the condition fails ConditionalCheckFailedException - which would require an additional operation.
(I don't use the javascript SDK that much, more in python, so I'm not sure this is the correct version/page but here is some documentation:
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#updateItem-property )

Create and update database with input from PageView.builder in flutter

I have a PageView.builder that has a textFormfield and a title. The builder iterates through the elements of the list and once on the last one, there is a submit button that I would like to send the Key : Value pairs to firestore. when I use for each, it only creates the content of the last item of the list multiple times.
here is my create and update function:
Future updateDatabase(String remarkText) async {
return await databaseCollection.document().setData({
questionItems[index].title : questionItems[index].remarkText
});
}
and this is how I call it in my button
onPressed: () async {
questionItems.forEach((question) async {
await updateDatabase(remarkText);
});
},
How can I loop through them to send data for the previous items as well? Please help.
I think it's iterating over all items, but updating always the same item in Firestore. The variable index should be changed in each iteration somehow. Otherwise each iteration will set value on the same questionItems[index].
I hope it will help!

NGXS: Can we split out an object from array into a substate?

My top level state model looks like this:
{
listOfItems: [], // Item[]
selections: {
itemId: 0
}
}
The items list may contain 10 different shopping items.
When the user clicks on an item, it updates the selection, and my #Selector will rerun.
Action: Set Item Selection
#Action(Item.Select)
setState(
{ setState }: StateContext<ItemsModel>,
{ itemId }: Item.Select
) {
setState(patch({ selections: patch({ itemId }) }));
}
Selector: Select Current Item
#Selector()
static getSelectedItem(state: ItemModel): Item {
return state.itemList.find(i => i.itemId === state.selections.itemId);
}
Problem is: I have up to 20 actions to perform on the selected Item. This results in:
Lots of .find() lookups to find item in the original array (both selector and actions)
Actions to perform on the listOfItems are in the same place as those to perform on a specific Item
I would like to: Keep the array and selection in this state, but separate out the "selected item" into a new substate, where the child state's model can just be Item type. This way I can encapsulate all the actions on Item in a different place to actions on the Items[] array.
I'm not sure how to keep them in sync. I need to keep the 'selectedItem' state up to date when the selection itemId changes in the parent. I also need to make sure any mutations to the selectedItem are reflected in the original array in the parent.
This seems like it might be more of a fundamental problem with how you are trying to represent your application state. Have you thought of normalizing your list of items? Or at least using a key/value lookup object instead of an array? You wouldn't need to use the .find() to do your lookup and could access the key of the object via the unique id you are interested in. Let me know if that is of any help!

Popup and Redux Reducers

I'd like to know how to handle specific use case with redux reducer. To give an example, say I have a form with a DataGrid/Table. On Edit button click from a row, I want to open the popup with seleted row-data. After editing the data, On Popup-Submit button click, I want to update the Table/DataGrid (i.e. DataGrid will now should have the edited values).
I've written two separate Components
1. MainPage.js and its corresponding reducer MainPageReducer (Employee List)
2. PopupPage.js and its corresponding reducer PopupPageReducer (Selected Employee)
How these two reducers share the state?
You may need to read this first
http://redux.js.org/docs/basics/UsageWithReact.html
The main concept is that through the connect function, you would simply map needed properties of your state to the properties of your component i.e MapStateToProps. So in your case, imagine that your state, for contrived purposes, is structed like so:
{employees: {employees: {1: {id: 1, name: 'Foo'}}, editedEmployeeId: 1}
You could map the array of employees to an employees property for your EmployeeList component whilst also mapping a dispatch function, named editEmployee(id) to a click function on each row in the table.
You could map [the employee with the associated editedEmployeeId] to the individual employee in your employees array for your popup component
It may be efficient to just use one reducer instead of two.
Specifically, if you're making an update to an individual employee then you would call an EDIT_EMPLOYEE action and then a SAVE_EMPLOYEE action on save. After the SAVE_EMPLOYEE action, then, I assume, you'd call a post method, and then react-redux would re-render your entire list.
It could look like this:
function employees(state = {editedEmployeeId: undefined, employees = []}, action) {
switch(action.type) {
case EDIT_EMPLOYEE:
return Object.assign({}, state, {editedEmployee: action.employee_id})
case SAVE_EMPLOYEE:
return Object.assign({}, state, {employees: action.employees});
default:
return state;
}
}
There are great holes in my answer because the question you're asking might be too broad; I'm presuming you don't fully understand how the connect, subscribe, and dispatch functions work.
Like one of the comments said, reducers don't share state. They simply take the previous version of your state and return another version of it.
Anyways, hope this helps. Read the redux docs!

How to access and change a specific attribute in a collection?

I have a collection called "Products". I want to access and change an attribute called "screenShots" inside the collection.
This code didn't work with me
screenshotsURLS: function(sshots) {
check(sshots, [String]);
Products.update({},{$set:{screenShots:sshots}});
console.log(sshots);
}
when i console.log the sshots, i can see that the array exists, but the update function isn't working
how do i set the screenShots attribute inside the Product collection to whatever value passed in "screenshotsURLS" function?
For that you have to update the mongodb document.
THis is how you can update the doc in meteor.
collectionName.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
In your your case collectionName is Products and field is screenShots.
So for doing this. Your query will be
sshots
Products.update({},{$set:{screenShots:sshots}}) (Be careful this will update all of your doc)
For selecting doc update use the query like.
Products.update({name:'yourProductName'},{$set:{screenShots:sshots}})
For more about update a please check this link.

Resources