Skip to content

Type-safe arguments to components #7

Open
@felixSchl

Description

@felixSchl

Currently, each mounted component (as part of Router) gets as props the arg in Router RouteProps arg, by virtue of RouteProps arg.

But if I have an ADT:

type Query = String
data Locations
  = HomeR
  | SearchR (Maybe Query)

Then hook my search route, to my "searchClass" (see below),

router :: Router RouteProps Locations
router =
...
        lit "search" *> do
          SearchR
            <$> ((Just <$> param "query") <|> pure Nothing)
        ) searchClass' :+ Nil)
...

Then my searchClass receives as the arg the Locations type ADT. I wonder if it possible to somehow scope the args into the correct entry of the ADT.

So, for arguments sake, I could have:

newtype SearchRArgs = ...
data Locations
  = HomeR
  | SearchR SearchRArgs 

And then somehow connect the dots. I understand the Router data structure most be homogeneous of course, so maybe type classes could come into this? Have you had to deal with this scenario before, is there an easy way out of this?

For now I'll resort to pattern matching in each class, which makes for some non-sensical code, where my search component might end up with args pointing to an entirely different route.

EDIT: Actually resorting to Partial => ... and unsafePartial in my class is arguably an acceptable middleground:

searchClass
  :: Redux.ConnectClass state { arg :: Locations } _ action
searchClass = Redux.connect (unsafePartial stateToProps) dispatchToProps {} klass
  where
  stateToProps :: Partial => _
  stateToProps {} { arg: SearchR foo } = { foo }
  ...

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions