Created
May 15, 2017 18:12
-
-
Save anonymous/6eb85f11198eb27d5e68763be2f5db29 to your computer and use it in GitHub Desktop.
contacts page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "1.0.0", | |
"summary": "Tell the world about your project!", | |
"repository": "https://github.com/user/project.git", | |
"license": "BSD3", | |
"source-directories": [ | |
"." | |
], | |
"exposed-modules": [], | |
"dependencies": { | |
"elm-lang/core": "5.1.1 <= v < 5.1.1", | |
"elm-lang/html": "2.0.0 <= v < 2.0.0", | |
"NoRedInk/elm-decode-pipeline": "3.0.0 <= v < 3.0.0" | |
}, | |
"elm-version": "0.18.0 <= v < 0.19.0" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<style> | |
html { | |
background: #F7F7F7; | |
color: red; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
var app = Elm.Main.fullscreen() | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (..) | |
import Html exposing (Html, div, li, text, ul) | |
import Json.Decode as Json exposing (Decoder, andThen, at, fail, int, list, map, nullable, string, succeed) | |
import Json.Decode.Pipeline exposing (custom, decode, hardcoded, optional, required) | |
import String exposing (join) | |
main : Html a | |
main = | |
div [] | |
[ div [] [ text <| "Showing contacts for language " ++ toString model.language ] | |
, viewContacts model.language model.contacts | |
] | |
type Language | |
= Arabic | |
| Hebrew | |
type Name | |
= Name Language String | |
type alias Contact = | |
{ name : List Name | |
, phone : String | |
} | |
contact1 : Contact | |
contact1 = | |
{ name = | |
[ Name Arabic "Foo" | |
, Name Hebrew "Bar" | |
] | |
, phone = "1234" | |
} | |
contact2 : Contact | |
contact2 = | |
{ name = | |
[ Name Hebrew "Nik" | |
] | |
, phone = "5678" | |
} | |
type alias Model = | |
{ language : Language | |
, contacts : List Contact | |
} | |
model : Model | |
model = | |
{ language = Hebrew | |
, contacts = [ contact1, contact2 ] | |
} | |
viewContacts : Language -> List Contact -> Html msg | |
viewContacts language contacts = | |
div [] | |
(List.map | |
(\contact -> | |
ul | |
[] | |
[ li [] [ text <| getContactNameByLanguage language contact ] | |
, li [] [ text contact.phone ] | |
] | |
) | |
(getContactsByLanguage language contacts) | |
) | |
{-| Determine if we have the name of the contact in the given language. | |
-} | |
getContactsByLanguage : Language -> List Contact -> List Contact | |
getContactsByLanguage language contacts = | |
List.filter | |
(\contact -> | |
List.foldl | |
(\name accum -> | |
if accum == True then | |
-- We already found a matching language. | |
True | |
else | |
let | |
-- Extract the language form the name. | |
(Name nameLanguage _) = | |
name | |
in | |
language == nameLanguage | |
) | |
False | |
contact.name | |
) | |
contacts | |
{-| Get the name by language. In case language doesn't exist, we show an emoty | |
string, but that should never be the case. | |
-} | |
getContactNameByLanguage : Language -> Contact -> String | |
getContactNameByLanguage language contact = | |
List.foldl | |
(\name accum -> | |
let | |
(Name nameLanguage nameString) = | |
name | |
in | |
if nameLanguage == language then | |
nameString | |
else | |
accum | |
) | |
"" | |
contact.name | |
jsonExample : String | |
jsonExample = | |
""" | |
{ "name" : { | |
"arabic" : "foo", | |
"hebrew" : "bar" | |
}, | |
"phone" : "1234" | |
} | |
""" | |
decodeContact : Decoder Contact | |
decodeContact = | |
decode Contact | |
|> custom (at [ "name" ] decodeName) | |
|> required "phone" string | |
decodeName : Decoder (List Name) | |
decodeName = | |
Json.keyValuePairs string | |
|> andThen | |
(\names -> | |
let | |
( validNames, invalidNames ) = | |
List.foldl | |
(\( key, val ) ( validNames, invalidNames ) -> | |
if key == "arabic" then | |
( (Name Arabic val) :: validNames | |
, invalidNames | |
) | |
else if key == "hebrew" then | |
( (Name Hebrew val) :: validNames | |
, invalidNames | |
) | |
else | |
( validNames, key :: invalidNames ) | |
) | |
( [], [] ) | |
names | |
in | |
if List.isEmpty invalidNames then | |
succeed validNames | |
else | |
fail <| "Could not recognise names: " ++ String.join ", " invalidNames | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment