Permalink
Browse files

Simplify new task creation code

  • Loading branch information...
samstokes committed Sep 12, 2011
1 parent 486701e commit ad7c6413d0435cdd7f133df15e0cb244d7e0900f
Showing with 12 additions and 8 deletions.
  1. +6 −8 Handler/Root.hs
  2. +6 −0 Model.hs
View
@@ -46,7 +46,7 @@ getTasksR = maybeAuth >>= getTasksR' where
let (done, pending) = partition (taskDone . snd) tasks
estimates <- mapM (runDB . taskEstimates . fst) tasks
let tasksEstimates = M.fromList $ zip (map fst tasks) estimates
- ((_, taskWidget), enctype) <- generateFormPost $ taskForm userId
+ ((_, taskWidget), enctype) <- generateFormPost taskForm
defaultLayout $ do
setTitle "tasks"
addWidget $(widgetFile "tasks")
@@ -62,21 +62,19 @@ oneButton label route = [whamlet|
<button>#{label}
|]
-taskForm :: UserId -> Html -> Form Yesodoro Yesodoro (FormResult Task, Widget)
-taskForm uid = renderDivs $ Task uid
- <$> areq textField "Title" Nothing
- <*> pure 0
- <*> pure False
+taskForm :: Html -> Form Yesodoro Yesodoro (FormResult NewTask, Widget)
+taskForm = renderDivs $ NewTask <$> areq textField "Title" Nothing
postTasksR :: Handler RepHtml
postTasksR = maybeAuthId >>= postTasksR' where
postTasksR' :: Maybe UserId -> Handler RepHtml
postTasksR' Nothing = redirectTemporary RootR
postTasksR' (Just userId) = do
- ((result, taskWidget), _) <- runFormPost $ taskForm userId
+ ((result, taskWidget), _) <- runFormPost taskForm
case result of
FormSuccess task -> do
- runDB $ insert task
+ let task' = newTask userId task
+ runDB $ insert task'
redirectTemporary TasksR
_ -> undefined -- TODO
View
@@ -17,6 +17,12 @@ share [mkPersist sqlSettings, mkMigrate "migrateAll"] $(persistFile "config/mode
newtype TaskState = TaskState Text
deriving (ToHtml, IsString)
+
+data NewTask = NewTask { newTaskTitle :: Text } deriving (Show)
+
+newTask :: UserId -> NewTask -> Task
+newTask uid (NewTask title) = Task uid title 0 False
+
taskState :: Task -> TaskState
taskState task = if taskDone task then "done" else "pending"

0 comments on commit ad7c641

Please sign in to comment.