From 12ef77c52eb0f744bdd7d96c31c54a2f8096bb6b Mon Sep 17 00:00:00 2001 From: Deimos Date: Thu, 9 May 2019 23:51:15 -0600 Subject: [PATCH] Make topic bookmark button toggle properly I'll do some more work on this later because this is still pretty ugly, but it'll do the job for now. --- .../post_action_toggle_button.jinja2 | 14 +++++++ tildes/tildes/templates/macros/buttons.jinja2 | 39 +++++++++++++++++++ .../tildes/templates/macros/comments.jinja2 | 30 +++++--------- tildes/tildes/templates/topic.jinja2 | 29 +++++--------- tildes/tildes/views/api/web/topic.py | 23 ++++++++++- 5 files changed, 94 insertions(+), 41 deletions(-) create mode 100644 tildes/tildes/templates/intercooler/post_action_toggle_button.jinja2 create mode 100644 tildes/tildes/templates/macros/buttons.jinja2 diff --git a/tildes/tildes/templates/intercooler/post_action_toggle_button.jinja2 b/tildes/tildes/templates/intercooler/post_action_toggle_button.jinja2 new file mode 100644 index 0000000..050d818 --- /dev/null +++ b/tildes/tildes/templates/intercooler/post_action_toggle_button.jinja2 @@ -0,0 +1,14 @@ +{# Copyright (c) 2019 Tildes contributors #} +{# SPDX-License-Identifier: AGPL-3.0-or-later #} + +{% from 'macros/buttons.jinja2' import post_action_toggle_button with context %} + +{{ post_action_toggle_button( + subject=subject, + is_toggled=is_toggled, + normal_name=normal_name, + normal_label=normal_label, + toggled_name=toggled_name, + toggled_label=toggled_label, + route_name=route_name, +) }} diff --git a/tildes/tildes/templates/macros/buttons.jinja2 b/tildes/tildes/templates/macros/buttons.jinja2 new file mode 100644 index 0000000..75f5b7e --- /dev/null +++ b/tildes/tildes/templates/macros/buttons.jinja2 @@ -0,0 +1,39 @@ +{# Copyright (c) 2019 Tildes contributors #} +{# SPDX-License-Identifier: AGPL-3.0-or-later #} + +{% macro post_action_toggle_button(subject, is_toggled, normal_name, normal_label, toggled_name, toggled_label, route_name) %} + {% if is_toggled %} +
  • +{% endmacro %} diff --git a/tildes/tildes/templates/macros/comments.jinja2 b/tildes/tildes/templates/macros/comments.jinja2 index 3b521e2..75275b8 100644 --- a/tildes/tildes/templates/macros/comments.jinja2 +++ b/tildes/tildes/templates/macros/comments.jinja2 @@ -1,6 +1,7 @@ {# Copyright (c) 2018 Tildes contributors #} {# SPDX-License-Identifier: AGPL-3.0-or-later #} +{% from 'buttons.jinja2' import post_action_toggle_button with context %} {% from 'datetime.jinja2' import adaptive_date_responsive %} {% from 'forms.jinja2' import markdown_textarea %} {% from 'links.jinja2' import username_linked %} @@ -207,26 +208,15 @@ {% endif %} {% if request.has_permission('bookmark', comment) %} - {% if comment.bookmark_created_time %} -
  • + {{ post_action_toggle_button( + subject=comment, + is_toggled=comment.bookmark_created_time, + normal_name="bookmark", + normal_label="Bookmark", + toggled_name="unbookmark", + toggled_label="Unbookmark", + route_name="ic_comment_bookmark", + ) }} {% endif %} {% if request.has_permission("remove", comment) %} diff --git a/tildes/tildes/templates/topic.jinja2 b/tildes/tildes/templates/topic.jinja2 index 87d85dd..7800706 100644 --- a/tildes/tildes/templates/topic.jinja2 +++ b/tildes/tildes/templates/topic.jinja2 @@ -3,6 +3,7 @@ {% extends 'base.jinja2' %} +{% from 'macros/buttons.jinja2' import post_action_toggle_button with context %} {% from 'macros/comments.jinja2' import comment_label_options_template, comment_reply_template, render_comment_tree with context %} {% from 'macros/datetime.jinja2' import adaptive_date_responsive, time_ago %} {% from 'macros/forms.jinja2' import markdown_textarea %} @@ -164,25 +165,15 @@ {% endif %} {% if request.has_permission('bookmark', topic) %} -
  • - {% if not topic.bookmark_created_time %} - - {% else %} - - {% endif %} -
  • + {{ post_action_toggle_button( + subject=topic, + is_toggled=topic.bookmark_created_time, + normal_name="bookmark", + normal_label="Bookmark", + toggled_name="unbookmark", + toggled_label="Unbookmark", + route_name="ic_topic_bookmark", + ) }} {% endif %} {% if request.has_permission("remove", topic) %} diff --git a/tildes/tildes/views/api/web/topic.py b/tildes/tildes/views/api/web/topic.py index 6097cce..2832dde 100644 --- a/tildes/tildes/views/api/web/topic.py +++ b/tildes/tildes/views/api/web/topic.py @@ -6,6 +6,7 @@ from marshmallow import ValidationError from marshmallow.fields import String from pyramid.httpexceptions import HTTPNotFound +from pyramid.renderers import render_to_response from pyramid.response import Response from pyramid.request import Request from sqlalchemy import cast, Text @@ -394,7 +395,7 @@ def put_topic_bookmark(request: Request) -> Response: # the user has already bookmarked this topic savepoint.rollback() - return Response("Bookmarked") + return _bookmark_toggle_button(topic, request, is_toggled=True) @ic_view_config( @@ -408,4 +409,22 @@ def delete_topic_bookmark(request: Request) -> Response: TopicBookmark.user == request.user, TopicBookmark.topic == topic ).delete(synchronize_session=False) - return Response("Unbookmarked") + return _bookmark_toggle_button(topic, request, is_toggled=False) + + +def _bookmark_toggle_button( + topic: Topic, request: Request, is_toggled: bool +) -> Response: + return render_to_response( + "tildes:templates/intercooler/post_action_toggle_button.jinja2", + value={ + "subject": topic, + "is_toggled": is_toggled, + "normal_name": "bookmark", + "normal_label": "Bookmark", + "toggled_name": "unbookmark", + "toggled_label": "Unbookmark", + "route_name": "ic_topic_bookmark", + }, + request=request, + )