@@ -54,11 +54,27 @@ routes appstateVar =
5454
5555 -- A page for creating a new post
5656 , Twain. get " /new" $
57- Twain. send $ Twain. text " not yet implemented "
57+ Twain. send handleGetNewPost
5858
5959 -- A request to submit a new page
60- , Twain. post " /new" $
61- Twain. send $ Twain. text " not yet implemented"
60+ , Twain. post " /new" $ do
61+ title <- Twain. param " title"
62+ author <- Twain. param " author"
63+ content <- Twain. param " content"
64+ time <- liftIO C. getCurrentTime
65+
66+ response <-
67+ liftIO $ handlePostNewPost
68+ ( Post
69+ { pTitle = title
70+ , pAuthor = author
71+ , pContent = content
72+ , pTime = time
73+ }
74+ )
75+ appstateVar
76+
77+ Twain. send response
6278
6379 -- A request to delete a specific post
6480 , Twain. post " /post/:id/delete" $ do
@@ -105,6 +121,20 @@ handleDeletePost pid appstateVar = do
105121 [(" Content-Type" , " text/html; charset=utf-8" )]
106122 " 404 Not Found."
107123
124+ -- | Respond with the new post page.
125+ handleGetNewPost :: Twain. Response
126+ handleGetNewPost =
127+ Twain. html $
128+ H. renderBS $
129+ template " Bulletin board - posts" $
130+ newPostHtml
131+
132+ -- | Respond with the new post page.
133+ handlePostNewPost :: Post -> STM. TVar AppState -> IO Twain. Response
134+ handlePostNewPost post appstateVar = do
135+ pid <- newPost post appstateVar
136+ pure $ Twain. redirect302 (" /post/" <> T. pack (show pid))
137+
108138-- ** Application state
109139
110140-- | Application state.
@@ -249,3 +279,18 @@ postHtml pid post = do
249279 ( do
250280 H. input_ [H. type_ " submit" , H. value_ " Delete" , H. class_ " deletebtn" ]
251281 )
282+
283+ -- | A new post form.
284+ newPostHtml :: Html
285+ newPostHtml = do
286+ H. form_
287+ [ H. method_ " post"
288+ , H. action_ " /new"
289+ , H. class_ " new-post"
290+ ]
291+ ( do
292+ H. p_ $ H. input_ [H. type_ " text" , H. name_ " title" , H. placeholder_ " Title..." ]
293+ H. p_ $ H. input_ [H. type_ " text" , H. name_ " author" , H. placeholder_ " Author..." ]
294+ H. p_ $ H. textarea_ [H. name_ " content" , H. placeholder_ " Content..." ] " "
295+ H. p_ $ H. input_ [H. type_ " submit" , H. value_ " Submit" , H. class_ " submit-button" ]
296+ )
0 commit comments