1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-27 10:10:50 +03:00

Make -added things- libraries, create links

This commit is contained in:
Artyom 2016-02-02 20:41:29 +03:00
parent 59128a9bca
commit 126e91c38e
2 changed files with 30 additions and 12 deletions

View File

@ -29,10 +29,18 @@ import Web.Spock
import Network.Wai.Middleware.Static
data Item
= Library {
_name :: Text }
| Something {
_name :: Text }
makeLenses ''Item
data Category = Category {
_categoryId :: Int,
_categoryTitle :: Text,
_categoryItems :: [Text] }
_categoryItems :: [Item] }
makeLenses ''Category
@ -65,12 +73,15 @@ main = runSpock 8080 $ spockT id $ do
(categories %~ (++ [newCategory])) .
(nextId %~ succ)
lucid $ renderCategory newCategory
post ("/add/item" <//> var) $ \catId -> do
item <- param' "item"
post ("/add/item/library" <//> var) $ \catId -> do
libName <- param' "name"
let newItem = Library {
_name = libName }
-- TODO: maybe do something if the category doesn't exist (e.g. has been
-- already deleted)
liftIO $ modifyIORef stateVar $
categoryById catId . categoryItems %~ (++ [item])
categoryById catId . categoryItems %~ (++ [newItem])
lucid $ renderItem newItem
renderRoot :: S -> Html ()
renderRoot s = do
@ -86,15 +97,22 @@ renderRoot s = do
renderCategory :: Category -> Html ()
renderCategory Category{..} =
div_ [id_ (format "cat{}" [_categoryId])] $ do
-- TODO: make category headings links
h2_ (toHtml _categoryTitle)
ul_ $ do
mapM_ (li_ . toHtml) _categoryItems
ul_ $ mapM_ (li_ . renderItem) _categoryItems
-- TODO: probably move handlers to the Javascript part
let handler = format "if (event.keyCode == 13) {\
\ addItem({}, this.value);\
\ addLibrary({}, this.value);\
\ this.value = ''; }"
[_categoryId]
input_ [type_ "text", placeholder_ "new item", onkeyup_ handler]
renderItem :: Item -> Html ()
renderItem Library{..} = a_ [href_ link] (toHtml _name)
where
link = format "https://hackage.haskell.org/package/{}" [_name]
renderItem Something{..} = toHtml _name
-- Utils
loadJS :: Text -> Html ()

View File

@ -1,11 +1,11 @@
// add a new item to a category
function addItem(catId, s) {
$.post("/add/item/" + catId, {item: s})
.done(function() {
// add a new library
function addLibrary(catId, s) {
$.post("/add/item/library/" + catId, {name: s})
.done(function(data) {
var list = $("#cat"+catId+">ul");
var newItem = document.createElement("li");
newItem.appendChild(document.createTextNode(s));
$(newItem).append(data);
list.append(newItem);
});
}