-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aef8bdc
commit 05751fe
Showing
5 changed files
with
161 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"use strict"; | ||
|
||
(function () { | ||
// This JS code won't be executed on IE11, so we can write ES6 code | ||
class MessageForm extends Stimulus.Controller { | ||
initialize() { | ||
this.onGoingRequestValue = false; | ||
} | ||
|
||
async onSubmit(evt) { | ||
if (this.onGoingRequestValue) { | ||
return; | ||
} | ||
|
||
evt.preventDefault(); | ||
evt.stopPropagation(); | ||
|
||
if (!this.formTarget.checkValidity()) { | ||
return; | ||
} | ||
|
||
let dest = new URL(this.formTarget.action, location.origin); | ||
dest.searchParams.append("http-api", "true"); | ||
let formData = new FormData(this.formTarget); | ||
|
||
this.onGoingRequestValue = true; | ||
|
||
let response = await fetch(dest.toString(), { | ||
method: this.formTarget.method.toUpperCase(), | ||
body: formData, | ||
}).finally(() => { | ||
this.onGoingRequestValue = false; | ||
}); | ||
|
||
if (response.ok) { | ||
let html = await response.text(); | ||
if (this.hasEmptyElementTarget) { | ||
this.emptyElementTarget.remove(); | ||
} | ||
|
||
this.messagesListTarget.insertAdjacentHTML("beforeend", html); | ||
this.formTarget.reset(); | ||
} | ||
} | ||
|
||
static targets = ["form", "emptyElement", "messagesList"] | ||
static values = {"onGoingRequest": Boolean} | ||
} | ||
|
||
if (window.fetch) { | ||
window.addEventListener("load", () => | ||
Stimulus.Application.start().register("message-form", MessageForm) | ||
); | ||
} | ||
})(); |
23 changes: 23 additions & 0 deletions
23
aidants_connect_habilitation/templates/request_messages/_message_item.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{% comment %} | ||
Context: | ||
{ | ||
"message": RequestMessage, | ||
"issuer": Issuer, | ||
} | ||
{% endcomment %} | ||
<li> | ||
<div class="fr-grid-row fr-grid-row--gutters"> | ||
{% if message.sender == "AC" %} | ||
<div class="fr-col-4 text-blue"> | ||
<strong>"Aidant Connect</strong> | ||
<p class="more-info-messages">{{ message.created_at }}</p> | ||
</div> | ||
{% else %} | ||
<div class="fr-col-4 text-red"> | ||
<strong>{{ issuer.first_name }} {{ issuer.last_name }}</strong> | ||
<p class="more-info-messages">{{ message.created_at }}</p> | ||
</div> | ||
{% endif %} | ||
<div class="fr-col-8">{{ message.content|linebreaksbr }}</div> | ||
</div> | ||
</li> |
59 changes: 59 additions & 0 deletions
59
aidants_connect_habilitation/templates/request_messages/_messages.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{% comment %} | ||
Context: | ||
{ | ||
"messages": List[RequestMessage], | ||
"issuer": Issuer, | ||
"form": RequestMessageForm, | ||
} | ||
{% endcomment %} | ||
<h2>Vos échanges avec Aidants Connect</h2> | ||
<div | ||
class="fr-grid-row fr-grid-row--gutters" | ||
data-controller="message-form" | ||
> | ||
<div class="fr-col-12 fr-col-md-8"> | ||
<div class="shadowed"> | ||
<div class="messages-list-container"> | ||
<ol class="messages-list" data-message-form-target="messagesList"> | ||
{% for message in messages.all %} | ||
{% include "request_messages/_message_item.html" with issuer=issuer message=message %} | ||
{% empty %} | ||
<li data-message-form-target="emptyElement"> | ||
<div class="fr-grid-row fr-grid-row--gutters"> | ||
<div class="fr-col-4 text-blue"><strong>Aidants Connect</strong></div> | ||
<div class="fr-col-8"> | ||
<p class="more-info-messages"><strong>Notre conversation démarre ici.</strong></p> | ||
<p class="more-info-messages"> | ||
Vous retrouverez ici vos échanges par écrit avec l’équipe d’Aidants Connect. Vous pouvez utiliser | ||
l’interface ci-dessous pour envoyer un message, | ||
une remarque ou une demande complémentaire.</p> | ||
<p class="more-info-messages">Nous vous répondrons dans les meilleurs délais.</p> | ||
</div> | ||
</div> | ||
</li> | ||
{% endfor %} | ||
</ol> | ||
</div> | ||
<div class="message-form-container"> | ||
<form method="post" data-message-form-target="form"> | ||
{% csrf_token %} | ||
<div class="fr-grid-row fr-grid-row--gutters"> | ||
<label class="fr-col-4" for="{{ form.content.id_for_label }}">Votre message</label> | ||
<div class="fr-col-6"> | ||
{{ form.content }} | ||
</div> | ||
<div class="fr-col-2"> | ||
<button | ||
type="submit" | ||
class="button primary" | ||
data-action="message-form#onSubmit" | ||
> | ||
Envoyer | ||
</button> | ||
</div> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters