2022-05-03 21:51:32 +03:00
|
|
|
module Route.FileUpload exposing (ActionData, Data, Model, Msg, route)
|
2021-12-30 02:57:04 +03:00
|
|
|
|
|
|
|
import DataSource exposing (DataSource)
|
2022-03-29 21:48:04 +03:00
|
|
|
import ErrorPage exposing (ErrorPage)
|
2021-12-30 02:57:04 +03:00
|
|
|
import Head
|
|
|
|
import Head.Seo as Seo
|
|
|
|
import Html
|
|
|
|
import Html.Attributes as Attr
|
2022-05-13 19:03:09 +03:00
|
|
|
import Pages.Msg
|
2021-12-30 02:57:04 +03:00
|
|
|
import Pages.PageUrl exposing (PageUrl)
|
|
|
|
import Pages.Url
|
2022-03-05 20:50:01 +03:00
|
|
|
import RouteBuilder exposing (StatelessRoute, StaticPayload)
|
2021-12-30 02:57:04 +03:00
|
|
|
import Server.Request as Request
|
2022-01-18 07:56:37 +03:00
|
|
|
import Server.Response
|
2021-12-30 02:57:04 +03:00
|
|
|
import Shared
|
|
|
|
import View exposing (View)
|
|
|
|
|
|
|
|
|
|
|
|
type alias Model =
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
type alias Msg =
|
2022-01-06 01:09:37 +03:00
|
|
|
()
|
2021-12-30 02:57:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
type alias RouteParams =
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2022-05-03 21:51:32 +03:00
|
|
|
route : StatelessRoute RouteParams Data ActionData
|
2022-03-05 21:35:17 +03:00
|
|
|
route =
|
2022-03-05 20:50:01 +03:00
|
|
|
RouteBuilder.serverRender
|
2021-12-30 02:57:04 +03:00
|
|
|
{ head = head
|
|
|
|
, data = data
|
2022-05-03 21:51:32 +03:00
|
|
|
, action = \_ -> Request.skip "No action."
|
2021-12-30 02:57:04 +03:00
|
|
|
}
|
2022-03-05 20:50:01 +03:00
|
|
|
|> RouteBuilder.buildNoState { view = view }
|
2021-12-30 02:57:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
type alias Data =
|
|
|
|
Maybe Request.File
|
|
|
|
|
|
|
|
|
2022-05-03 21:51:32 +03:00
|
|
|
type alias ActionData =
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2022-03-29 21:48:04 +03:00
|
|
|
data : RouteParams -> Request.Parser (DataSource (Server.Response.Response Data ErrorPage))
|
2021-12-30 02:57:04 +03:00
|
|
|
data routeParams =
|
2021-12-31 22:14:23 +03:00
|
|
|
Request.oneOf
|
2021-12-30 02:57:04 +03:00
|
|
|
[ Request.expectMultiPartFormPost
|
|
|
|
(\{ field, optionalField, fileField } ->
|
|
|
|
fileField "file"
|
|
|
|
)
|
2021-12-31 22:14:23 +03:00
|
|
|
|> Request.map
|
2021-12-30 02:57:04 +03:00
|
|
|
(\file ->
|
2022-01-18 07:56:37 +03:00
|
|
|
DataSource.succeed (Server.Response.render (Just file))
|
2021-12-30 02:57:04 +03:00
|
|
|
)
|
2021-12-31 22:14:23 +03:00
|
|
|
, Request.succeed
|
2022-01-18 07:56:37 +03:00
|
|
|
(DataSource.succeed (Server.Response.render Nothing))
|
2021-12-30 02:57:04 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
head :
|
2022-05-03 21:51:32 +03:00
|
|
|
StaticPayload Data ActionData RouteParams
|
2021-12-30 02:57:04 +03:00
|
|
|
-> 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
|
2022-05-03 21:51:32 +03:00
|
|
|
-> StaticPayload Data ActionData RouteParams
|
2022-05-13 19:03:09 +03:00
|
|
|
-> View (Pages.Msg.Msg Msg)
|
2021-12-30 02:57:04 +03:00
|
|
|
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"
|
|
|
|
]
|
|
|
|
[]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
}
|