Unified message display, scroll to newest after sending or deleting

This commit is contained in:
Igor Scheller 2022-06-06 16:07:03 +02:00
parent 9e5f0ff854
commit d9e4bf3cac
3 changed files with 19 additions and 29 deletions

View File

@ -16,38 +16,28 @@
<div class="col-12 col-lg-8 offset-lg-2"> <div class="col-12 col-lg-8 offset-lg-2">
<div class="row conversation"> <div class="row conversation">
{% for msg in messages %} {% for msg in messages %}
{% if msg.user_id == user.id %} {% set own_message = msg.user_id == user.id %}
<div class="col-12"{% if loop.last %} id="newest"{% endif %}> <div class="col-12"{% if loop.last %} id="newest"{% endif %}>
<div class="d-flex justify-content-end"> <div class="d-flex{% if own_message %} justify-content-end{% endif %}">
<div class="message alert alert-primary"> <div class="message alert {% if own_message %}alert-primary{% else %}alert-secondary{% endif %}">
<p>{{ msg.text | nl2br }}</p> <p>{{ msg.text | nl2br }}</p>
<div class="text-end"> <div class="text-end">
<small class="opacity-75">{{ msg.created_at.format(__('Y-m-d H:i')) }}</small>
{% if own_message %}
<form action="{{ url('/messages/' ~ other_user.id ~ '/' ~ msg.id) }}" <form action="{{ url('/messages/' ~ other_user.id ~ '/' ~ msg.id) }}"
enctype="multipart/form-data" method="post" enctype="multipart/form-data" method="post" class="float-end"
> >
{{ csrf() }} {{ csrf() }}
<small class="opacity-75">{{ msg.created_at.format(__('Y-m-d H:i')) }}</small>
{{ f.submit(m.icon('trash'), {'btn_type': 'primary', 'size': 'sm'}) }} {{ f.submit(m.icon('trash'), {'btn_type': 'primary', 'size': 'sm'}) }}
</form> </form>
{% endif %}
</div> </div>
</div> {% if not own_message and msg.read == false %}
</div>
</div>
{% else %}
<div class="col-12"{% if loop.last %} id="newest"{% endif %}>
<div>
<div class="message alert alert-secondary position-relative">
<div>{{ msg.text | nl2br }}</div>
<div class="text-end">
<small class="opacity-75">{{ msg.created_at.format(__('Y-m-d H:i')) }}</small>
</div>
{% if msg.read == false %}
<span class="position-absolute top-0 start-100 translate-middle-x p-2 bg-danger rounded-circle"></span> <span class="position-absolute top-0 start-100 translate-middle-x p-2 bg-danger rounded-circle"></span>
{% endif %} {% endif %}
</div> </div>
</div> </div>
</div> </div>
{% endif %}
{% endfor %} {% endfor %}
</div> </div>
@ -56,7 +46,7 @@
<div class="input-group"> <div class="input-group">
<label for="message" class="visually-hidden">{{ __('message.message') }}</label> <label for="message" class="visually-hidden">{{ __('message.message') }}</label>
<textarea class="form-control" id="message" name="text" required="" rows="2"></textarea> <textarea class="form-control" id="message" name="text" required="" rows="3"></textarea>
{{ f.submit(m.icon('send-fill')) }} {{ f.submit(m.icon('send-fill')) }}
</div> </div>
</form> </form>

View File

@ -179,7 +179,7 @@ class MessagesController extends BaseController
$newMessage->read = $otherUser->id == $currentUser->id; // if its to myself, I obviously read it. $newMessage->read = $otherUser->id == $currentUser->id; // if its to myself, I obviously read it.
$newMessage->save(); $newMessage->save();
return $this->redirect->to('/messages/' . $otherUser->id); return $this->redirect->to('/messages/' . $otherUser->id . '#newest');
} }
/** /**
@ -199,7 +199,7 @@ class MessagesController extends BaseController
throw new HttpForbidden('You can not delete a message you haven\'t send'); throw new HttpForbidden('You can not delete a message you haven\'t send');
} }
return $this->redirect->to('/messages/' . $otherUserId); return $this->redirect->to('/messages/' . $otherUserId . '#newest');
} }
/** /**

View File

@ -443,7 +443,7 @@ class MessagesControllerTest extends ControllerTest
$this->response->expects($this->once()) $this->response->expects($this->once())
->method('redirectTo') ->method('redirectTo')
->with('http://localhost/messages/' . $this->userB->id) ->with('http://localhost/messages/' . $this->userB->id . '#newest')
->willReturn($this->response); ->willReturn($this->response);
$this->controller->send($this->request); $this->controller->send($this->request);
@ -465,7 +465,7 @@ class MessagesControllerTest extends ControllerTest
$this->response->expects($this->once()) $this->response->expects($this->once())
->method('redirectTo') ->method('redirectTo')
->with('http://localhost/messages/' . $this->userA->id) ->with('http://localhost/messages/' . $this->userA->id . '#newest')
->willReturn($this->response); ->willReturn($this->response);
$this->controller->send($this->request); $this->controller->send($this->request);
@ -511,7 +511,7 @@ class MessagesControllerTest extends ControllerTest
$this->response->expects($this->once()) $this->response->expects($this->once())
->method('redirectTo') ->method('redirectTo')
->with('http://localhost/messages/1') ->with('http://localhost/messages/1#newest')
->willReturn($this->response); ->willReturn($this->response);
$this->controller->delete($this->request); $this->controller->delete($this->request);