@@ -24,11 +24,11 @@ import Internal.RemoteData as RD
2424import Select as S
2525import Select.Setters as SS
2626
27- type Slot =
27+ type Slot =
2828 S.Slot Query ChildSlots Message
2929
3030type State =
31- ( selections :: Array Location
31+ ( selections :: Array Location
3232 , available :: RD.RemoteData String (Array Location )
3333 )
3434
@@ -39,51 +39,51 @@ data Action
3939data Query a
4040 = GetSelections (Array Location -> a )
4141
42- data Message
43- = ItemRemoved Location
42+ data Message
43+ = ItemRemoved Location
4444 | SelectionsChanged (Array Location )
4545
46- type ChildSlots =
46+ type ChildSlots =
4747 ( dropdown :: D.Slot Unit )
4848
49- -- this typeahead will be opaque; users can just use this pre-built
50- -- input instead of the usual select one.
51- input :: S.Input State
52- input =
53- { inputType: S.Text
54- , debounceTime: Just (Milliseconds 300.0 )
55- , search: Nothing
56- , getItemCount: maybe 0 length <<< RD .toMaybe <<< _.available
57- , selections: []
58- , available: RD.NotAsked
59- }
60-
61- spec :: S.Spec State Query Action ChildSlots Message Aff
62- spec = S .defaultSpec
49+ component :: H.Component HH.HTML (S.Query Query ChildSlots ) Unit Message Aff
50+ component = S .component (const input) $ S .defaultSpec
6351 { render = render
6452 , handleAction = handleAction
6553 , handleQuery = handleQuery
66- , handleMessage = handleMessage
54+ , handleEvent = handleEvent
6755 }
6856 where
69- handleMessage
70- :: S.Message
57+ -- this typeahead will be opaque; users can just use this pre-built
58+ -- input instead of the usual select one.
59+ input :: S.Input State
60+ input =
61+ { inputType: S.Text
62+ , debounceTime: Just (Milliseconds 300.0 )
63+ , search: Nothing
64+ , getItemCount: maybe 0 length <<< RD .toMaybe <<< _.available
65+ , selections: []
66+ , available: RD.NotAsked
67+ }
68+
69+ handleEvent
70+ :: S.Event
7171 -> H.HalogenM (S.State State ) (S.Action Action ) ChildSlots Message Aff Unit
72- handleMessage = case _ of
72+ handleEvent = case _ of
7373 S.Selected ix -> do
7474 st <- H .get
7575 for_ st.available \arr ->
7676 for_ (arr !! ix) \item -> do
7777 let newSelections = item : st.selections
78- H .modify_ _
78+ H .modify_ _
7979 { selections = item : st.selections
8080 , available = RD.Success (filter (_ /= item) arr)
8181 , search = " "
8282 }
8383 H .raise $ SelectionsChanged newSelections
8484 S.Searched str -> do
8585 st <- H .get
86- -- we'll use an external api to search locations
86+ -- we'll use an external api to search locations
8787 H .modify_ _ { available = RD.Loading }
8888 items <- H .liftAff $ searchLocations str
8989 H .modify_ _ { available = items <#> \xs -> difference xs st.selections }
@@ -92,13 +92,13 @@ spec = S.defaultSpec
9292 -- You can remove all type signatures except for this one; we need to tell the
9393 -- compiler about the `a` type variable. The minimal necessary signature is below.
9494 handleQuery :: forall a . Query a -> H.HalogenM _ _ _ _ _ (Maybe a )
95- handleQuery = case _ of
95+ handleQuery = case _ of
9696 GetSelections reply -> do
9797 st <- H .get
9898 pure $ Just $ reply st.selections
9999
100- handleAction
101- :: Action
100+ handleAction
101+ :: Action
102102 -> H.HalogenM (S.State State ) (S.Action Action ) ChildSlots Message Aff Unit
103103 handleAction = case _ of
104104 Remove item -> do
@@ -109,23 +109,23 @@ spec = S.defaultSpec
109109 HandleDropdown msg -> case msg of
110110 D.SelectionChanged oldSelection newSelection -> do
111111 st <- H .get
112- let
112+ let
113113 mkLocation str = { name: " User Added: " <> str, population: " 1" }
114114 newSelections = case oldSelection, newSelection of
115- Nothing , Nothing ->
115+ Nothing , Nothing ->
116116 Nothing
117- Nothing , Just str ->
117+ Nothing , Just str ->
118118 Just (mkLocation str : st.selections)
119- Just str, Nothing ->
119+ Just str, Nothing ->
120120 Just (filter (_ /= mkLocation str) st.selections)
121- Just old, Just new ->
121+ Just old, Just new ->
122122 Just (mkLocation new : (filter (_ /= mkLocation old) st.selections))
123- for_ newSelections \selections ->
123+ for_ newSelections \selections ->
124124 H .modify_ _ { selections = selections }
125125
126126 render :: S.State State -> H.ComponentHTML (S.Action Action ) ChildSlots Aff
127- render st =
128- HH .div
127+ render st =
128+ HH .div
129129 [ class_ " Typeahead" ]
130130 [ renderSelections, renderInput, renderDropdown, renderContainer ]
131131 where
@@ -139,40 +139,40 @@ spec = S.defaultSpec
139139 renderSelectedItem item =
140140 HH .div
141141 [ class_ " Typeahead__item--selected Location" ]
142- [ HH .span
142+ [ HH .span
143143 [ class_ " Location__name" ]
144144 [ HH .text item.name ]
145145 , closeButton item
146146 ]
147147
148148 closeButton item =
149149 HH .span
150- [ class_ " Location__closeButton"
150+ [ class_ " Location__closeButton"
151151 , HE .onClick \_ -> Just $ S.Action $ Remove item
152152 ]
153153 [ HH .text " ×" ]
154154
155155 renderInput = HH .input $ SS .setInputProps
156- [ classes_
156+ [ classes_
157157 [ " Typeahead__input"
158158 , " Typeahead__input--selections" # guard hasSelections
159159 , " Typeahead__input--active" # guard (st.visibility == S.On )
160160 ]
161- , HP .placeholder " Type to search..."
161+ , HP .placeholder " Type to search..."
162162 ]
163163
164164 renderDropdown = whenElem (st.visibility == S.On ) \_ ->
165- HH .slot _dropdown unit ( S .component D .spec) ( D .input dropdownInput) handler
165+ HH .slot _dropdown unit D .component dropdownInput handler
166166 where
167167 _dropdown = SProxy :: SProxy " dropdown"
168168 handler msg = Just $ S.Action $ HandleDropdown msg
169169 dropdownInput = { items: [ " Earth" , " Mars" ], buttonLabel: " Human Planets" }
170170
171171 renderContainer = whenElem (st.visibility == S.On ) \_ ->
172- HH .div
173- (SS .setContainerProps
174- [ classes_
175- [ " Typeahead__container"
172+ HH .div
173+ (SS .setContainerProps
174+ [ classes_
175+ [ " Typeahead__container"
176176 , " Typeahead__container--hasItems" # guard hasItems
177177 ]
178178 ]
@@ -186,14 +186,14 @@ spec = S.defaultSpec
186186 RD.NotAsked -> renderMsg " No search performed..."
187187 RD.Loading -> renderMsg " Loading..."
188188 RD.Failure e -> renderMsg e
189- RD.Success available
189+ RD.Success available
190190 | hasItems -> renderItem `mapWithIndex` available
191191 | otherwise -> renderMsg " No results found"
192192
193193 renderItem index { name, population } =
194- HH .div
195- (SS .setItemProps index [ classes_ [ base, highlight, " Location" ] ])
196- [ HH .span
194+ HH .div
195+ (SS .setItemProps index [ classes_ [ base, highlight, " Location" ] ])
196+ [ HH .span
197197 [ class_ " Location__name" ]
198198 [ HH .text name ]
199199 , HH .span
@@ -214,7 +214,6 @@ type Location =
214214
215215searchLocations :: String -> Aff (RD.RemoteData String (Array Location ))
216216searchLocations search = do
217- res <- AX .get AR .json (" https://swapi.co/api/planets/?search=" <> search)
217+ res <- AX .get AR .json (" https://swapi.co/api/planets/?search=" <> search)
218218 let body = lmap AR .printResponseFormatError res.body
219219 pure $ RD .fromEither $ traverse decodeJson =<< (_ .: " results" ) =<< decodeJson =<< body
220-
0 commit comments