Implement remark preview

This commit is contained in:
swagg boi 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,31 +17,34 @@ 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';
if ($v && $v->has_data) { if ($v && $v->has_data) {
$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)
} }
else { else {
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) %>