Use all module list for subscriptions and view

This commit is contained in:
Tessa Kelly 2020-03-31 15:16:10 -07:00
parent 92573874fc
commit b87a61b86f
2 changed files with 129 additions and 28 deletions

View File

@ -19,23 +19,42 @@ type alias Example state msg =
wrap :
{ wrapMsg : msg -> msg2, unwrapMsg : msg2 -> Maybe msg }
{ wrapMsg : msg -> msg2
, unwrapMsg : msg2 -> Maybe msg
, wrapState : state -> state2
, unwrapState : state2 -> Maybe state
}
-> Example state msg
-> Example state msg2
wrap { wrapMsg, unwrapMsg } example =
-> Example state2 msg2
wrap { wrapMsg, unwrapMsg, wrapState, unwrapState } example =
{ name = example.name
, state = example.state
, state = wrapState example.state
, update =
\msg2 state ->
case unwrapMsg msg2 of
Just msg ->
\msg2 state2 ->
case ( unwrapMsg msg2, unwrapState state2 ) of
( Just msg, Just state ) ->
example.update msg state
|> Tuple.mapFirst wrapState
|> Tuple.mapSecond (Cmd.map wrapMsg)
_ ->
( state2, Cmd.none )
, subscriptions =
\state2 ->
case unwrapState state2 of
Just state ->
Sub.map wrapMsg (example.subscriptions state)
Nothing ->
( state, Cmd.none )
, subscriptions = \state -> Sub.map wrapMsg (example.subscriptions state)
, view = \state -> List.map (Html.map wrapMsg) (example.view state)
Sub.none
, view =
\state2 ->
case unwrapState state2 of
Just state ->
List.map (Html.map wrapMsg) (example.view state)
Nothing ->
[]
, categories = example.categories
}

View File

@ -39,9 +39,9 @@ import Html.Styled as Html exposing (Html)
type alias ModuleStates =
{ accordion : Example Accordion.State Msg
, button : Example Button.State Msg
, bannerAlert : Example BannerAlert.State Msg
{ accordion : Example State Msg
, button : Example State Msg
, bannerAlert : Example State Msg
, clickableText : Example ClickableText.State ClickableText.Msg
, checkbox : Example Checkbox.State Checkbox.Msg
, dropdown : Example Dropdown.State Dropdown.Msg
@ -51,7 +51,7 @@ type alias ModuleStates =
, textArea : Example TextArea.State TextArea.Msg
, textInput : Example TextInput.State TextInput.Msg
, disclosureIndicator : Example DisclosureIndicator.State DisclosureIndicator.Msg
, modal : Example Modal.State Msg
, modal : Example State Msg
, slideModal : Example SlideModal.State SlideModal.Msg
, slide : Example Slide.State Slide.Msg
, sortableTable : Example SortableTable.State SortableTable.Msg
@ -64,9 +64,30 @@ type alias ModuleStates =
init : ModuleStates
init =
{ accordion = Example.wrap { wrapMsg = AccordionMsg, unwrapMsg = getAccordionMsg } Accordion.example
, button = Example.wrap { wrapMsg = ButtonMsg, unwrapMsg = getButtonMsg } Button.example
, bannerAlert = Example.wrap { wrapMsg = BannerAlertMsg, unwrapMsg = getBannerAlertMsg } BannerAlert.example
{ accordion =
Example.wrap
{ wrapMsg = AccordionMsg
, unwrapMsg = getAccordionMsg
, wrapState = AccordionState
, unwrapState = getAccordionState
}
Accordion.example
, button =
Example.wrap
{ wrapMsg = ButtonMsg
, unwrapMsg = getButtonMsg
, wrapState = ButtonState
, unwrapState = getButtonState
}
Button.example
, bannerAlert =
Example.wrap
{ wrapMsg = BannerAlertMsg
, unwrapMsg = getBannerAlertMsg
, wrapState = BannerAlertState
, unwrapState = getBannerAlertState
}
BannerAlert.example
, clickableText = ClickableText.example
, checkbox = Checkbox.example
, dropdown = Dropdown.example
@ -76,7 +97,14 @@ init =
, textArea = TextArea.example
, textInput = TextInput.example
, disclosureIndicator = DisclosureIndicator.example
, modal = Example.wrap { wrapMsg = ModalMsg, unwrapMsg = getModalMsg } Modal.example
, modal =
Example.wrap
{ wrapMsg = ModalMsg
, unwrapMsg = getModalMsg
, wrapState = ModalState
, unwrapState = getModalState
}
Modal.example
, slideModal = SlideModal.example
, slide = Slide.example
, sortableTable = SortableTable.example
@ -87,6 +115,53 @@ init =
}
type State
= AccordionState Accordion.State
| ButtonState Button.State
| BannerAlertState BannerAlert.State
| ModalState Modal.State
getAccordionState : State -> Maybe Accordion.State
getAccordionState msg =
case msg of
AccordionState childState ->
Just childState
_ ->
Nothing
getButtonState : State -> Maybe Button.State
getButtonState msg =
case msg of
ButtonState childState ->
Just childState
_ ->
Nothing
getBannerAlertState : State -> Maybe BannerAlert.State
getBannerAlertState msg =
case msg of
BannerAlertState childState ->
Just childState
_ ->
Nothing
getModalState : State -> Maybe Modal.State
getModalState msg =
case msg of
ModalState childState ->
Just childState
_ ->
Nothing
type Msg
= AccordionMsg Accordion.Msg
| ButtonMsg Button.Msg
@ -162,17 +237,24 @@ update msg moduleStates =
{-| -}
subscriptions : ModuleStates -> Sub Msg
subscriptions moduleStates =
Sub.batch
[ moduleStates.modal.subscriptions moduleStates.modal.state
]
allExamples moduleStates
|> List.map (\example -> example.subscriptions example.state)
|> Sub.batch
{-| -}
view : Bool -> (Example state msg -> Bool) -> ModuleStates -> List (Html Msg)
view : Bool -> (Example State Msg -> Bool) -> ModuleStates -> List (Html Msg)
view showFocusLink filter moduleStates =
-- TODO add the filter back in
[ Example.view showFocusLink moduleStates.accordion
, Example.view showFocusLink moduleStates.button
, Example.view showFocusLink moduleStates.bannerAlert
, Example.view showFocusLink moduleStates.modal
allExamples moduleStates
|> List.filter filter
|> List.map (Example.view showFocusLink)
allExamples : ModuleStates -> List (Example State Msg)
allExamples moduleStates =
List.map (\accessor -> accessor moduleStates)
[ .accordion
, .button
, .bannerAlert
, .modal
]