blob: 621fb97206f32e112145d94d001dcf8414119285 (
plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
module Main
( main
) where
import Graphics.Element exposing (..)
import Html exposing (Html)
import Http
import Task exposing (..)
import Time exposing (..)
import Json.Decode as Json exposing ((:=))
import Model exposing (Model, initialModel)
import Model.User exposing (Users, usersDecoder, UserId, userIdDecoder)
import Model.Payment exposing (Payments, paymentsDecoder, perPage)
import Model.Payers exposing (Payers, payersDecoder)
import Model.Translations exposing (..)
import Update exposing (Action(..), actions, updateModel)
import Update.SignIn exposing (..)
import View.Page exposing (renderPage)
import ServerCommunication exposing (serverCommunications, sendRequest)
main : Signal Html
main = Signal.map renderPage model
model : Signal Model
model = Signal.foldp updateModel (initialModel initialTime translations) update
update : Signal Action
update = Signal.mergeMany
[ Signal.map UpdateTime (Time.every 30)
, actions.signal
]
---------------------------------------
port signInError : Maybe String
---------------------------------------
port initialTime : Time
---------------------------------------
port translations : String
---------------------------------------
port initView : Task Http.Error ()
port initView =
case signInError of
Just msg ->
Signal.send actions.address (SignInError msg)
Nothing ->
Task.onError goLoggedInView (\_ -> Signal.send actions.address GoSignInView)
goLoggedInView : Task Http.Error ()
goLoggedInView =
Task.andThen getUsers <| \users ->
Task.andThen whoAmI <| \me ->
Task.andThen getMonthlyPayments <| \monthlyPayments ->
Task.andThen getPayments <| \payments ->
Task.andThen getPaymentsCount <| \paymentsCount ->
Task.andThen getPayers <| \payers ->
Task.andThen getIncome <| \income ->
Signal.send actions.address (GoLoggedInView users me monthlyPayments payments paymentsCount payers income)
getUsers : Task Http.Error Users
getUsers = Http.get usersDecoder "/users"
whoAmI : Task Http.Error UserId
whoAmI = Http.get ("id" := userIdDecoder) "/whoAmI"
getMonthlyPayments : Task Http.Error Payments
getMonthlyPayments = Http.get paymentsDecoder "/monthlyPayments"
getPayments : Task Http.Error Payments
getPayments = Http.get paymentsDecoder ("/payments?page=1&perPage=" ++ toString perPage)
getPaymentsCount : Task Http.Error Int
getPaymentsCount = Http.get ("number" := Json.int) "/payments/count"
getPayers : Task Http.Error Payers
getPayers = Http.get payersDecoder "/payments/total"
getIncome : Task Http.Error (Maybe Int)
getIncome = Http.get (Json.maybe ("income" := Json.int)) "/income"
---------------------------------------
port serverCommunicationsPort : Signal (Task Http.RawError ())
port serverCommunicationsPort =
Signal.map
(\comm -> sendRequest comm `Task.andThen` (Signal.send actions.address))
serverCommunications.signal
|