-
Notifications
You must be signed in to change notification settings - Fork 13
/
Settings.elm
126 lines (101 loc) · 3.64 KB
/
Settings.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
module Pages.Settings exposing (Model, Msg(..), currentSharedStateView, getTranslations, initModel, selectionButton, translationRow, update, view)
import Browser.Navigation exposing (pushUrl)
import Decoders
import Dict
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (css)
import Html.Styled.Events exposing (..)
import I18n
import RemoteData exposing (RemoteData(..), WebData)
import RemoteData.Http
import Routing.Helpers exposing (Route(..), reverseRoute)
import SharedState exposing (SharedState, SharedStateUpdate(..))
import Styles exposing (..)
import Time
import Types exposing (Language(..), Translations)
type alias Model =
{ selectedLanguage : Language
}
type Msg
= SelectLanguage Language
| HandleTranslationsResponse (WebData Translations)
| NavigateTo Route
initModel : Model
initModel =
{ selectedLanguage = English
}
update : SharedState -> Msg -> Model -> ( Model, Cmd Msg, SharedStateUpdate )
update sharedState msg model =
case msg of
SelectLanguage lang ->
( { model | selectedLanguage = lang }
, getTranslations lang
, NoUpdate
)
HandleTranslationsResponse webData ->
case webData of
Success translations ->
( model, Cmd.none, UpdateTranslations translations )
_ ->
( model, Cmd.none, NoUpdate )
NavigateTo route ->
( model, pushUrl sharedState.navKey (reverseRoute route), NoUpdate )
getTranslations : Language -> Cmd Msg
getTranslations language =
let
url =
case language of
English ->
"/api/en.json"
Finnish ->
"/api/fi.json"
FinnishFormal ->
"/api/fi-formal.json"
in
RemoteData.Http.get url HandleTranslationsResponse Decoders.decodeTranslations
view : SharedState -> Model -> Html Msg
view sharedState model =
let
t =
I18n.get sharedState.translations
in
div []
[ h2 [] [ text (t "language-selection-heading") ]
, selectionButton model English "English"
, selectionButton model FinnishFormal "Suomi (virallinen)"
, selectionButton model Finnish "Suomi (puhekieli)"
, h2 [] [ text (t "navigation-button-heading") ]
, p [] [ text (t "navigation-button-desc") ]
, button [ onClick (NavigateTo HomeRoute), css actionButton ]
[ text (t "page-title-home") ]
, h2 [] [ text (t "current-sharedState-heading") ]
, currentSharedStateView sharedState
]
currentSharedStateView : SharedState -> Html never
currentSharedStateView sharedState =
div [ css card ]
[ h4 [ css monospaceFont ] [ text "currentTime" ]
, pre [] [ text (String.fromInt (Time.posixToMillis sharedState.currentTime)) ]
, h4 [ css monospaceFont ] [ text "translations" ]
, table [ css sharedStateTable ] (List.map translationRow (Dict.toList sharedState.translations))
]
translationRow : ( String, String ) -> Html never
translationRow ( key, value ) =
tr []
[ td [ css tableCell ] [ text key ]
, td [ css tableCell ] [ text value ]
]
selectionButton : Model -> Language -> String -> Html Msg
selectionButton model language shownName =
let
buttonStyles =
if model.selectedLanguage == language then
actionButtonActive ++ gutterRight
else
actionButton ++ gutterRight
in
button
[ css buttonStyles
, onClick (SelectLanguage language)
]
[ text shownName ]