diff --git a/opentech/apply/activity/templates/activity/include/listing_base.html b/opentech/apply/activity/templates/activity/include/listing_base.html index 670772603a881ff879e7c5f2b7e08c6d6e971d77..b4fc73b953317d170ca442f8ef6870ec10a0afad 100644 --- a/opentech/apply/activity/templates/activity/include/listing_base.html +++ b/opentech/apply/activity/templates/activity/include/listing_base.html @@ -36,7 +36,7 @@ {% endif %} {% if editable %} - <div class="feed__comment js-comment" data-id="{{activity.id}}" data-comment="{{activity.message|to_markdown}}"> + <div class="feed__comment js-comment" data-id="{{activity.id}}" data-comment="{{activity.message|to_markdown}}" data-edit-url="{% url 'funds:api:comments:edit' pk=activity.pk %}"> {{ activity|display_for:request.user|submission_links|markdown|bleach }} </div> diff --git a/opentech/apply/funds/serializers.py b/opentech/apply/funds/serializers.py index d76d8ad1c849108aac8f101830c3616a73186dc3..bf933e22d34771eb49056adeef5fcd7d3889ffd2 100644 --- a/opentech/apply/funds/serializers.py +++ b/opentech/apply/funds/serializers.py @@ -217,10 +217,11 @@ class RoundLabSerializer(serializers.ModelSerializer): class CommentSerializer(serializers.ModelSerializer): user = serializers.StringRelatedField() message = serializers.SerializerMethodField() + edit_url = serializers.HyperlinkedIdentityField(view_name='funds:api:comments:edit') class Meta: model = Activity - fields = ('id', 'timestamp', 'user', 'submission', 'message', 'visibility', 'edited') + fields = ('id', 'timestamp', 'user', 'submission', 'message', 'visibility', 'edited', 'edit_url') def get_message(self, obj): return bleach_value(markdown(obj.message)) @@ -228,15 +229,14 @@ class CommentSerializer(serializers.ModelSerializer): class CommentCreateSerializer(serializers.ModelSerializer): user = serializers.StringRelatedField() + edit_url = serializers.HyperlinkedIdentityField(view_name='funds:api:comments:edit') class Meta: model = Activity - fields = ('id', 'timestamp', 'user', 'message', 'visibility', 'edited') + fields = ('id', 'timestamp', 'user', 'message', 'visibility', 'edited', 'edit_url') read_only_fields = ('timestamp', 'edited',) class CommentEditSerializer(CommentCreateSerializer): - user = serializers.StringRelatedField() - class Meta(CommentCreateSerializer.Meta): read_only_fields = ('timestamp', 'visibility', 'edited',) diff --git a/opentech/static_src/src/javascript/apply/edit-comment.js b/opentech/static_src/src/javascript/apply/edit-comment.js index 39f5043ae4cde96b9b7873efbd245894245c3943..a99927a4d6618470bdd740a5d8ff58c4f2d6d4d6 100644 --- a/opentech/static_src/src/javascript/apply/edit-comment.js +++ b/opentech/static_src/src/javascript/apply/edit-comment.js @@ -59,14 +59,11 @@ // handle submit $(document).on('click', submitEditButton, function () { const commentContainer = $(this).closest(editBlock).siblings(comment); - const id = $(commentContainer).attr('data-id'); const editedComment = $(this).closest(pageDown).find('.wmd-preview').html(); const commentMD = $(this).closest(editBlock).find('textarea').val(); + const editUrl = $(commentContainer).attr('data-edit-url'); - // TODO - get correct URL - const url = `${window.location.origin}/apply/api/comments/${id}/edit/`; - - fetch(url, { + fetch(editUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -83,7 +80,7 @@ } return response.json(); }).then(data => { - updateComment(commentContainer, data.id, data.message, commentMD); + updateComment(commentContainer, data.id, data.message, data.edit_url, commentMD); updateLastEdited(this, data.edited); showComment(this); showEditButton(this); @@ -116,10 +113,11 @@ $(el).closest(feedContent).find(lastEdited).html(`${parsedDate} ${time}`); }; - const updateComment = (el, id, newComment, newCommentMD) => { + const updateComment = (el, id, newComment, editUrl, newCommentMD) => { $(el).attr('data-id', id); $(el).html(newComment); $(el).attr('data-comment', newCommentMD); + $(el).attr('data-edit-url', editUrl); }; const closeAllEditors = () => {