[Fencommits] fenserve: edit links and delete and new item actions to table view
Tuukka Hastrup
Tuukka.Hastrup at iki.fi
Wed May 23 13:39:40 EEST 2007
Wed May 23 13:36:36 EEST 2007 Tuukka Hastrup <Tuukka.Hastrup at iki.fi>
* edit links and delete and new item actions to table view
diff -rN -u old-fenserve/fendata/Main.hs new-fenserve/fendata/Main.hs
--- old-fenserve/fendata/Main.hs 2007-05-23 13:39:40.000000000 +0300
+++ new-fenserve/fendata/Main.hs 2007-05-23 13:39:40.000000000 +0300
@@ -388,9 +388,16 @@
showTable :: () -> MyState -> (String, MyState)
showTable () s = (
- concatFor (Map.toList $ stateItems s) $ \(_, Item fields _) ->
- ("<p>"++) . (++"<hr>") . concatFor (Map.toList fields) $ \(f:fs,v) ->
- "<b>" ++ (toUpper f : fs) ++ ":</b> " ++ v ++ "<br>",
+ (concatFor (Map.toList $ stateItems s) $ \(id, Item fields _) ->
+ (("<p><a href='item/"++show id++"'>[edit]</a><br>")++)
+ . (++"<form action='delItem' method=post>\
+ \<input type=hidden name=item value='"++show id++"'>\
+ \<input type=submit value='Delete item'></form><hr>")
+ . concatFor (Map.toList fields) $ \(f:fs,v) ->
+ "<b>" ++ (toUpper f : fs) ++ ":</b> " ++ v ++ "<br>")
+ ++ (concatFor (Map.keys $ stateSchema s) $ \cat ->
+ "<form action='newItem' method=post>\
+ \<input type=submit name=action value='New "++cat++"'></form>"),
s)
showItem :: String -> () -> MyState -> (String, MyState)
@@ -403,12 +410,22 @@
s)
updateItem :: String -> Request -> MyState -> (String, MyState)
-updateItem item_s req s = (fst $ showItem item_s () s', s') where
+updateItem item_s req s = ("/table", s') where
item = read item_s
s' = s { stateItems = Map.insert item (Item fields' cats) (stateItems s) }
Item fields cats = stateItems s Map.! item
fields' = flip Map.mapWithKey fields $ \k v -> fromMaybe v (lookM req k)
-
+
+newItem msg s = ("item/"++show id, s') where
+ id = nextId $ stateItems s
+ s' = s { stateItems = Map.insert id item $ stateItems s }
+ item = Item (Map.fromList $ [(f,"") | f <- catFs]) (Set.fromList [cat])
+ catFs = stateSchema s Map.! cat
+ Just cat = fmap (drop 4) $ lookM msg "action"
+
+delItem msg s = ("table", s') where
+ s' = s { stateItems = Map.delete id $ stateItems s }
+ Just id = fmap read $ lookM msg "item"
main = stdHTTP [ debugFilter
, h ["potion"] GET $ ok $ \() -> run $
@@ -424,7 +441,10 @@
, h ["makefun"] GET $ ok $ \() -> run makeFun
, h ["table"] GET $ ok $ \() -> run showTable
, h (Prefix ["item"]) GET $ ok $ \[item] -> run (showItem item)
- , h (Prefix ["item"]) POST $ ok $ \[item] -> run (updateItem item)
+ , h (Prefix ["item"]) POST $ seeOther $ \[item] ->
+ runRedirect (updateItem item)
+ , h ["newItem"] POST $ seeOther $ \() -> runRedirect newItem
+ , h ["delItem"] POST $ seeOther $ \() -> runRedirect delItem
, h [""] GET $ ok $ \() () -> view
, h ["addfield"] POST $ ok $ \() (cid,name) -> addField cid name
More information about the Fencommits
mailing list