Replies: 2 comments 1 reply
-
FYI there was some similar work done at #82 |
Beta Was this translation helpful? Give feedback.
-
For another feedback on this case : We faced a quite-similar problem in our app, when a component State is represented by an enum (i think this can happen very often on basic components). So we basically just have implemented a classic State, with common informations as root properties, and another property as an enum, named "ContentState" or "ViewState", depending on usages. |
Beta Was this translation helpful? Give feedback.
-
Hello folks.
I’ve been using TCA for about a year now and I’m truly excited that we’ve started using it in my jobby-job banking project (with a hundred of thousands user base). We’re close to shipping the first feature with it.
I wanted to walk you through a small problem we had and how we overcame it. It may be considered as a feature request or just me making sure I haven’t done anything stupid.
In the aforementioned feature, we have an input field that can be an arbitrary amount of types - amount entry, date selection, multiple choice, etc.
Initially, I had put all the children input states (
AmountEntryState
,DateSelectionState
, etc) as optionals in the parentInputState
. Whichever state is non-optional, that one’s corresponding view gets displayed (by using a couple ofIfLetStore
).While the feature is unit tested and we are sure there’s no issue of having two of those states non-optional at any given time, one still can’t help but notice that this is a perfect case for having a switch. There will always be one and only one children state alive at a given point.
So we did a couple of things.
First of all, we made all children states a case of an enum. The parent state has a variable that is of type the enum.
Furthermore, to transform a reducer that works on single case of an enum into one that works on the whole enum, we added
casePullback
:So now we can have a small input view like
DateSelectionView
that works withDateSelectionState
andDateSelectionAction
and a reducer that works on those types, but can be pulled back and invoked only when theGlobalState
enum is a case of.dateSelection(DateSelectionState
.We can continue using
IfLetStore
for all different cases, but now we need to be able to extract the state from the enum case. To do that, we added aStore.scope
overload that uses aCasePath
:So now we can show the
DateSelectionView
only when the parent state enum is.dateSelection(DateSelectionState)
Finally, to streamline mutations, especially in unit tests, we’ve added a little helper, once again relying on
CasePaths
.This greatly reduces boilerplate. For example, in unit tests, when we want to assert the date selected, instead of writing this:
ugh…
We can write this:
Any feedback would be very much welcomed. Everything works as expected, but if you see any red flags, let me know.
Thank you! ✌️
Beta Was this translation helpful? Give feedback.
All reactions