Handle data with react (API request once or more) - reactjs

I'm currently developing an web app to keep track of cash flow. Every day an employee counts the amount of coins in the checkout and enters the respective amounts in a form called "Protokoll". These "Protokolls" are displayed in a list which is fetched from a node server. When an item is clicked a new request to the server is performed to get the amounts and display them in a form.
My question is: What is the best/correct way to handle my data?
Should I request the data once when the list is loaded and then pass the respective data to the clicked item or is it okay to use my current method, where the data is fetched again for each individual item?

Related

Variable value for Amount for Salesforce opportunity API

I'm working on a Magento website. In that, after the customer is done with adding products to the cart he fills a form and once the form is submitted, form data along with product details like SKU, price is sent to Salesforce to create a new opportunity.
The API is working and the Data is going to salesforce, but the issue is that the same product can have different prices based on certain conditions.
the new amount going to salesforce but on the salesforce side, the new amount is not reflecting but the amount when the product is created for the first time is coming.
is there any way to make the amount a variable field so the new amount reflects?
below is an image of the opportunity and the JSON data I'm sending.
{"StageName":"New Opportunity","Name":"test","CloseDate":"2021-06-04","What_are_you_interested_in__c":"Purchase New Trailer","Product_JSON__c":"[{\"Name\":\"Large Trailers\",\"Sku\":\"large-trailers-rent\",\"Quantity\":1,\"Price\":\"1301.0400\"}]","Purchase_Delivery_Contact_Phone__c":"123","On_Site_Contact_Email__c":"test-12#test-12.com","Mailing_Street__c":"test-12","Delivery_Street__c":"","Qty__c":"1","X66969__c":0,"First_Name__c":"test-12","Last_Name__c":"test-12"}
This request looks weird. Vanilla Salesforce doesn't need hacks like a text field to hold serialised JSON in it (Product_JSON__c).
Where do you send it? Normal POST to something like services/data/v51.0/sobjects/Opportunity or some custom endpoint (it'd have "/apexrest" in the URL). Even if it goes to normal endpoint - I guess there's trigger that deserialises this Product_JSON__c and creates OpportunityLineItems out of it. You'll have to chat with developer responsible for that trigger, we can't tell what's going on in there.
In general yes, it's possible to have variable prices. You (or that developer) have to read up about OpportunityLineItem. There's ListPrice,UnitPrice,TotalPrice,Discount, lots of choices to do this right and report properly on the discounts! And there are tips like
Creating an OpportunityLineItem increments the Opportunity Amount
value by the TotalPrice of the OpportunityLineItem
There are ways to do it properly, insert header (Opportunity) and line items in one go, without such hacks. See
https://salesforce.stackexchange.com/questions/155422/using-the-rest-api-to-create-parent-and-child-records-in-a-single-http-request
https://salesforce.stackexchange.com/questions/274694/can-you-upsert-using-composite-sobject-tree
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_sobject_tree.htm

Handling API lags with React.js

I'm pulling from an API that takes like 30 seconds to load in the data. Right now I have a loading widget that displays while the API is fetching data. But I was curious if there is a way to show the previous data while the updated data is fetching?
For example, let's say I'm pulling in total payout. The total payout was $100,500. Then, at the next fetch it's $100,501. Then $100,502. Is there a way I can display the old number while the new number is being fetched (as opposed to displaying a loading widget)?

What is the best method to fetch more data based on event?

I have built an app that fetches a list of posts based on selected category.
When the category changes I reset state and load a new set of posts based on the new category. I use componentDidMount() to call a fetch data function based on category and componentDidUpdate(prevProps) to check if a new category has been selected. If so I reset data and call the data function again. This works fine except I only want to call the first 50 entries. If a customer triggers an event (window scroll or clocks load more data button) I then want to load and display the next 50 records. This is where I am struggling. any suggestions on the the best way of implementing this?

Large amount of data handling in react

I am using react with redux for development.
I have around 20000 rows in database. I need to display list to users on screen, 10 rows at a time. I also have sorting and filters applied to the list.
I can think of 2 options
Store only UI state of component in the redux state and bring data for each page using ajax/fetch. This way all sorting and filters are applied on server end and i get only 10 rows to display at a time.
Bring all data once to redux state and display 10 records. This way we do not request server for data again, as all sorting and filters are applied within data stored in state
What is better option/practice?

AngularJS dynamic table - RESTfull data

I have a big data portion that I would like to post in a table. However, the data should be sorted and paginated. I know I am able to pass the whole data to the client at once and then paginate it using angular, but this will be too slow. I prefer to pass the data page-by-page, so one the client want to open a page from a table to load the data for it.
Up until now I have created an API that returns me the data that I need, based on the page number and the number of rows on the page. However, I don't know how to use it with AngularJS.
Can you please help me?
It looks like a backend problem. If you are using a standard restful backend, use the limit/skip parameters, you can encapsulate into a paginate.
Example:
localhost:1337/dataTable?skip=0&limit=100
localhost:1337/dataTable?skip=100&limit=100
localhost:1337/dataTable?skip=200&limit=100
...
On the frontend use a table object like ng-Table, and use the pages to keep track of the offset, the page number and the total items available.
skip = (pagNum - 1 * pageSize)
limit = pageSize
Make your backend return you the page you want plus the available dataNumber so you can build the pages controller.
Documentation for skip/limit on sails
http://sailsjs.org/documentation/reference/waterline-orm/queries/limit
http://sailsjs.org/documentation/reference/waterline-orm/queries/skip
Best approach is to keep track of the limit and offset in your controller. Then when user selects new page (offset) or changes items per page (limit), update the corresponding values and use them to make a new http request.
You could call a function on ng-change of a dropdown and that drop down would contain values of page number and number of records to fetch. Or you can provide two text boxes one for page number other for number of records and keep a button and on its ng-click event that will take value of those text boxes and post to your server and bring back data based on new values in text boxes

Resources