/
Update.elm
63 lines (46 loc) · 1.7 KB
/
Update.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module Update exposing (..)
import Commands exposing (fetch, fetchContact)
import Messages exposing (..)
import Model exposing (..)
import Navigation
import Routing exposing (Route(..), parse, toPath)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
FetchResult (Ok response) ->
{ model | contactList = Success response } ! []
FetchResult (Err error) ->
{ model | contactList = Failure "Something went wrong..." } ! []
Paginate pageNumber ->
model ! [ fetch pageNumber model.search ]
HandleSearchInput value ->
{ model | search = value } ! []
HandleFormSubmit ->
{ model | contactList = Requesting } ! [ fetch 1 model.search ]
ResetSearch ->
{ model | search = "" } ! [ fetch 1 "" ]
UrlChange location ->
let
currentRoute =
parse location
in
urlUpdate { model | route = currentRoute }
NavigateTo route ->
model ! [ Navigation.newUrl <| toPath route ]
FetchContactResult (Ok response) ->
{ model | contact = Success response } ! []
FetchContactResult (Err error) ->
{ model | contact = Failure "Contact not found" } ! []
urlUpdate : Model -> ( Model, Cmd Msg )
urlUpdate model =
case model.route of
HomeIndexRoute ->
case model.contactList of
NotRequested ->
model ! [ fetch 1 "" ]
_ ->
model ! []
ShowContactRoute id ->
{ model | contact = Requesting } ! [ fetchContact id ]
_ ->
model ! []