Skip to content

Instantly share code, notes, and snippets.

@PifyZ
Last active June 8, 2016 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PifyZ/cd6318d45f58d528038815709d3c4643 to your computer and use it in GitHub Desktop.
Save PifyZ/cd6318d45f58d528038815709d3c4643 to your computer and use it in GitHub Desktop.
import Array
import String
import Html.App as Html
import Json.Decode as Json
import Array exposing (Array)
import Html.Events exposing (keyCode, on, onClick, onInput, targetValue)
import Html exposing (..)
import Html.Attributes exposing (..)
main : Program Never
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = always Sub.none
}
type ProjectType
= Unknown
| Wordpress
| Prestashop
| Php
| Other String
type alias Project =
{ name : String
, type' : ProjectType
, coreVersion : String
, coreModified : Bool
, content : String
, uid : Int
}
type alias Model =
{ input : String
, projects : Array Project
, current : Maybe Project
, uid : Int
}
initProject : Int -> String -> Project
initProject uid name =
{ name = name
, type' = Unknown
, coreVersion = ""
, coreModified = False
, content = ""
, uid = uid
}
init : (Model, Cmd Msg)
init =
(Model "" Array.empty Nothing 0, Cmd.none)
type Msg
= NoOp
| Input String
| NewProject
| ChangeCurrent Int
update : Msg -> Model -> (Model, Cmd Msg)
update action model =
case action of
NoOp ->
(model, Cmd.none)
Input newInput ->
({ model | input = newInput}, Cmd.none)
NewProject ->
let projects =
if String.isEmpty model.input then
model.projects
else
let
project = initProject model.uid model.input
in
Array.push project model.projects
in
({ model
| input = ""
, projects = projects
, uid = (model.uid + 1) }
, Cmd.none)
ChangeCurrent uid ->
let
current = Array.get 0 (Array.filter (\x -> x.uid == uid) model.projects)
in
({ model | current = current }, Cmd.none)
view : Model -> Html Msg
view model =
div [] [ header', section' model, footer' ]
header' : Html Msg
header' =
header [] [ text "Projects manager" ]
onEnter : Msg -> Msg -> Attribute Msg
onEnter fail action =
let
tagger code =
if code == 13 then
action
else
fail
in
on "keyup" (Json.map tagger keyCode)
targetValueIntDecoder : Json.Decoder Int
targetValueIntDecoder =
targetValue `Json.andThen` \val ->
case String.toInt val of
Ok i ->
Json.succeed i
Err err ->
Json.fail err
section' : Model -> Html Msg
section' model =
let
projects = Array.toList model.projects
in
section []
[ div []
[ label [] [ text "Nom du projet" ]
, input [ onInput Input, onEnter NoOp NewProject, value model.input, type' "text" ] []
]
, div []
[ button [ onClick NewProject ] [ text "Valider" ]
]
, projectsListView projects
, projectsListUlView projects
, uProjectView model.current
, typesListView
]
footer' : Html Msg
footer' =
footer [] [ text "2016" ]
listTypes : List ProjectType
listTypes =
[ Unknown
, Wordpress
, Prestashop
, Php
, Other "Autre"
]
typesListView : Html Msg
typesListView =
select [] (List.map typeView (List.map typeToString listTypes))
typeView : String -> Html Msg
typeView type' =
option [] [ text type' ]
projectsListView : List Project -> Html Msg
projectsListView projects =
select [ on "change" (Json.map ChangeCurrent targetValueIntDecoder) ] (List.map projectView (List.sortBy (.name) projects))
projectView : Project -> Html Msg
projectView project =
option [ value (toString project.uid) ] [ text project.name ]
projectsListUlView : List Project -> Html Msg
projectsListUlView projects =
ul [] (List.map projectUlView (List.sortBy (.name) projects))
projectUlView : Project -> Html Msg
projectUlView project =
li [] [ text project.name ]
typeToString : ProjectType -> String
typeToString projectType =
case projectType of
Unknown ->
"Inconnu"
Wordpress ->
"WordPress"
Prestashop ->
"PrestaShop"
Php ->
"PHP"
Other type' ->
type'
uProjectView : Maybe Project -> Html Msg
uProjectView project =
case project of
Nothing ->
div [] [ text "-- ERROR --" ]
Just p ->
div []
[ div []
[ span [] [ text "Nom du projet : " ]
, span [] [ text p.name ]
]
, div []
[ span [] [ text "Type : " ]
, span [] [ text (typeToString p.type') ]
]
, div []
[ span [] [ text "Version du cœur : " ]
, span [] [ text p.coreVersion ]
]
, div []
[ span [] [ text "Cœur modifié : " ]
, span [] [ text (if p.coreModified then "Oui" else "Non") ]
]
, div []
[ span [] [ text "Contenu : " ]
, span [] [ text p.content ]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment