Dynamically get the name of a custom state so I can add an item to its list

Hi,

I’m working on an app where the user can create their own product.
Imagine a burger.

They can configure the product through multiple steps, and based on what options they pick the subsequent pages/ options will be different. the general steps are below, and if they choose a particular option on a page, it may add or remove upcoming pages. also based on what product they pick, they will have different limits on the max number of ingredients they can pick.

  1. choose the base product.

  2. Choose variant

  3. choose size

  4. choose ingredient/s (based on the product and variant they pick, the user will have the option to choose from multiple combinations, i.e product 1 allows you to choose 1 meat and 1 vegetable, product 2 allows you to choose 1 meat and 2 vegetables.)

  5. choose container

  6. choose top

  7. summary

this dynamic ingredient list has been causing me the most difficulty, as I have set it up as a reusable element, so it’s not actually fixed to a single ingredient type.

I am able to display the correct ingredients based on the product selected, but how can I dynamically save the selected ingredients to the database thing called order?
as when I’m trying to save the ingredient list to the database thing, ingredient type could be meat, vegetables or sauces based on whatever ingredient the user is currently picking.

I know I could possibly just copy and paste the set list to custom state several times and then do the same when adding to the db, but I was hoping someone could help me with a more elegant solution.

I have attached images of how I’m trying to save the ingredient list to the correct list in the custom state, but trying to figure out how to dynamically get the name of the list in custom state.


Thank you very much for any help,

Andrew

For your use case, instead of adding ingredients directly to the order, it may be better to create a new data type, something like Ingredient-Line, with fields for: Type ingredient, Type Order, Quantity, etc. In this way you can have a much more customizable datatype with more options for list manipulation, pricing changes, etc.

3 Likes

@msgiblin Thanks very much! I’ll look into it!
I keep thinking that creating things as data types only relates to things in the database, but that’s not the case.

1 Like

Honestly I had the same growing pains. I’m working on a recipe app, and my first iteration I was dropping ingredients right onto the recipe. Some very helpful forum folks pointed me this direction and I created what I called “recipe-ingredients” and my data was MUCH easier to manage on each recipe, especially as quantity would change from recipe to recipe. In your case, since you most likely have pricing as well, this data structure will keep future pricing changes at the ingredient level from inadvertently updating past orders.

1 Like