Commit a5ebcb08 authored by xushu's avatar xushu

cleaning up; initial version result page

parent 331e738b
......@@ -21,6 +21,7 @@ As your code changes, your site will be automatically be recompiled and redeploy
```
stack test --flag yesod-webproject:library-only --flag yesod-webproject:dev
stack test
```
(Because `yesod devel` passes the `library-only` and `dev` flags, matching those flags means you don't need to recompile between tests and development, and it disables optimization to speed up your test compile times).
......
......@@ -8,4 +8,6 @@
/ HomeR GET POST
/success ResultR GET
/comments CommentR POST
......@@ -37,6 +37,7 @@ import System.Log.FastLogger (defaultBufSize, newStdoutLoggerSet,
-- Don't forget to add new modules to your cabal file!
import Handler.Common
import Handler.Home
import Handler.Result
import Handler.Comment
-- This line actually creates our YesodDispatch instance. It is the second half
......
......@@ -13,7 +13,7 @@ import Text.Julius (RawJS (..))
import Yesod.Core
import Yesod.Form
data RegistrationForm = RegistrationForm
data Registration = Registration
{ name :: Text
, email :: Text
, futter :: Food
......@@ -27,37 +27,30 @@ data Food = Vegan | Vegetarisch | Alles
getHomeR :: Handler Html
getHomeR = do
(formWidget, formEnctype) <- generateFormPost registrationForm
let submission = Nothing :: Maybe RegistrationForm
(formWidget, formEnctype) <- generateFormPost $ renderBootstrap3 BootstrapBasicForm $ registrationForm Nothing
let submission = Nothing :: Maybe Registration
handlerName = "getHomeR" :: Text
defaultLayout $ do
let (commentFormId, commentTextareaId, commentListId) = commentIds
aDomId <- newIdent
setTitle "GRODEN 2018 - Anmeldung"
$(widgetFile "homepage")
postHomeR :: Handler Html
postHomeR = do
((result, formWidget), formEnctype) <- runFormPost registrationForm
let handlerName = "postHomeR" :: Text
submission = case result of
FormSuccess res -> Just res
_ -> Nothing
defaultLayout $ do
let (commentFormId, commentTextareaId, commentListId) = commentIds
aDomId <- newIdent
setTitle "GRODEN 2018 - Angemeldet"
$(widgetFile "homepage")
registrationForm :: Form RegistrationForm
registrationForm = renderBootstrap3 BootstrapBasicForm $ RegistrationForm
<$> areq textField nameSettings Nothing
<*> areq textField mailSettings Nothing
<*> areq (selectFieldList foods) futterSettings Nothing
<*> areq textField schlafenSettings Nothing
((result, formWidget), formEnctype) <- runFormPost $ renderBootstrap3 BootstrapBasicForm $ registrationForm Nothing
let submission = Nothing :: Maybe Registration
case result of
FormSuccess res -> do
redirect $ ResultR
_ -> defaultLayout $(widgetFile "homepage")
registrationForm :: Maybe Registration -> AForm Handler Registration
registrationForm mRegistrationForm = Registration
<$> areq textField nameSettings (name <$> mRegistrationForm)
<*> areq textField mailSettings (email <$> mRegistrationForm)
<*> areq (selectFieldList foods) futterSettings (futter <$> mRegistrationForm)
<*> areq textField schlafenSettings (schlafen <$> mRegistrationForm)
<*> fileAFormReq "Choose a file"
<*> areq textField textSettings Nothing
<*> areq textField textSettings (fileDescription <$> mRegistrationForm)
where textSettings = FieldSettings "What's on the file?" Nothing Nothing Nothing [ ("class", "form-control"), ("placeholder", "File description")]
nameSettings = FieldSettings "Name" Nothing (Just "name") (Just "name") [ ("class", "form-control"), ("placeholder", "Lian")]
mailSettings = FieldSettings "E-Mail" Nothing (Just "email") (Just "email") [ ("class", "form-control"), ("placeholder", "mail@example.com") ]
......@@ -65,6 +58,3 @@ registrationForm = renderBootstrap3 BootstrapBasicForm $ RegistrationForm
schlafenSettings = FieldSettings "Schlafen" Nothing (Just "schlafen") (Just "schlafen") [ ("class", "form-control"), ("placeholder", "Eigenes Zelt, Gemeinschaftsschlafraum, Eigener Wohnwagen/Auto (nicht auf Gelände!), Ich übernachte nicht")]
foods :: [(Text, Food)]
foods = [("vegan", Vegan), ("vegetarisch", Vegetarisch), ("alles", Alles)]
commentIds :: (Text, Text, Text)
commentIds = ("js-commentForm", "js-createCommentTextarea", "js-commentList")
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE QuasiQuotes #-}
module Handler.Result where
import Import
import Yesod.Form.Bootstrap3 (BootstrapFormLayout (..), renderBootstrap3)
import Text.Julius (RawJS (..))
getResultR :: Handler Html
getResultR = error "TODO"
......@@ -21,33 +21,10 @@
<div .col-lg-4.col-lg-offset-1>
<div .bs-callout.bs-callout-info.upload-response>
$maybe (RegistrationForm name email futter schlafen info con) <- submission
$maybe (Registration name email futter schlafen info con) <- submission
Your file type is <em>#{fileContentType info}</em>. You say it has: <em>#{con}</em>
$nothing
File upload result will be here...
<hr>
<!-- JSON
================================================== -->
<div .bs-docs-section>
<div .row>
<div .col-lg-6>
<div .bs-callout.bs-callout-info.well>
<form .form-horizontal ##{commentFormId}>
<div .field>
<textarea rows="2" ##{commentTextareaId} placeholder="Your comment here..." required></textarea>
<button .btn.btn-primary type=submit>
Create comment
<div .col-lg-4.col-lg-offset-1>
<div .bs-callout.bs-callout-info>
<small>
Your comments will appear here. You can also open the
console log to see the raw response from the server.
<ul ##{commentListId}>
<hr>
document.getElementById(#{toJSON aDomId}).innerHTML = "This text was added by the Javascript part of the homepage widget.";
$(function() {
$("##{rawJS commentFormId}").submit(function(event) {
event.preventDefault();
var message = $("##{rawJS commentTextareaId}").val();
// (Browsers that enforce the "required" attribute on the textarea won't see this alert)
if (!message) {
alert("Please fill out the comment form first.");
return;
}
// Make an AJAX request to the server to create a new comment
$.ajax({
url: '@{CommentR}',
type: 'POST',
contentType: "application/json",
data: JSON.stringify({
message: message,
}),
success: function (data) {
var newNode = $("<li></li>");
newNode.text(data.message);
console.log(data);
$("##{rawJS commentListId}").append(newNode);
},
error: function (data) {
console.log("Error creating comment: " + data);
},
});
});
});
h2##{aDomId} {
color: #990
}
li {
line-height: 2em;
font-size: 16px
}
##{commentTextareaId} {
width: 400px;
height: 100px;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment