From 890037144b7aa11d69d251b8e8f9648d55c5c8d7 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Sun, 16 Feb 2025 19:15:39 -0800 Subject: [PATCH] update --- elm.json | 5 ++- src/HTTP.elm | 85 +++++++++++++++++++++++++++++++++++++ src/PasswordValidation.elm | 87 ++++++++++++++++++++++---------------- 3 files changed, 140 insertions(+), 37 deletions(-) create mode 100644 src/HTTP.elm diff --git a/elm.json b/elm.json index ce2a08d..47f4870 100644 --- a/elm.json +++ b/elm.json @@ -8,9 +8,12 @@ "direct": { "elm/browser": "1.0.2", "elm/core": "1.0.5", - "elm/html": "1.0.0" + "elm/html": "1.0.0", + "elm/http": "2.0.0" }, "indirect": { + "elm/bytes": "1.0.8", + "elm/file": "1.0.5", "elm/json": "1.1.3", "elm/time": "1.0.0", "elm/url": "1.0.0", diff --git a/src/HTTP.elm b/src/HTTP.elm new file mode 100644 index 0000000..af126d4 --- /dev/null +++ b/src/HTTP.elm @@ -0,0 +1,85 @@ +module HTTP exposing (..) + +import Browser +import Html exposing (Html, pre, text) +import Http + + + +-- MAIN + + +main : Program () Model Msg +main = + Browser.element + { init = init + , update = update + , subscriptions = subscriptions + , view = view + } + + + +-- MODEL + + +type Model + = Failure + | Loading + | Success String + + +init : () -> ( Model, Cmd Msg ) +init _ = + ( Loading + , Http.get + { url = "https://elm-lang.org/assets/public-opinion.txt" + , expect = Http.expectString GotText + } + ) + + + +-- UPDATE + + +type Msg + = GotText (Result Http.Error String) + + +update : Msg -> Model -> ( Model, Cmd Msg ) +update msg model = + case msg of + GotText result -> + case result of + Ok fullText -> + ( Success fullText, Cmd.none ) + + Err _ -> + ( Failure, Cmd.none ) + + + +-- SUBSCRIPTIONS + + +subscriptions : Model -> Sub Msg +subscriptions model = + Sub.none + + + +-- VIEW + + +view : Model -> Html Msg +view model = + case model of + Failure -> + text "I was unable to load your book." + + Loading -> + text "Loading..." + + Success fullText -> + pre [] [ text fullText ] diff --git a/src/PasswordValidation.elm b/src/PasswordValidation.elm index 15d4329..7823edb 100644 --- a/src/PasswordValidation.elm +++ b/src/PasswordValidation.elm @@ -1,4 +1,5 @@ module PasswordValidation exposing (..) + import Browser import Html exposing (..) import Html.Attributes exposing (..) @@ -10,7 +11,7 @@ import Html.Events exposing (onInput) main = - Browser.sandbox { init = init, update = update, view = view } + Browser.sandbox { init = init, update = update, view = view } @@ -18,14 +19,15 @@ main = type alias Model = - { name : String - , password : String - , passwordAgain : String - } + { name : String + , password : String + , passwordAgain : String + } init : Model -init = Model "" "" "" +init = + Model "" "" "" @@ -33,22 +35,22 @@ init = Model "" "" "" type Msg - = Name String - | Password String - | PasswordAgain String + = Name String + | Password String + | PasswordAgain String update : Msg -> Model -> Model update msg model = - case msg of - Name name -> - { model | name = name } - - Password password -> - { model | password = password } + case msg of + Name name -> + { model | name = name } - PasswordAgain password -> - { model | passwordAgain = password } + Password password -> + { model | password = password } + + PasswordAgain password -> + { model | passwordAgain = password } @@ -57,35 +59,48 @@ update msg model = view : Model -> Html Msg view model = - div [] - [ viewInput "text" "Name" model.name Name - , viewInput "password" "Password" model.password Password - , viewInput "password" "Re-enter Password" model.passwordAgain PasswordAgain - , viewValidation model - ] + div [] + [ viewInput "text" "Name" model.name Name + , viewInput "password" "Password" model.password Password + , viewInput "password" "Re-enter Password" model.passwordAgain PasswordAgain + , viewValidation model + ] + viewInput : String -> String -> String -> (String -> msg) -> Html msg viewInput t p v toMsg = - input [ type_ t, placeholder p, value v, onInput toMsg ] [] + input [ type_ t, placeholder p, value v, onInput toMsg ] [] + viewValidation : Model -> Html msg viewValidation model = - if List.length (passwordHints model.password) > 0 then - div [ style "color" "red" ] (passwordHints model.password) - else if model.password /= model.passwordAgain then - div [ style "color" "red" ] [ text "Passwords do not match!" ] - else - div [ style "color" "green" ] [ text "OK" ] + if List.length (passwordHints model.password) > 0 then + div [ style "color" "red" ] (passwordHints model.password) + + else if model.password /= model.passwordAgain then + div [ style "color" "red" ] [ text "Passwords do not match!" ] + + else + div [ style "color" "green" ] [ text "OK" ] + passwordHints : String -> List (Html msg) passwordHints s = - (optional (String.length s <= 8) (text "Password must be at least 8 characters. ")) - ++ (optional (String.toUpper s == s) (text "Password must contain an uppercase letter. ")) - ++ (optional (String.toLower s == s) (text "Password must contain a lowercase letter. ")) - ++ (optional (not (containsNumeric s)) (text "Password must contain a number. ")) + optional (String.length s <= 8) (text "Password must be at least 8 characters. ") + ++ optional (String.toUpper s == s) (text "Password must contain an uppercase letter. ") + ++ optional (String.toLower s == s) (text "Password must contain a lowercase letter. ") + ++ optional (not (containsNumeric s)) (text "Password must contain a number. ") + optional : Bool -> Html msg -> List (Html msg) -optional b xs = if b then [xs] else [] +optional b xs = + if b then + [ xs ] + + else + [] + containsNumeric : String -> Bool -containsNumeric s = List.any (\b -> b) (List.map (\x -> String.contains x s) ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]) +containsNumeric s = + List.any (\b -> b) (List.map (\x -> String.contains x s) [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" ])