Skip to content

Commit

Permalink
Changed UI and API of how marking a text as done/published work.
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnyLlama committed Apr 22, 2024
1 parent 2b66265 commit c3ab793
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 42 deletions.
6 changes: 2 additions & 4 deletions sql/articles/update.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ UPDATE articles SET
text_body = $4,
text_type = $5,
updated_at = NOW(),
tags = $6,
is_published = $7,
marked_as_done = $8
tags = $6
FROM creators
WHERE
articles.author = creators.username
AND
id = $9
id = $7
RETURNING
id,
title,
Expand Down
3 changes: 0 additions & 3 deletions src/api/text/forms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ pub struct SaveOrEditText<'a> {
#[field(name = "text-body")]
pub text_body: &'a str,
pub tags: &'a str,
pub publish: Option<bool>,
#[field(name = "marked-as-done")]
pub marked_as_done: bool,
}

#[derive(Debug, FromForm)]
Expand Down
19 changes: 7 additions & 12 deletions src/api/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,13 @@ pub async fn text_save(
.collect::<Vec<String>>(),
};

// Only admins are allowed to publish on save.
let should_publish_instantly = form.publish.unwrap_or(false) && claims.admin;

let text = Text::create(
form.title,
&claims.data.username,
form.leading_paragraph,
form.text_body,
form.text_type,
tags,
should_publish_instantly,
form.marked_as_done,
);

match text.save_to_db(db).await {
Expand Down Expand Up @@ -79,11 +74,13 @@ pub async fn text_edit(

let current_text = Text::get_by_id(db, text_id, false).await?;

// Only admins are allowed to edit publish status.
let should_publish = match claims.admin {
true => form.publish.unwrap_or(false),
false => current_text.is_published,
};
if current_text.author != claims.sub && !claims.admin {
return Err(Error::create(
&format!("{}:{}", file!(), line!()),
"Must be owner of text or publisher to edit!",
Status::Unauthorized,
));
}

let updated_text = Text::update_by_id(
db,
Expand All @@ -93,8 +90,6 @@ pub async fn text_edit(
form.text_body,
form.text_type,
&tags,
should_publish,
form.marked_as_done,
)
.await?;

Expand Down
2 changes: 1 addition & 1 deletion src/app/control_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async fn editor_text_id(

Ok(Template::render(
"control_panel/editor_v2",
context! { text, is_publisher: claims.data.is_publisher() },
context! { text, is_publisher: claims.data.is_publisher(), is_editing: true, creator: claims.data },
))
}

Expand Down
10 changes: 0 additions & 10 deletions src/database/models/article.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ impl Text {
text_body: &str,
text_type: TextType,
tags: Vec<String>,
is_published: bool,
marked_as_done: bool,
) -> Self {
Self {
title: title.into(),
Expand All @@ -95,8 +93,6 @@ impl Text {
text_body: text_body.into(),
text_type,
tags,
is_published,
marked_as_done,
..Default::default()
}
}
Expand Down Expand Up @@ -131,8 +127,6 @@ impl Text {
text_body: &str,
text_type: TextType,
tags: &Vec<String>,
is_published: bool,
marked_as_done: bool,
) -> Result<Text, Error> {
sqlx::query_file_as!(
Self,
Expand All @@ -143,8 +137,6 @@ impl Text {
text_body,
text_type as TextType,
tags,
is_published,
marked_as_done,
id,
)
.fetch_one(&db.pool)
Expand Down Expand Up @@ -338,8 +330,6 @@ mod tests {
"Text body",
TextType::Article,
vec![],
true, // is_published
true, // marked as done
);

text.save_to_db(&db).await.expect("SAVING ARTICLE FAILED");
Expand Down
44 changes: 32 additions & 12 deletions templates/control_panel/editor_v2.html.tera
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@

{% block main %}
<h1>Textredigerare</h1>

{% if is_editing %}
<div class="infobox">
<h2 icon="handyman">Verktyg</h2>
<div class="flex wrap gap">
{% if text.author == creator.username and not text.marked_as_done %}
<form class="hidden-form" id="mark-text-done-form" action="/api/text/set-done-status/true" method="post">
<input type="hidden" name="text-id" value="{{ text.id }}">
</form>
<button form="mark-text-done-form" type="submit" class="btn" icon="done">Markera som klar</button>
{% elif text.author == creator.username and text.marked_as_done %}
<form class="hidden-form" id="mark-text-not-done-form" action="/api/text/set-done-status/false" method="post">
<input type="hidden" name="text-id" value="{{ text.id }}">
</form>
<button form="mark-text-not-done-form" class="btn" icon="close">Markera som inte klar</button>
{% endif %}

{% if is_publisher and text.is_published %}
<form class="hidden-form" id="unpublish-text-form" action="/api/text/set-publish-status/false" method="post">
<input type="hidden" name="text-id" value="{{ text.id }}">
</form>
<button form="unpublish-text-form" class="btn dangerous" icon="unpublished">Avpublicera</button>
{% elif is_publisher and not text.is_published %}
<form class="hidden-form" id="publish-text-form" action="/api/text/set-publish-status/true" method="post">
<input type="hidden" name="text-id" value="{{ text.id }}">
</form>
<button form="publish-text-form" class="btn" icon="publish">Publicera</button>
{% endif %}
</div>
</div>
{% endif %}

<form action="{% if text %}/api/text/edit{% else %}/api/text/save{% endif %}" method="post">
<label for="text-type">Texttyp:</label>
<select name="text-type" id="text-type">
Expand Down Expand Up @@ -35,18 +67,6 @@
<label for="tags">Taggar, separera med semikolon (;):</label>
<input type="text" name="tags" value="{% if text.tags %}{{ text.tags | join(sep=";") }}{% endif %}" id="tags">

{% if is_publisher %}
<div>
<input id="publish" name="publish" type="checkbox" {% if text.is_published %}checked{% endif %}>
<label for="publish">Publicera artikel.</label>
</div>
{% endif %}

<div>
<input id="marked_as_done" name="marked_as_done" type="checkbox" {% if text.marked_as_done %}checked{% endif %}>
<label for="marked_as_done">Markera artikeln som klar.</label>
</div>

<div class="flex wrap gap">
<button class="btn" icon="save" type="submit">Spara</button>
</div>
Expand Down

0 comments on commit c3ab793

Please sign in to comment.