MainPage refactor

This commit is contained in:
Simon Hardt
2021-05-27 16:54:58 +02:00
parent 7903b0b8c5
commit 91776d9591
3 changed files with 136 additions and 106 deletions

View File

@@ -1,5 +1,7 @@
module Color exposing (..) module Color exposing (..)
import Element exposing (rgb, rgba)
white = white =
Element.rgb 1 1 1 Element.rgb 1 1 1

View File

@@ -15,81 +15,33 @@ import Element.Font as Font
import Element.Input as Input import Element.Input as Input
import Element.Region as Region import Element.Region as Region
import Html exposing (header) import Html exposing (header)
import MainPage
white =
Element.rgb 1 1 1
grey =
Element.rgb 0.9 0.9 0.9
blue =
Element.rgb 0 0 0.8
red =
Element.rgb 0.8 0 0
darkBlue =
Element.rgb 0 0 0.9
type alias Form =
{ url: String
}
type Model type Model
= AddPage Form = PageMain MainPage.Model
| Quere
type Msg type Msg
= UpdateForm Form = MsgMain MainPage.Msg
| AddUrl Form
| UrlAdded (Result Http.Error UrlAddedResponse)
initModel : Model initModel : Model
initModel = initModel =
AddPage PageMain MainPage.initModel
{ url = ""
}
type alias UrlAddedResponse = { id: Int, status: String } combineModel : (MainPage.Model, Cmd MainPage.Msg) -> (Model, Cmd Msg)
urlAddDecoder : Decoder UrlAddedResponse combineModel (mainPageModell, cmd) =
urlAddDecoder = (PageMain mainPageModell, Cmd.map MsgMain cmd)
succeed UrlAddedResponse
|> required "id" int
|> required "status" string
urlAddEncode : Form -> Encode.Value
urlAddEncode form = Encode.object
[ ("url", Encode.string form.url)
]
-- Http.header "Access-Control-Request-Method" "POST"
-- ,
update : Msg -> Model -> (Model, Cmd Msg) update : Msg -> Model -> (Model, Cmd Msg)
update msg model = update msg model =
case (msg, model) of case (msg, model) of
(UpdateForm new_form, AddPage _) -> (MsgMain l_msg, PageMain l_model) ->
(AddPage new_form, Cmd.none) combineModel (MainPage.update l_msg l_model)
(AddUrl form, AddPage _) -> -- ( _, _ ) ->
( model -- Debug.todo "branch '( Decrement, _ )' not implemented"
, Http.request
{ method = "POST"
, headers =
[
]
, url = "http://127.0.0.1/api/add"
, body = Http.jsonBody (urlAddEncode form)
, expect = Http.expectJson UrlAdded (urlAddDecoder)
, timeout = Nothing
, tracker = Nothing
}
)
( _, _ ) ->
Debug.todo "branch '( Decrement, _ )' not implemented"
view : Model -> Html Msg view : Model -> Html Msg
view model = view model =
@@ -98,53 +50,13 @@ view model =
Font.size 20 Font.size 20
] ]
<| case (model) of <| case (model) of
(AddPage form) -> (PageMain mainModell) ->
Element.column MainPage.view mainModell |> Element.map MsgMain
[ width (px 800)
, height shrink
, centerX
, centerY
, spacing 36
, padding 10
]
[ el
[ Region.heading 1
, alignLeft
, Font.size 36
]
(text "localTube")
, Input.text
[ spacing 12
, below
(el
[ Font.color red
, Font.size 14
, alignRight
, moveDown 6]
(text "This one is wrong")
)
]
{ text = form.url
, placeholder = Just (Input.placeholder [] (text "http://youtube.com"))
, onChange = \new -> UpdateForm { form | url = new }
, label = Input.labelAbove [ Font.size 14 ](text "Video Url")
}
, Input.button
[ Background.color blue
, Font.color white
, Border.color darkBlue
, paddingXY 32 16
, Border.rounded 3
]
{ onPress = Just (AddUrl form)
, label = Element.text "Add Link to Query"
}
]
(_) -> -- (_) ->
el -- el
[ Region.heading 1 -- [ Region.heading 1
, Font.size 36](text "Non") -- , Font.size 36](text "Non")
main: Program () Model Msg main: Program () Model Msg

View File

@@ -0,0 +1,116 @@
module MainPage exposing (..)
import Http
import Json.Decode exposing (Decoder, bool, int, list, string, succeed)
import Json.Decode.Pipeline exposing(optional, required)
import Json.Encode as Encode
import Element exposing (..)
import Element.Background as Background
import Element.Border as Border
import Element.Font as Font
import Element.Input as Input
import Element.Region as Region
import Html exposing (header)
import Color exposing (..)
type alias Form =
{ url: String
}
type alias Model
= { form: Form }
initModel : Model
initModel
= { form =
{ url = ""
}
}
type Msg
= UpdateForm Form
| PostForm Form
| PostResult (Result Http.Error PostFormResponse)
-- -- Request -- --
type alias PostFormResponse = { id: Int, status: String }
formDecoder : Decoder PostFormResponse
formDecoder =
succeed PostFormResponse
|> required "id" int
|> required "status" string
formPostEncoder : Form -> Encode.Value
formPostEncoder form = Encode.object
[ ("url", Encode.string form.url)
]
-- -- Update -- --
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case (msg) of
(UpdateForm new_form) ->
( {model | form = new_form } , Cmd.none)
(PostForm form) ->
( model
, Http.post
{ url = "http://127.0.0.1/api/add"
, body = Http.jsonBody (formPostEncoder form)
, expect = Http.expectJson PostResult formDecoder
}
)
(_) -> ( model, Cmd.none )
-- -- view -- --
view : Model -> Element Msg
view model =
Element.column
[ width (px 800)
, height shrink
, centerX
, centerY
, spacing 36
, padding 10
]
[ el
[ Region.heading 1
, alignLeft
, Font.size 36
]
(text "localTube")
, Input.text
[ spacing 12
, below
(el
[ Font.color red
, Font.size 14
, alignRight
, moveDown 6]
(text "This one is wrong")
)
]
{ text = model.form.url
, placeholder = Just (Input.placeholder [] (text "http://youtube.com"))
, onChange = \new -> UpdateForm { model | form = { url = new } }.form
, label = Input.labelAbove [ Font.size 14 ](text "Video Url")
}
, Input.button
[ Background.color blue
, Font.color white
, Border.color darkBlue
, paddingXY 32 16
, Border.rounded 3
]
{ onPress = Just (PostForm model.form)
, label = Element.text "Download"
}
]