Implement remark preview

This commit is contained in:
swaggboi 2023-08-13 19:39:36 -05:00
parent b6962c540c
commit 91588bd816
4 changed files with 78 additions and 19 deletions

View File

@ -64,6 +64,12 @@
padding: 0.5em 1em; padding: 0.5em 1em;
} }
.form-preview {
background-color: var(--dark-warm-gray);
border: inset var(--dark-warm-gray) 0.18rem;
padding: 0.25em 0.5em;
}
.form-body { .form-body {
background-color: var(--light-warm-gray); background-color: var(--light-warm-gray);
border: outset var(--light-warm-gray) 0.18rem; border: outset var(--light-warm-gray) 0.18rem;

View File

@ -17,7 +17,7 @@ sub by_id($self) {
sub create($self) { sub create($self) {
my $thread_id = $self->param('thread_id'); my $thread_id = $self->param('thread_id');
my $remark_id = $self->param('remark_id'); my $remark_id = $self->param('remark_id');
my $v; my ($v, $draft);
$v = $self->validation if $self->req->method eq 'POST'; $v = $self->validation if $self->req->method eq 'POST';
@ -25,6 +25,7 @@ sub create($self) {
$v->required('author' )->size(1, 63); $v->required('author' )->size(1, 63);
$v->required('body' )->size(2, 4000); $v->required('body' )->size(2, 4000);
$v->optional('bump' ); $v->optional('bump' );
$v->optional('preview');
if ($v->has_error) { if ($v->has_error) {
$self->stash(status => 400) $self->stash(status => 400)
@ -33,15 +34,17 @@ sub create($self) {
my $remark_author = $v->param('author' ); my $remark_author = $v->param('author' );
my $remark_body = $v->param('body' ); my $remark_body = $v->param('body' );
my $bump_thread = $v->param('bump' ); my $bump_thread = $v->param('bump' );
my $preview = $v->param('preview');
$self->session(author => $remark_author);
unless ($preview) {
$self->remark->create( $self->remark->create(
$thread_id, $thread_id,
$remark_author, $remark_author,
$remark_body $remark_body
); );
$self->session(author => $remark_author);
$self->thread->bump($thread_id) if $bump_thread; $self->thread->bump($thread_id) if $bump_thread;
return $self->redirect_to($self->url_for(single_thread => { return $self->redirect_to($self->url_for(single_thread => {
@ -49,6 +52,9 @@ sub create($self) {
thread_page => $self->remark->last_page_for($thread_id) thread_page => $self->remark->last_page_for($thread_id)
})->fragment('remarks')); })->fragment('remarks'));
} }
$draft = $remark_body;
}
} }
my $thread = $self->thread->by_id($thread_id); my $thread = $self->thread->by_id($thread_id);
@ -58,7 +64,8 @@ sub create($self) {
$self->stash( $self->stash(
thread => $thread, thread => $thread,
last_remark => $last_remark last_remark => $last_remark,
draft => $draft
); );
$self->stash(status => 404, error => 'Thread not found 🤷') $self->stash(status => 404, error => 'Thread not found 🤷')

37
t/preview.t Normal file
View File

@ -0,0 +1,37 @@
use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
my $t = Test::Mojo->new('PostText');
#my %preview_thread = (
# author => 'Anonymous',
# title => 'hi',
# body => 'ayy... lmao',
# preview => 1
# );
my %preview_remark = (
author => 'Anonymous',
body => 'ayy... lmao',
preview => 1
);
subtest 'Check the form + button', sub {
$t->get_ok('/remark/post/1')->status_is(200)
->element_exists('input[id="preview"]');
#$t->get_ok('/thread/post')->status_is(200)
# ->element_exists('input[id="preview"]');
};
subtest 'Submit input', sub {
$t->post_ok('/remark/post/1', form => \%preview_remark)
->status_is(200)
->text_like(p => qr/ayy\.\.\. lmao/);
#$t->post_ok('/thread/post', form => \%preview_thread)
# ->status_is(200)
# ->text_like(p => qr/ayy\.\.\. lmao/);
};
done_testing;

View File

@ -14,6 +14,11 @@
content="Remark on thread #<%= $thread->{'id'} %>."> content="Remark on thread #<%= $thread->{'id'} %>.">
<% end %> <% end %>
<form method="post" class="form-body"> <form method="post" class="form-body">
<% if ($draft) { =%>
<div class="form-preview">
<%== markdown $draft =%>
</div>
<% } =%>
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('author')) { =%> <% if (my $error = validation->error('author')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %> <p class="field-with-error">Must be between <%= $error->[2] %>
@ -46,6 +51,10 @@
<%= check_box bump => 1, id => 'bump', checked => undef %> <%= check_box bump => 1, id => 'bump', checked => undef %>
<%= label_for bump => 'Bump' %> <%= label_for bump => 'Bump' %>
</div> </div>
<div class="form-checkbox">
<%= check_box preview => 1, id => 'preview' %>
<%= label_for preview => 'Preview' %>
</div>
<button type="submit" class="form-button">Post</button> <button type="submit" class="form-button">Post</button>
</form> </form>
<%# Putting this first above the thread body (nested if, yucky sry) %> <%# Putting this first above the thread body (nested if, yucky sry) %>