elm-pages-v3-beta/examples/pokedex/src/Page/FileUpload.elm

111 lines
2.6 KiB
Elm
Raw Normal View History

2021-12-30 02:57:04 +03:00
module Page.FileUpload exposing (Data, Model, Msg, page)
import DataSource exposing (DataSource)
import Head
import Head.Seo as Seo
import Html
import Html.Attributes as Attr
import Page exposing (Page, PageWithState, StaticPayload)
import PageServerResponse exposing (PageServerResponse)
import Pages.PageUrl exposing (PageUrl)
import Pages.Url
import Server.Request as Request
import Shared
import View exposing (View)
type alias Model =
{}
type alias Msg =
Never
type alias RouteParams =
{}
page : Page RouteParams Data
page =
Page.serverRender
{ head = head
, data = data
}
|> Page.buildNoState { view = view }
type alias Data =
Maybe Request.File
data : RouteParams -> Request.Handler (PageServerResponse Data)
data routeParams =
Request.oneOfHandler
[ Request.expectMultiPartFormPost
(\{ field, optionalField, fileField } ->
fileField "file"
)
|> Request.thenRespond
(\file ->
DataSource.succeed (PageServerResponse.RenderPage (Just file))
)
, Request.succeed ()
|> Request.thenRespond
(\() ->
DataSource.succeed (PageServerResponse.RenderPage Nothing)
)
]
head :
StaticPayload Data RouteParams
-> List Head.Tag
head static =
Seo.summary
{ canonicalUrlOverride = Nothing
, siteName = "elm-pages"
, image =
{ url = Pages.Url.external "TODO"
, alt = "elm-pages logo"
, dimensions = Nothing
, mimeType = Nothing
}
, description = "TODO"
, locale = Nothing
, title = "TODO title" -- metadata.title -- TODO
}
|> Seo.website
view :
Maybe PageUrl
-> Shared.Model
-> StaticPayload Data RouteParams
-> View Msg
view maybeUrl sharedModel static =
{ title = "File Upload"
, body =
[ static.data
|> Maybe.map
(\file ->
Html.div []
[ Html.h1 [] [ Html.text "Got file" ]
, Html.p [] [ Html.text file.name ]
]
)
|> Maybe.withDefault (Html.text "No file uploaded. Choose a file to get started.")
, Html.form [ Attr.method "POST", Attr.enctype "multipart/form-data" ]
[ Html.input
[ Attr.type_ "file"
, Attr.name "file"
]
[]
, Html.input
[ Attr.type_ "submit"
]
[]
]
]
}