From 3cd744c0b04ece54fe5e6687a0b09e84d7810cee Mon Sep 17 00:00:00 2001 From: Zohir Tamda Date: Tue, 20 Jan 2026 00:25:32 +0100 Subject: [PATCH 1/3] Avoid redudant code in the controller. --- src/controllers/page_controller.cr | 78 +++++++++++++++--------------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/src/controllers/page_controller.cr b/src/controllers/page_controller.cr index 2fd26e1..0042661 100644 --- a/src/controllers/page_controller.cr +++ b/src/controllers/page_controller.cr @@ -7,41 +7,37 @@ class Kanjo::Controllers::PageController < ATH::Controller @[ARTA::Get("/")] # Renders the dashboard page. def dashboard : ATH::Response - journals = @journal_repository.find_all.map do |journal| - { - "date" => journal.date.to_s("%Y-%m-%d"), - "content" => journal.content, - "readonly" => journal.readonly?, - "emotion" => journal.emotion.to_s, - } - end - Kanjo.render("pages/dashboard.html.j2", { - :journals => journals, + :journals => get_all_journals, }) end @[ARTA::Get("/journals/{date}")] # Renders the journal show page for a specific date. def show_journal(date : String) : ATH::Response + journal = journal_to_hash(@journal_repository.find(date)) + journals = get_all_journals + + Kanjo.render("pages/journal-show.html.j2", { + :journal => journal, + :journals => journals, + }) + end + + @[ARTA::Get("/journals/{date}/edit")] + # Renders the journal edit page for a specific date. + def edit_journal(date : String) : ATH::Response journal_obj = @journal_repository.find(date) - journal = { - "date" => journal_obj.date.to_s("%Y-%m-%d"), - "content" => journal_obj.content, - "readonly" => journal_obj.readonly?, - "emotion" => journal_obj.emotion.to_s, - } - journals = @journal_repository.find_all.map do |j| - { - "date" => j.date.to_s("%Y-%m-%d"), - "content" => j.content, - "readonly" => j.readonly?, - "emotion" => j.emotion.to_s, - } + # Redirect to show page if journal is readonly + if journal_obj.readonly? + return ATH::RedirectResponse.new("/journals/#{date}") end - Kanjo.render("pages/journal-show.html.j2", { + journal = journal_to_hash(journal_obj) + journals = get_all_journals + + Kanjo.render("pages/journal-edit.html.j2", { :journal => journal, :journals => journals, }) @@ -50,14 +46,7 @@ class Kanjo::Controllers::PageController < ATH::Controller @[ARTA::Get("/journals")] # Renders the journals page. def journals : ATH::Response - journals = @journal_repository.find_all.map do |journal| - { - "date" => journal.date.to_s("%Y-%m-%d"), - "content" => journal.content, - "readonly" => journal.readonly?, - "emotion" => journal.emotion.to_s, - } - end + journals = get_all_journals Kanjo.render("pages/journals.html.j2", { :journals => journals, @@ -67,17 +56,26 @@ class Kanjo::Controllers::PageController < ATH::Controller @[ARTA::Get("/about")] # Renders the about page. def about : ATH::Response - journals = @journal_repository.find_all.map do |journal| - { - "date" => journal.date.to_s("%Y-%m-%d"), - "content" => journal.content, - "readonly" => journal.readonly?, - "emotion" => journal.emotion.to_s, - } - end + journals = get_all_journals Kanjo.render("pages/about.html.j2", { :journals => journals, }) end + + # Private methods to avoid code duplication + private def journal_to_hash(journal) : Hash(String, String | Bool) + { + "date" => journal.date.to_s("%Y-%m-%d"), + "content" => journal.content, + "readonly" => journal.readonly?, + "emotion" => journal.emotion.to_s, + } + end + + private def get_all_journals : Array(Hash(String, String | Bool)) + @journal_repository.find_all.map do |journal| + journal_to_hash(journal) + end + end end From 4c2bafff9126c97b545c080128c351d315667712 Mon Sep 17 00:00:00 2001 From: Zohir Tamda Date: Tue, 20 Jan 2026 01:04:27 +0100 Subject: [PATCH 2/3] Refactor the CSS and JS --- public/css/index.css | 33 ++++++++++++++++++++++++++ public/js/app.js | 56 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/public/css/index.css b/public/css/index.css index 3835a8b..f929372 100644 --- a/public/css/index.css +++ b/public/css/index.css @@ -701,6 +701,39 @@ body.kanjo-sky-color { } } +/* Journal Edit Page */ +.kanjo-journal-edit { + max-width: 800px; + margin: 0 auto; +} + +.kanjo-textarea { + font-family: 'Courier New', monospace; + resize: vertical; + min-height: 300px; +} + +.kanjo-form-actions { + display: flex; + gap: 12px; + justify-content: flex-end; + margin-top: 24px; +} + +.kanjo-checkbox-label { + display: flex; + align-items: center; + gap: 8px; + cursor: pointer; + user-select: none; +} + +.kanjo-checkbox-label input[type="checkbox"] { + width: 18px; + height: 18px; + cursor: pointer; +} + @media (max-width: 992px) { .kanjo-double-sidebar { grid-template-columns: 1fr; diff --git a/public/js/app.js b/public/js/app.js index e1db5a7..9a92a47 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -150,3 +150,59 @@ function isValidJournalDate(dateString) { return date <= today; } + +/** + * Initializes the journal edit form. + */ +function initJournalEditForm() { + const form = document.getElementById('journal-edit-form'); + const messageDiv = document.getElementById('message'); + + if (!form) + return; + + const date = form.dataset.journalDate; + + form.addEventListener('submit', async (e) => { + e.preventDefault(); + + const formData = new FormData(form); + + // Convert checkbox value to proper format + if (formData.get('readonly') === 'on') + formData.set('readonly', 'true'); + else + formData.set('readonly', 'false'); + + try { + const response = await fetch(`/api/journals/update/${date}`, { + method: 'PUT', + body: formData + }); + + const result = await response.json(); + + if (response.ok) { + messageDiv.textContent = 'Journal modifié avec succès !'; + messageDiv.className = 'kanjo-message kanjo-message-success'; + messageDiv.style.display = 'block'; + + // Redirect to journal show page after 1.5 seconds + setTimeout(() => { + window.location.href = `/journals/${date}`; + }, 1500); + } else { + throw new Error(result.message || 'Erreur lors de la modification du journal'); + } + } catch (error) { + messageDiv.textContent = error.message; + messageDiv.className = 'kanjo-message kanjo-message-error'; + messageDiv.style.display = 'block'; + } + }); +} + +if (document.readyState === 'loading') + document.addEventListener('DOMContentLoaded', initJournalEditForm); +else + initJournalEditForm(); From a95cb27d745f585fa271130da9ec2b46873142ba Mon Sep 17 00:00:00 2001 From: Zohir Tamda Date: Tue, 20 Jan 2026 01:05:23 +0100 Subject: [PATCH 3/3] Remove conditional macro for the log --- src/server.cr | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/server.cr b/src/server.cr index 057adda..b781322 100644 --- a/src/server.cr +++ b/src/server.cr @@ -56,11 +56,7 @@ formatter = Log::Formatter.new do |entry, io| end end -{% if flag?(:release) %} - Log.setup(:debug, Log::IOBackend.new(formatter: formatter)) -{% else %} - Log.setup(:debug, Log::IOBackend.new(formatter: formatter)) -{% end %} +Log.setup(:debug, Log::IOBackend.new(formatter: formatter)) {% unless flag?(:test) %} # Start the Athena server.