diff --git a/app/controllers/public/api/v1/portals/articles_controller.rb b/app/controllers/public/api/v1/portals/articles_controller.rb index bd490f240..22ffbbf7a 100644 --- a/app/controllers/public/api/v1/portals/articles_controller.rb +++ b/app/controllers/public/api/v1/portals/articles_controller.rb @@ -16,8 +16,7 @@ class Public::Api::V1::Portals::ArticlesController < PublicController def set_article @article = @category.articles.find(params[:id]) - @article.views = @article.views ? @article.views + 1 : 1 - @article.save + @article.increment_view_count @parsed_content = render_article_content(@article.content) end diff --git a/app/javascript/dashboard/routes/dashboard/helpcenter/components/ArticleItem.vue b/app/javascript/dashboard/routes/dashboard/helpcenter/components/ArticleItem.vue index 759389da6..00dba9eb8 100644 --- a/app/javascript/dashboard/routes/dashboard/helpcenter/components/ArticleItem.vue +++ b/app/javascript/dashboard/routes/dashboard/helpcenter/components/ArticleItem.vue @@ -29,8 +29,8 @@ - - {{ views || 0 }} + + {{ readableViewCount }} @@ -95,6 +95,15 @@ export default { lastUpdatedAt() { return this.dynamicTime(this.updatedAt); }, + formattedViewCount() { + return Number(this.views || 0).toLocaleString('en'); + }, + readableViewCount() { + return new Intl.NumberFormat('en-US', { + notation: 'compact', + compactDisplay: 'short', + }).format(this.views || 0); + }, articleAuthorName() { return this.author.name; }, diff --git a/app/models/article.rb b/app/models/article.rb index 77b86b2ca..842bf2a6f 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -106,6 +106,12 @@ class Article < ApplicationRecord update(status: :draft) end + def increment_view_count + # rubocop:disable Rails/SkipsModelValidations + update_column(:views, views? ? views + 1 : 1) + # rubocop:enable Rails/SkipsModelValidations + end + private def ensure_account_id diff --git a/spec/controllers/public/api/v1/portals/articles_controller_spec.rb b/spec/controllers/public/api/v1/portals/articles_controller_spec.rb index 53c455c1b..0f074ea29 100644 --- a/spec/controllers/public/api/v1/portals/articles_controller_spec.rb +++ b/spec/controllers/public/api/v1/portals/articles_controller_spec.rb @@ -41,8 +41,8 @@ RSpec.describe 'Public Articles API', type: :request do describe 'GET /public/api/v1/portals/:slug/articles/:id' do it 'Fetch article with the id' do get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/#{article.id}" - expect(response).to have_http_status(:success) + expect(article.reload.views).to eq 1 end end end diff --git a/spec/factories/articles.rb b/spec/factories/articles.rb index ec9c4cd2d..e68ed8f4b 100644 --- a/spec/factories/articles.rb +++ b/spec/factories/articles.rb @@ -7,6 +7,6 @@ FactoryBot.define do content { 'MyText' } description { 'MyDescrption' } status { 1 } - views { 1 } + views { 0 } end end