Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(#150): unify django views responses #151

Merged
merged 1 commit into from
Jan 23, 2021
Merged

Conversation

Radi85
Copy link
Owner

@Radi85 Radi85 commented Jan 17, 2021

resolves #150
fixes #149

  • Replace all responses in django views with UTF8JsonResponse.
  • Handle anonymouse comment form.
  • Refactore get context - remove get_comment_context_data and use DABContext class instead.

comment/mixins.py Outdated Show resolved Hide resolved
comment/mixins.py Outdated Show resolved Hide resolved
comment/mixins.py Outdated Show resolved Hide resolved
@abhiabhi94
Copy link
Collaborator

abhiabhi94 commented Jan 20, 2021

I get an error when trying to post an anonymous parent comment from the UI, Unable to post your comment!, please try again.

Also, you probably forgot to change the response for ConfirmComment view.

@Radi85
Copy link
Owner Author

Radi85 commented Jan 20, 2021

I get an error when trying to post an anonymous parent comment from the UI, Unable to post your comment!, please try again.

I am not sure that I can help with the current info since it's working for me locally.
I will try to test this branch again on a fresh project.

Also, you probably forgot to change the response for ConfirmComment view.

In ConfirmComment We redirect the user to the site after saving the comment so there is no need to use JsonResponse here.

@Radi85
Copy link
Owner Author

Radi85 commented Jan 20, 2021

I will make a new release 2.6b2 after merging this.

@abhiabhi94
Copy link
Collaborator

In ConfirmComment We redirect the user to the site after saving the comment so there is no need to use JsonResponse here.

Yes, you're right but there's also a case, where you render a template when the key passed doesn't resolve into a valid comment.

if not temp_comment.is_valid:
return render(request, template_name='comment/anonymous/discarded.html')

@Radi85
Copy link
Owner Author

Radi85 commented Jan 20, 2021

if not temp_comment.is_valid:
return render(request, template_name='comment/anonymous/discarded.html')

Oh nice observation, good you mentioned that. I will update it.

@abhiabhi94
Copy link
Collaborator

The GIF of the error when trying to post an anonymous comment locally on my machine.

anonymous-comment-error

I got the issue, it is occurring because of the firefox relay icon present in the email field.

I have used comments in the javascript code below to give more of an idea about the issue.

...
/* here formButton actually captures the relay-icon */ 
 let formButton = form.querySelector("button");
...
/* Although it is a parent comment, since it doesn't get the attribute `value`, hence considers it as a child comment */
// parent comment
if (formButton.getAttribute('value') === 'parent') {
    // reload all comments only when posting parent comment
    if (result.anonymous) {
        createInfoElement(form.closest('.js-comment'), 'success', result.msg, 3);
        form.reset();
        return;
    }
    document.getElementById("comments").outerHTML = result.data;
} else {
// child comment
    if (result.anonymous) {
    /*Since it doesn't get the parent element here, it passed null to `createInfoElement` which was causing an error*/
    createInfoElement(form.closest('.js-parent-comment'), 'success', result.msg, 3);
    form.reset();
    return; 
    }
...
}

Although this might not occur with a lot of other people, maybe we can use a more comprehensive check to use some sort of id or class rather than using the element name for the query to find the button.

@Radi85
Copy link
Owner Author

Radi85 commented Jan 23, 2021

if not temp_comment.is_valid:
return render(request, template_name='comment/anonymous/discarded.html')

I don't think this needs to be changed to JsonResponse since this call is not handled in JS. The current response seems to be the right one.

@Radi85
Copy link
Owner Author

Radi85 commented Jan 23, 2021

The GIF of the error when trying to post an anonymous comment locally on my machine.

anonymous-comment-error

I got the issue, it is occurring because of the firefox relay icon present in the email field.

I tested on Firefox and it's working! I didn't get what the issue is yet.

@abhiabhi94
Copy link
Collaborator

I tested on Firefox and it's working! I didn't get what the issue is yet.

Yes, it won't affect unless you use an extension firefox-relay.

This generates email aliases that can be forwarded to your original email and ...

So in my case it is causing an issue because it creates a button icon(to generate email aliases) as seen in the GIF above on the email fields.

@Radi85
Copy link
Owner Author

Radi85 commented Jan 23, 2021

I tested on Firefox and it's working! I didn't get what the issue is yet.

Yes, it won't affect unless you use an extension firefox-relay.

This generates email aliases that can be forwarded to your original email and ...

So in my case it is causing an issue because it creates a button icon(to generate email aliases) as seen in the GIF above on the email fields.

So this is a very special case. However, I am thinking to change the UI for creating anonymous comments to be similar to the one implemented in the subscription feature. That means the email field will be removed from the current UI form and instead will be prompted by a modal form.

resolves #150
fixes #149

- Replace all responses in django views with UTF8JsonResponse.
- Handle anonymouse comment form.
- Refactore get context - remove get_comment_context_data and use DABContext class instead.
@abhiabhi94
Copy link
Collaborator

However, I am thinking to change the UI for creating anonymous comments to be similar to the one implemented in the subscription feature. That means the email field will be removed from the current UI form and instead will be prompted by a modal form.

I think this should ideally be done in a separate issue.

@Radi85 Radi85 merged commit 57455dd into develop Jan 23, 2021
@Radi85 Radi85 deleted the user-json-responses branch February 6, 2021 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unify django views response. JS error when replying to a comment anonymously.
2 participants