Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions public/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
56 changes: 56 additions & 0 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
78 changes: 38 additions & 40 deletions src/controllers/page_controller.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand All @@ -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,
Expand All @@ -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
6 changes: 1 addition & 5 deletions src/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down