Browse Source

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.
merge-requests/69/head
Deimos 6 years ago
parent
commit
12ef77c52e
  1. 14
      tildes/tildes/templates/intercooler/post_action_toggle_button.jinja2
  2. 39
      tildes/tildes/templates/macros/buttons.jinja2
  3. 30
      tildes/tildes/templates/macros/comments.jinja2
  4. 29
      tildes/tildes/templates/topic.jinja2
  5. 23
      tildes/tildes/views/api/web/topic.py

14
tildes/tildes/templates/intercooler/post_action_toggle_button.jinja2

@ -0,0 +1,14 @@
{# Copyright (c) 2019 Tildes contributors <code@tildes.net> #}
{# 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,
) }}

39
tildes/tildes/templates/macros/buttons.jinja2

@ -0,0 +1,39 @@
{# Copyright (c) 2019 Tildes contributors <code@tildes.net> #}
{# 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 %}
<li><button class="btn-post-action btn-post-action-used" name="{{ toggled_name }}"
{% if subject is comment %}
data-ic-delete-from="{{ request.route_url(
route_name,
comment_id36=subject.comment_id36,
) }}"
data-ic-target="#comment-{{ subject.comment_id36 }} .comment-itself:first"
{% elif subject is topic %}
data-ic-delete-from="{{ request.route_url(
route_name,
topic_id36=subject.topic_id36,
) }}"
{% endif %}
data-ic-replace-target="true"
>{{ toggled_label }}
{% else %}
<li><button class="btn-post-action" name="{{ normal_name }}"
{% if subject is comment %}
data-ic-put-to="{{ request.route_url(
route_name,
comment_id36=subject.comment_id36,
) }}"
data-ic-target="#comment-{{ subject.comment_id36 }} .comment-itself:first"
{% elif subject is topic %}
data-ic-put-to="{{ request.route_url(
route_name,
topic_id36=subject.topic_id36,
) }}"
{% endif %}
data-ic-replace-target="true"
>{{ normal_label }}
{% endif %}
</button></li>
{% endmacro %}

30
tildes/tildes/templates/macros/comments.jinja2

@ -1,6 +1,7 @@
{# Copyright (c) 2018 Tildes contributors <code@tildes.net> #} {# Copyright (c) 2018 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #} {# 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 'datetime.jinja2' import adaptive_date_responsive %}
{% from 'forms.jinja2' import markdown_textarea %} {% from 'forms.jinja2' import markdown_textarea %}
{% from 'links.jinja2' import username_linked %} {% from 'links.jinja2' import username_linked %}
@ -207,26 +208,15 @@
{% endif %} {% endif %}
{% if request.has_permission('bookmark', comment) %} {% if request.has_permission('bookmark', comment) %}
{% if comment.bookmark_created_time %}
<li><button class="btn-post-action btn-post-action-used" name="unbookmark"
data-ic-delete-from="{{ request.route_url(
'ic_comment_bookmark',
comment_id36=comment.comment_id36,
) }}"
data-ic-target="#comment-{{ comment.comment_id36 }} .comment-itself:first"
data-ic-replace-target="true"
>Bookmarked
{% else %}
<li><button class="btn-post-action" name="bookmark"
data-ic-put-to="{{ request.route_url(
'ic_comment_bookmark',
comment_id36=comment.comment_id36,
) }}"
data-ic-target="#comment-{{ comment.comment_id36 }} .comment-itself:first"
data-ic-replace-target="true"
>Bookmark
{% endif %}
</button></li>
{{ 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 %} {% endif %}
{% if request.has_permission("remove", comment) %} {% if request.has_permission("remove", comment) %}

29
tildes/tildes/templates/topic.jinja2

@ -3,6 +3,7 @@
{% extends 'base.jinja2' %} {% 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/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/datetime.jinja2' import adaptive_date_responsive, time_ago %}
{% from 'macros/forms.jinja2' import markdown_textarea %} {% from 'macros/forms.jinja2' import markdown_textarea %}
@ -164,25 +165,15 @@
{% endif %} {% endif %}
{% if request.has_permission('bookmark', topic) %} {% if request.has_permission('bookmark', topic) %}
<li>
{% if not topic.bookmark_created_time %}
<button class="btn-post-action"
data-ic-put-to="{{ request.route_url(
'ic_topic_bookmark',
topic_id36=topic.topic_id36,
) }}"
data-ic-replace-target="true"
>Bookmark</button>
{% else %}
<button class="btn-post-action"
data-ic-delete-from="{{ request.route_url(
'ic_topic_bookmark',
topic_id36=topic.topic_id36,
) }}"
data-ic-replace-target="true"
>Unbookmark</button>
{% endif %}
</li>
{{ 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 %} {% endif %}
{% if request.has_permission("remove", topic) %} {% if request.has_permission("remove", topic) %}

23
tildes/tildes/views/api/web/topic.py

@ -6,6 +6,7 @@
from marshmallow import ValidationError from marshmallow import ValidationError
from marshmallow.fields import String from marshmallow.fields import String
from pyramid.httpexceptions import HTTPNotFound from pyramid.httpexceptions import HTTPNotFound
from pyramid.renderers import render_to_response
from pyramid.response import Response from pyramid.response import Response
from pyramid.request import Request from pyramid.request import Request
from sqlalchemy import cast, Text from sqlalchemy import cast, Text
@ -394,7 +395,7 @@ def put_topic_bookmark(request: Request) -> Response:
# the user has already bookmarked this topic # the user has already bookmarked this topic
savepoint.rollback() savepoint.rollback()
return Response("Bookmarked")
return _bookmark_toggle_button(topic, request, is_toggled=True)
@ic_view_config( @ic_view_config(
@ -408,4 +409,22 @@ def delete_topic_bookmark(request: Request) -> Response:
TopicBookmark.user == request.user, TopicBookmark.topic == topic TopicBookmark.user == request.user, TopicBookmark.topic == topic
).delete(synchronize_session=False) ).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,
)
Loading…
Cancel
Save