I have a shopping application. I am trying to use the most recommended approach BLOC for the project.
Now I have a screen which contains three major parts
Categories list
Product list
Cart list
Now these subsections of that 1 screen have this operation
load/fetch
apply conditions or filter data
then multiple operations on loaded data
For example like I have a cart
Now in the cart, I have these operations
before adding a product to the cart there are multiple operations
of filtering the data like if the product has discounts or not. if so
then app discount and update price.
increment/decrement
edit
Now I am not getting the idea that should I?
The screen has 3 major sections like portions so should I use 3
blocs of 1 bloc
most apps have examples like to load, show, success, and error type
events and states so where will I apply these increment/decrements,
and apply discount operations?
there may be a case where I will have to access the current data
state from another class or file or bloc. so in short I want to hold
the data to share across multiple screens
For me, i associate every api request with a bloc.
so for example in your case, u have a Cart Page.
I would assume u have these few API requests,
GetCart
RemoveItemFromCart
UpdateItemQuantityInCartItem
So u will have then (using cubit)
GetCartCubit
RemoveItemFromCartCubit
UpdateItemQuantityInCartItemCubit
And each of this blocs can have a function called request() where you pass the data required for the api request and make an api request to a repository where u have your client API requests.
And each of this blocs will have states.
Initial
Requesting - state where api request made waiting for a response
Requested - state where there is a successful API response
Error - error state for when an API returns an error
You can check out this repo for a good example:
https://github.com/momoDragon/flutter_structure
Related
Im having trouble finding documentation regarding best practices for handling activity data emitted from a realtime subscription to a getStream feed.
Our current set-up mimics what it seems react-activity-feed does: (1) subscribe to feed and listen for new activities (2) when a new activity is emitted display a button at the top of the feed announcing the new activity (3) when the announcement button is selected make a new feed.get() call to retrieve the most recent feed data.
The problem we are considering is how we could avoid making a new call to feed.get() every time a new activity is emitted (seems wasteful). We would rather store the original response data from feed.get() inside a state variable and insert new activities into that object.
This doesn't seem possible, however, as we get the following error whenever we try to append to the nested array inside the feed.get() response object: TypeError: Cannot assign to read only property 'activities'
I would greatly appreciate any advice on how others have handled new activities emitted from a feed.
I discovered that react-activity-feed does NOT make a call to feed.get() to retrieve the updated feed data after a new event is emitted to the feed.
feed.get() is only called when a feed is first visited or a pagination event occurs that makes a request to get more feed data. Otherwise, a copy of the feed activity object is created (using immutableJS.Map) and newly emitted events are pushed to this object which is used to display content in the feed (see object definition here). The same is done for managing reactions to activities as well as notifications.
So for anyone who is curious how to manage new events emitted from Streams real-time subscription try to managed feed state in your app by making a copy of the object that is initially returned by feed.get() and manipulate that object in your UI to limit the number of requests made to getStream and improves the performance of the feed as well.
What would be the best approach to handle Model State Errors for the the basket to the user that loads the basket from the API ?
Scenario:
User adds product to the basket ( basket is valid at the time of creating it )
The product is taken off ( for example has been set as not available or the price has changed since the user added to the basket )
( this can be done outside API )
Should the client make two requests:
Get The basket
Validate the basket
( a little bit RPC style )
Another way of doing it might be
extending the response view model with 'errors' that might get populated
whenever user GET it via API.
Not sure though if this is good practice though.
What would be the RESTful way of solving this problem ?
Thanks in advance for help
What would be the RESTful way of solving this problem ?
How would you do it with a web page?
It would probably be a single web page, right? Containing
a list of items in the basket, and...
a list of problems that might prevent the order from growing through
and maybe also some links to other resources that might help resolve these problems.
otherwise forms, or links to forms, to aid in performing the next step of the ordering protocol.
Another way of doing it might be extending the response view model with 'errors' that might get populated whenever user GET it via API. Not sure though if this is good practice though.
It's fine - the resource model is not the domain model is not the data model. Your "resources" are documents that support interacting with the domain.
See also: Webber 2011.
I have started designing addToCart method as HTTP POST in my RESTful API. This looks good when the client adds to cart a product first time (POST create a new entry on server). But, the same HTTP rule breaks when the client browses through the site and adds the same item again; where we should not create a new entry but only update the quantity of existing item.
Isn't using POST to update resource wrong? What is the way to implement this? or how to interpret this situation.
Note: Client/UI front which uses my api would not remember if its already there on server. Please consider any ecommerce application's addtocart as example.
I think the difficulties here are already present in how you add the original item to the cart.
When you add an item to cart, are you creating a new object (the item)? Or are you modifying an existing item (the cart)? To me it makes more sense to say the latter. POSTing an item seems like it should be reserved for a different situation, when you add a new item to your store.
Conceptually, the cart is then like a vector of all the items in your store, associated with a number (0 for almost all of them). Adding something to the cart means incrementing this number for one of the items, regardless of whether it is already more than 0.
You can use PATCH to send a part object / update to an existing object.
It's based on your model. IMHO a POST is also fine if you are modelling a new intent or cart-operation with it, which can be basically anything.
Suppose I have a website with the following menu items on the left site of my page:
Jobs (12)
Users (3)
News (5)
Documents (4)
Please note the labels on the right side of the menu. They indicate how much items there are for each resource. So for example, the are currently 3 users and 5 News items etc.
When I create a web API for this, then I treat each item as a separate resource. So in my Web API I would have the basic end points like:
(GET) /api/job
(GET) /api/job/{id}
(GET) /api/job/count
(POST) /api/job
(GET) /api/user
(GET) /api/user/{id}
(GET) /api/user/count
(POST) /api/user
etc. etc.
Of course also end points for UPDATE and DELETE.
The point here is that I have a count endpoint for each resource. So what I could do is call the count API for each API resource so I can show that number next to each menu item.
But in this case that means I would have to do 4 separate API calls to get the count for each item. And this becomes more when I have more menu items. I don't want to end up with 15 separate calls just to show a label next to each menu item showing how much items each resource has.
What is a good practice to solve this in a good Web API manner? Can I for example create a new end point : api/MenuCount that returns all the separate counts in one call?
But then again, having such an API end point isn't really a resource. It only has one purpose, counting the total rows for all other resources.
Or is it still better to call each count end point for the API's separately? Eventhough that could mean I have to do 15 separate calls for that?
PS. I am talking about a SPA website.
New to ember and ember cli, and not having any JS based framework experience apart from jQuery (which is not a framework)
I find my self stuck at the very beginning compared to work done in Angular,
I have a static list of groups which are on REST api `http://localhost:8000/api/groups' and it is only 8 items there, I needed to show them as dropdown for a search criteria. Nothing fancy
I started with creating a route and model with the name of groups but app stopped working and I had to create a model for group which is identical to groups model, only having 2 items for dropdown
Now i have a url in my ember app http://localhost:4200/groups which I dont need and i dont want it to be there,
But I ignored it and had to create a dropdown of the cities, api is
http://localhost:8000/api/cities <-- All cities list, needed for admin
http://localhost:8000/api/cities/active <-- For clients to show active cities so they can add their records
http://localhost:8000/api/cities/filled <-- For users, to show them where clients are available
So in ember, I created a route and model for cities, and same model is copied as city just to show the list of cities in dropdown, I needed to show cities which are filled, I had created ember g route city/filled and it created folders, but its model is also same like other two models.
Ajax call is not being sent to city/filled url and now I ended up having
http://localhost:4200/cities // useless for me
http://localhost:4200/cities/filled //useless for me
and in filled i see that ajax call is made but to the http://localhost:8000/api/cities two times, loading same data. I tried adding a dropdown on my application and opened http://localhost:4200/cities/filled in browswer, and woosh, it send ajax call to the same url 3 times, one for application.hbs and one for cities.hbs and one for city/filled. Why load the same data 3 times if it is already fetched from same url within single request?
in angular I just call a custom url and I can get the data, but for ember its really hard to get grip on these small things and there is no help around
active and filled are filters for your cities resource and these are not standalone resources, so you should try to use them as query parameters. like
http://localhost:8000/api/cities?type=filled
http://localhost:8000/api/cities?type=active
Facebook uses this style for query params. You can also use query params for getting paginated data of a resource, like for 1st page and 25 records per page, the request will look like:
http://localhost:8000/api/cities?page=1&records=25