Skip to content

Commit

Permalink
Apply suggestion from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehenry committed Apr 12, 2022
1 parent 05751fe commit 2b967bf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
7 changes: 5 additions & 2 deletions aidants_connect_habilitation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
BooleanField,
CharField,
ChoiceField,
ModelForm,
Textarea,
modelformset_factory,
)
Expand Down Expand Up @@ -388,9 +387,13 @@ def save(
save.alters_data = True


class RequestMessageForm(ModelForm):
class RequestMessageForm(PatchedForm):
content = CharField(label="Votre message", widget=Textarea(attrs={"rows": 2}))

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.widget_attrs("content", {"data-message-form-target": "textarea"})

class Meta:
model = models.RequestMessage
fields = ["content"]
19 changes: 14 additions & 5 deletions aidants_connect_habilitation/static/js/message_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
dest.searchParams.append("http-api", "true");
let formData = new FormData(this.formTarget);

this.onGoingRequestValue = true;
this.lockForm(true);

let response = await fetch(dest.toString(), {
method: this.formTarget.method.toUpperCase(),
body: formData,
}).finally(() => {
this.onGoingRequestValue = false;
});
}).finally(() => this.lockForm(false));

if (response.ok) {
let html = await response.text();
Expand All @@ -43,7 +41,18 @@
}
}

static targets = ["form", "emptyElement", "messagesList"]
lockForm(lockState) {
this.onGoingRequestValue = lockState;
if (lockState) {
this.submitBtnTarget.setAttribute("disabled", "disabled");
this.textareaTarget.setAttribute("disabled", "disabled");
} else {
this.submitBtnTarget.removeAttribute("disabled");
this.textareaTarget.removeAttribute("disabled");
}
}

static targets = ["form", "emptyElement", "messagesList", "submitBtn", "textarea"]
static values = {"onGoingRequest": Boolean}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
{% 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>
{% 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>
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ <h2>Vos échanges avec Aidants Connect</h2>
type="submit"
class="button primary"
data-action="message-form#onSubmit"
data-message-form-target="submitBtn"
>
Envoyer
</button>
Expand Down
4 changes: 0 additions & 4 deletions aidants_connect_habilitation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,6 @@ class ReadonlyRequestView(LateStageRequestView, FormView):
template_name = "view_organisation_request.html"
form_class = RequestMessageForm

@property
def step(self) -> HabilitationFormStep:
return HabilitationFormStep.SUMMARY

def get_context_data(self, **kwargs):
return {
**super().get_context_data(**kwargs),
Expand Down

0 comments on commit 2b967bf

Please sign in to comment.