From 32e2d7963a4585cfe768bfb60f70c70d570240f1 Mon Sep 17 00:00:00 2001 From: Robin Ward <robin.ward@gmail.com> Date: Fri, 4 Sep 2015 16:56:02 -0400 Subject: [PATCH] FEATURE: Show FAQ at top of the hamburger until the user reads it --- .../discourse/controllers/static.js.es6 | 7 ++-- .../templates/components/hamburger-menu.hbs | 17 ++++++++-- .../javascripts/discourse/views/static.js.es6 | 34 +++++++++++-------- .../stylesheets/common/base/menu-panel.scss | 7 ++++ app/controllers/users_controller.rb | 2 +- app/serializers/current_user_serializer.rb | 7 +++- config/locales/client.en.yml | 1 + 7 files changed, 54 insertions(+), 21 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/static.js.es6 b/app/assets/javascripts/discourse/controllers/static.js.es6 index c22ce3c26..16b4d731e 100644 --- a/app/assets/javascripts/discourse/controllers/static.js.es6 +++ b/app/assets/javascripts/discourse/controllers/static.js.es6 @@ -3,8 +3,11 @@ export default Ember.Controller.extend({ actions: { markFaqRead() { - if (this.currentUser) { - Discourse.ajax("/users/read-faq", { method: "POST" }); + const currentUser = this.currentUser; + if (currentUser) { + Discourse.ajax("/users/read-faq", { method: "POST" }).then(() => { + currentUser.set('read_faq', true); + }); } } } diff --git a/app/assets/javascripts/discourse/templates/components/hamburger-menu.hbs b/app/assets/javascripts/discourse/templates/components/hamburger-menu.hbs index 76289134c..8daa315a0 100644 --- a/app/assets/javascripts/discourse/templates/components/hamburger-menu.hbs +++ b/app/assets/javascripts/discourse/templates/components/hamburger-menu.hbs @@ -1,6 +1,17 @@ {{#menu-panel visible=visible}} + {{#unless currentUser.read_faq}} + {{#menu-links}} + <li> + {{#d-link path=faqUrl class="faq-link"}} + {{i18n "faq"}} + <span class='new'>{{i18n "new_item"}}</span> + {{/d-link}} + </li> + {{/menu-links}} + {{/unless}} + {{#if currentUser.staff}} - {{#menu-links class="columned"}} + {{#menu-links}} <li>{{d-link route="admin" class="admin-link" icon="wrench" label="admin_title"}}</li> <li> {{#d-link route="adminFlags" class="flagged-posts-link"}} @@ -70,7 +81,9 @@ {{#menu-links omitRule="true"}} <li>{{d-link route="about" class="about-link" label="about.simple_title"}}</li> - <li>{{d-link path=faqUrl class="faq-link" label="faq"}}</li> + {{#if currentUser.read_faq}} + <li>{{d-link path=faqUrl class="faq-link" label="faq"}}</li> + {{/if}} {{#if showKeyboardShortcuts}} <li>{{d-link action="keyboardShortcuts" class="keyboard-shortcuts-link" label="keyboard_shortcuts_help.title"}}</li> diff --git a/app/assets/javascripts/discourse/views/static.js.es6 b/app/assets/javascripts/discourse/views/static.js.es6 index 12dbfdf60..35ce3d674 100644 --- a/app/assets/javascripts/discourse/views/static.js.es6 +++ b/app/assets/javascripts/discourse/views/static.js.es6 @@ -1,24 +1,28 @@ import isElementInViewport from "discourse/lib/is-element-in-viewport"; import ScrollTop from 'discourse/mixins/scroll-top'; - -var readFaq = false; +import { on } from 'ember-addons/ember-computed-decorators'; export default Ember.View.extend(ScrollTop, { - _checkRead: function() { - const path = this.get('controller.model.path'); - if (path === "faq" || path === "guidelines") { - const controller = this.get('controller'); - $(window).on('load.faq resize.faq scroll.faq', function() { - if (!readFaq && isElementInViewport($(".contents p").last())) { - readFaq = true; - controller.send('markFaqRead'); - } - }); + @on('didInsertElement') + _checkRead() { + const currentUser = this.get('controller.currentUser'); + if (currentUser) { + const path = this.get('controller.model.path'); + if (path === "faq" || path === "guidelines") { + const controller = this.get('controller'); + $(window).on('load.faq resize.faq scroll.faq', function() { + const faqUnread = !currentUser.get('read_faq'); + if (faqUnread && isElementInViewport($(".contents p").last())) { + controller.send('markFaqRead'); + } + }); + } } - }.on('didInsertElement'), + }, - _stopChecking: function(){ + @on('willDestroyElement') + _stopChecking() { $(window).off('load.faq resize.faq scroll.faq'); - }.on('willDestroyElement') + } }); diff --git a/app/assets/stylesheets/common/base/menu-panel.scss b/app/assets/stylesheets/common/base/menu-panel.scss index ce8219a48..1e1c8cbb1 100644 --- a/app/assets/stylesheets/common/base/menu-panel.scss +++ b/app/assets/stylesheets/common/base/menu-panel.scss @@ -62,6 +62,13 @@ background-color: dark-light-diff($highlight, $secondary, 50%, -55%); } } + + .new { + font-size: 0.8em; + margin-left: 0.5em; + color: dark-light-choose(scale-color($primary, $lightness: 50%), scale-color($secondary, $lightness: 50%)); + } + } li.category-link { diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index bb550483d..d537c6b7a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -572,7 +572,7 @@ class UsersController < ApplicationController end def read_faq - if(user = current_user) + if user = current_user user.user_stat.read_faq = 1.second.ago user.user_stat.save end diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rb index cc689e42b..71306753b 100644 --- a/app/serializers/current_user_serializer.rb +++ b/app/serializers/current_user_serializer.rb @@ -30,12 +30,17 @@ class CurrentUserSerializer < BasicUserSerializer :dismissed_banner_key, :is_anonymous, :post_queue_new_count, - :show_queued_posts + :show_queued_posts, + :read_faq def include_site_flagged_posts_count? object.staff? end + def read_faq + object.user_stat.read_faq? + end + def topic_count object.topics.count end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index fd9845690..e92de892c 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -952,6 +952,7 @@ en: private_messages: "Search messages" hamburger_menu: "go to another topic list or category" + new_item: "New!" go_back: 'go back' not_logged_in_user: 'user page with summary of current activity and preferences' current_user: 'go to your user page'