From b07950f7972f67e8763d06a8348c3b6e404b112c Mon Sep 17 00:00:00 2001 From: stuce-bot Date: Sun, 18 May 2025 23:14:03 +0200 Subject: [PATCH] added edit page --- app/FileActions.hs | 7 +-- app/Server.hs | 50 ++++++++++++++--- app/res/b | 12 ++--- shell.nix | 13 ++++- tags | 131 ++++++++++++++++++++++----------------------- todo.cabal | 2 +- todo.db | Bin 12288 -> 0 bytes 7 files changed, 128 insertions(+), 87 deletions(-) delete mode 100644 todo.db diff --git a/app/FileActions.hs b/app/FileActions.hs index 9d50b91..51b1217 100644 --- a/app/FileActions.hs +++ b/app/FileActions.hs @@ -16,14 +16,15 @@ tempFile = "tempFile" add folder file item = do content <- TIO.readFile (folder <> file) let result = content <> item - TIO.writeFile (folder <> tempFile) result + write folder file result +write folder file content = do + TIO.writeFile (folder <> tempFile) content renameFile (folder <> tempFile) (folder <> file) delete folder file item = do content <- TIO.readFile (folder <> file) let result = T.unlines $ filter (not . item) (T.lines content) - TIO.writeFile (folder <> tempFile) result - renameFile (folder <> tempFile) (folder <> file) + write folder file result check folder file item = do content <- TIO.readFile (folder <> file) diff --git a/app/Server.hs b/app/Server.hs index e3decac..19d4fdf 100644 --- a/app/Server.hs +++ b/app/Server.hs @@ -28,6 +28,31 @@ application req respond = TLE.encodeUtf8 $ TL.fromStrict $ simpleSite content + ("GET", ["edit"]) -> do + content <- TIO.readFile "./app/res/b" -- TODO: move that in fileActions + respond $ + responseLBS status200 [("Content-Type", "text/html")] $ + TLE.encodeUtf8 $ + TL.fromStrict $ + editPage content + ("POST", ["edit"]) -> do + -- TODO: refactor this shit, add sanitisation, make sure text ends with \n + putStrLn "overwriting list" + body <- strictRequestBody req + print body + let args = L8.split '&' body + case args of + [newList] -> f newList + _ -> error "should have exactly one arg" + where + f newList = do + let pair = BL.splitAt 5 newList + case pair of + ("list=", t) -> do + let text = TE.decodeUtf8 (urlDecode True (BL.toStrict t)) + FA.write "./app/res/" "b" text + _ -> error "incorrect args" + respond $ responseLBS status303 [(hLocation, "/")] "Redirecting..." ("POST", ["check", name]) -> do FA.check "./app/res/" "b" name respond $ responseLBS status303 [(hLocation, "/")] "Redirecting..." @@ -56,23 +81,24 @@ splitKeyValue bs = case L8.break (== '=') bs of openingTag tag = "<" <> tag <> ">" closingTag tag = " tag <> ">" wrapContent tag content = openingTag tag <> content <> closingTag tag -wrapContentWithArgs tag arg content = openingTag (tag <> arg) <> content <> closingTag tag +wrapContentWithArgs tag arg content = openingTag (tag <> " " <> arg) <> content <> closingTag tag singleWrap content = "<" <> content <> "/>" -headHtml lang title = " lang <> "\">" <> title <> "" -tailHtml = "" +headHtml lang title = " lang <> "\">" <> title <> "" +tailHtml = "" +br = "
" h1 = wrapContent "h1" h2 = wrapContent "h2" h3 = wrapContent "h3" -link = wrapContent "a" +link href = wrapContentWithArgs "a" ("href=\"" <> href <> "\"") li = wrapContent "li" p = wrapContent "p" checkboxPrimitive id name True = singleWrap $ "input type='checkbox' id=" <> id <> " name=" <> name <> " checked" checkboxPrimitive id name False = singleWrap $ "input type='checkbox' id=" <> id <> " name=" <> name -label for = wrapContentWithArgs "label " ("for=" <> for) - +label for = wrapContentWithArgs "label" ("for=" <> for) +textArea id rows cols = wrapContentWithArgs "textarea" ("id=\"" <> id <> "\" name=\"" <> id <> "\" rows=\"" <> rows <> "\" cols=\"" <> cols <> "\"") checkbox id name checked = checkboxPrimitive id name checked <> label id name simpleSite content = @@ -92,7 +118,17 @@ item rawLine = "
rawLine <> "\" method=\"POST\">" -rawEditButton = link "/edit" +editForm content = editLabel <> br <> editTextArea content <> br <> editButton + where + editLabel = label "list" "Edit the todo list" + editTextArea = textArea "list" "30" "33" + editButton = wrapContentWithArgs "button" "type=\"submit\"" "submit" + +editPage content = + headHtml "en" "edit" + <> wrapContentWithArgs "form" "action=\"/edit\" method=\"post\"" (editForm content) + <> tailHtml +rawEditButton = link "/edit" "edit todo list" runServer = do run 3000 application diff --git a/app/res/b b/app/res/b index d96b2c6..2253933 100644 --- a/app/res/b +++ b/app/res/b @@ -1,6 +1,6 @@ -[ ] Wake up -[ ] Eat -[ ] Sleep -[ ] Repeat -[ ] a -[ ] école +[ ] Wake up +[ ] Eat +[ ] Sleep +[x] Repeat +[x] +[ ] dasd diff --git a/shell.nix b/shell.nix index 7f99197..1d81261 100644 --- a/shell.nix +++ b/shell.nix @@ -1,2 +1,11 @@ -let pkgs = import { }; -in pkgs.haskellPackages.developPackage { root = ./.; } +let + pkgs = import {}; + pinnedPkgs = import (builtins.fetchTarball "https://github.com/NixOS/nixpkgs/archive/9b5ac7ad45298d58640540d0323ca217f32a6762.tar.gz") {}; +in + pinnedPkgs.haskellPackages.developPackage { + root = ./.; + modifier = drv: + pinnedPkgs.haskell.lib.addBuildTools drv (with pinnedPkgs.haskellPackages; [ + haskell-language-server + ]); + } diff --git a/tags b/tags index a6eedcd..72011bb 100644 --- a/tags +++ b/tags @@ -1,69 +1,64 @@ !_TAG_FILE_SORTED 1 // -ArgParser /home/stuce/haskell/todo/app/ArgParser.hs 1;" m -Args /home/stuce/haskell/todo/app/ArgParser.hs 5;" t -DBActions /home/stuce/haskell/todo/app/DBActions.hs 4;" m -FileActions /home/stuce/haskell/todo/app/FileActions.hs 5;" m -HtmlPrimitives /home/stuce/haskell/todo/app/HtmlPrimitives.hs 3;" m -Main /home/stuce/haskell/todo/app/Main.hs 3;" m -Server /home/stuce/haskell/todo/app/Server.hs 7;" m -TodoListItem /home/stuce/haskell/todo/app/TodoListItem.hs 1;" m -add /home/stuce/haskell/todo/app/FileActions.hs 16;" f -application /home/stuce/haskell/todo/app/Server.hs 22;" f -argAdd /home/stuce/haskell/todo/app/ArgParser.hs 6;" f -argCheck /home/stuce/haskell/todo/app/ArgParser.hs 8;" f -argDelete /home/stuce/haskell/todo/app/ArgParser.hs 7;" f -argTui /home/stuce/haskell/todo/app/ArgParser.hs 9;" f -args /home/stuce/haskell/todo/app/ArgParser.hs 12;" f -check /home/stuce/haskell/todo/app/FileActions.hs 28;" f -checkbox /home/stuce/haskell/todo/app/HtmlPrimitives.hs 26;" f -checkbox /home/stuce/haskell/todo/app/Server.hs 76;" f -checkboxPrimitive /home/stuce/haskell/todo/app/HtmlPrimitives.hs 21;" f -checkboxPrimitive /home/stuce/haskell/todo/app/Server.hs 71;" f -closingTag /home/stuce/haskell/todo/app/HtmlPrimitives.hs 8;" f -closingTag /home/stuce/haskell/todo/app/Server.hs 57;" f -delete /home/stuce/haskell/todo/app/FileActions.hs 22;" f -docType /home/stuce/haskell/todo/app/HtmlPrimitives.hs 13;" f -done /home/stuce/haskell/todo/app/TodoListItem.hs 5;" f -h1 /home/stuce/haskell/todo/app/HtmlPrimitives.hs 14;" f -h1 /home/stuce/haskell/todo/app/Server.hs 64;" f -h2 /home/stuce/haskell/todo/app/HtmlPrimitives.hs 15;" f -h2 /home/stuce/haskell/todo/app/Server.hs 65;" f -h3 /home/stuce/haskell/todo/app/HtmlPrimitives.hs 16;" f -h3 /home/stuce/haskell/todo/app/Server.hs 66;" f -headHtml /home/stuce/haskell/todo/app/Server.hs 62;" f -item /home/stuce/haskell/todo/app/HtmlPrimitives.hs 29;" f -item /home/stuce/haskell/todo/app/Server.hs 90;" f -label /home/stuce/haskell/todo/app/HtmlPrimitives.hs 24;" f -label /home/stuce/haskell/todo/app/Server.hs 74;" f -li /home/stuce/haskell/todo/app/HtmlPrimitives.hs 18;" f -li /home/stuce/haskell/todo/app/Server.hs 68;" f -link /home/stuce/haskell/todo/app/HtmlPrimitives.hs 17;" f -link /home/stuce/haskell/todo/app/Server.hs 67;" f -main /home/stuce/haskell/todo/app/Main.hs 7;" f -name /home/stuce/haskell/todo/app/TodoListItem.hs 4;" f -newItemForm /home/stuce/haskell/todo/app/Server.hs 93;" f -openingTag /home/stuce/haskell/todo/app/HtmlPrimitives.hs 7;" f -openingTag /home/stuce/haskell/todo/app/Server.hs 56;" f -p /home/stuce/haskell/todo/app/HtmlPrimitives.hs 19;" f -p /home/stuce/haskell/todo/app/Server.hs 69;" f -path /home/stuce/haskell/todo/app/HtmlPrimitives.hs 31;" f -rawEditButton /home/stuce/haskell/todo/app/Server.hs 95;" f -runAdd /home/stuce/haskell/todo/app/DBActions.hs 9;" f -runCheck /home/stuce/haskell/todo/app/DBActions.hs 20;" f -runDelete /home/stuce/haskell/todo/app/DBActions.hs 15;" f -runServer /home/stuce/haskell/todo/app/Server.hs 97;" f -runShow /home/stuce/haskell/todo/app/DBActions.hs 24;" f -simpleSite /home/stuce/haskell/todo/app/HtmlPrimitives.hs 33;" f -simpleSite /home/stuce/haskell/todo/app/Server.hs 78;" f -singleWrap /home/stuce/haskell/todo/app/HtmlPrimitives.hs 11;" f -singleWrap /home/stuce/haskell/todo/app/Server.hs 60;" f -splitKeyValue /home/stuce/haskell/todo/app/Server.hs 53;" f -swapCheck /home/stuce/haskell/todo/app/FileActions.hs 36;" f -swapIfMatch /home/stuce/haskell/todo/app/FileActions.hs 34;" f -tailHtml /home/stuce/haskell/todo/app/Server.hs 63;" f -tempFile /home/stuce/haskell/todo/app/FileActions.hs 12;" f -ul /home/stuce/haskell/todo/app/HtmlPrimitives.hs 38;" f -wrapContent /home/stuce/haskell/todo/app/HtmlPrimitives.hs 9;" f -wrapContent /home/stuce/haskell/todo/app/Server.hs 58;" f -wrapContentWithArgs /home/stuce/haskell/todo/app/HtmlPrimitives.hs 10;" f -wrapContentWithArgs /home/stuce/haskell/todo/app/Server.hs 59;" f +ArgParser /home/stuce/dev/haskell/todo/app/ArgParser.hs 1;" m +Args /home/stuce/dev/haskell/todo/app/ArgParser.hs 5;" t +FileActions /home/stuce/dev/haskell/todo/app/FileActions.hs 5;" m +HtmlPrimitives /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 3;" m +Main /home/stuce/dev/haskell/todo/app/Main.hs 3;" m +Server /home/stuce/dev/haskell/todo/app/Server.hs 7;" m +TodoListItem /home/stuce/dev/haskell/todo/app/TodoListItem.hs 1;" m +add /home/stuce/dev/haskell/todo/app/FileActions.hs 16;" f +application /home/stuce/dev/haskell/todo/app/Server.hs 22;" f +argAdd /home/stuce/dev/haskell/todo/app/ArgParser.hs 6;" f +argCheck /home/stuce/dev/haskell/todo/app/ArgParser.hs 8;" f +argDelete /home/stuce/dev/haskell/todo/app/ArgParser.hs 7;" f +argTui /home/stuce/dev/haskell/todo/app/ArgParser.hs 9;" f +args /home/stuce/dev/haskell/todo/app/ArgParser.hs 12;" f +check /home/stuce/dev/haskell/todo/app/FileActions.hs 28;" f +checkbox /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 26;" f +checkbox /home/stuce/dev/haskell/todo/app/Server.hs 76;" f +checkboxPrimitive /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 21;" f +checkboxPrimitive /home/stuce/dev/haskell/todo/app/Server.hs 71;" f +closingTag /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 8;" f +closingTag /home/stuce/dev/haskell/todo/app/Server.hs 57;" f +delete /home/stuce/dev/haskell/todo/app/FileActions.hs 22;" f +docType /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 13;" f +done /home/stuce/dev/haskell/todo/app/TodoListItem.hs 5;" f +h1 /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 14;" f +h1 /home/stuce/dev/haskell/todo/app/Server.hs 64;" f +h2 /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 15;" f +h2 /home/stuce/dev/haskell/todo/app/Server.hs 65;" f +h3 /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 16;" f +h3 /home/stuce/dev/haskell/todo/app/Server.hs 66;" f +headHtml /home/stuce/dev/haskell/todo/app/Server.hs 62;" f +item /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 29;" f +item /home/stuce/dev/haskell/todo/app/Server.hs 90;" f +label /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 24;" f +label /home/stuce/dev/haskell/todo/app/Server.hs 74;" f +li /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 18;" f +li /home/stuce/dev/haskell/todo/app/Server.hs 68;" f +link /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 17;" f +link /home/stuce/dev/haskell/todo/app/Server.hs 67;" f +main /home/stuce/dev/haskell/todo/app/Main.hs 7;" f +name /home/stuce/dev/haskell/todo/app/TodoListItem.hs 4;" f +newItemForm /home/stuce/dev/haskell/todo/app/Server.hs 93;" f +openingTag /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 7;" f +openingTag /home/stuce/dev/haskell/todo/app/Server.hs 56;" f +p /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 19;" f +p /home/stuce/dev/haskell/todo/app/Server.hs 69;" f +path /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 31;" f +rawEditButton /home/stuce/dev/haskell/todo/app/Server.hs 95;" f +runServer /home/stuce/dev/haskell/todo/app/Server.hs 97;" f +simpleSite /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 33;" f +simpleSite /home/stuce/dev/haskell/todo/app/Server.hs 78;" f +singleWrap /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 11;" f +singleWrap /home/stuce/dev/haskell/todo/app/Server.hs 60;" f +splitKeyValue /home/stuce/dev/haskell/todo/app/Server.hs 53;" f +swapCheck /home/stuce/dev/haskell/todo/app/FileActions.hs 36;" f +swapIfMatch /home/stuce/dev/haskell/todo/app/FileActions.hs 34;" f +tailHtml /home/stuce/dev/haskell/todo/app/Server.hs 63;" f +tempFile /home/stuce/dev/haskell/todo/app/FileActions.hs 12;" f +ul /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 38;" f +wrapContent /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 9;" f +wrapContent /home/stuce/dev/haskell/todo/app/Server.hs 58;" f +wrapContentWithArgs /home/stuce/dev/haskell/todo/app/HtmlPrimitives.hs 10;" f +wrapContentWithArgs /home/stuce/dev/haskell/todo/app/Server.hs 59;" f diff --git a/todo.cabal b/todo.cabal index ee34f73..1feaefd 100644 --- a/todo.cabal +++ b/todo.cabal @@ -63,7 +63,7 @@ executable todo main-is: Main.hs -- Modules included in this executable, other than Main. - -- other-modules: + other-modules: TodoListItem -- LANGUAGE extensions used by modules in this package. -- other-extensions: diff --git a/todo.db b/todo.db deleted file mode 100644 index d9a010684054f3768ea636c9d1a05b2e7e9b9afe..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmWFz^vNtqRY=P(%1ta$FlG>7U}R))P*7lCU{GgZU|?oI07eD|1{MUD0mMh*Vr8l` z=#_QwGB7YO@vAWKm+`A$RW&L&8UmvsFd71*Aut*OqaiRF0;3@?8UmvsFd71*Aut*O zgCGO~nApU%wHYHz5|eULiwkoyOH$*DQwvK|^ODiI%+5isjv=lJA&yQyt_o;sHS!X3 zQ+0|{3pE?r8QI0<h{X$&bU4s+?f;@d4 zgCZ5YT_Y77Lqq&M{hWhbeO>)RbQC}qDTKI2geds=hbZ`k`uON5q~zzNDmeN3`?xy# z!G$$JeRF312MqjQ`Csxs;J-2mf_u~nqaiRF0;3@?8UmvsFd71*Aut*OqaiRF0;3@? z8UmvsFd70xga8*m3xgy_Zem_~Y7sjh3xhC6QdSWcFGw)GG{q&cl9PvpL7X!qH76$@ z)c