mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 03:57:52 +00:00
chore: Add formatting for the view count (#6447)
This commit is contained in:
@@ -16,8 +16,7 @@ class Public::Api::V1::Portals::ArticlesController < PublicController
|
|||||||
|
|
||||||
def set_article
|
def set_article
|
||||||
@article = @category.articles.find(params[:id])
|
@article = @category.articles.find(params[:id])
|
||||||
@article.views = @article.views ? @article.views + 1 : 1
|
@article.increment_view_count
|
||||||
@article.save
|
|
||||||
@parsed_content = render_article_content(@article.content)
|
@parsed_content = render_article_content(@article.content)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -29,8 +29,8 @@
|
|||||||
</router-link>
|
</router-link>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<span class="fs-small">
|
<span class="fs-small" :title="formattedViewCount">
|
||||||
{{ views || 0 }}
|
{{ readableViewCount }}
|
||||||
</span>
|
</span>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
@@ -95,6 +95,15 @@ export default {
|
|||||||
lastUpdatedAt() {
|
lastUpdatedAt() {
|
||||||
return this.dynamicTime(this.updatedAt);
|
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() {
|
articleAuthorName() {
|
||||||
return this.author.name;
|
return this.author.name;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -106,6 +106,12 @@ class Article < ApplicationRecord
|
|||||||
update(status: :draft)
|
update(status: :draft)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def increment_view_count
|
||||||
|
# rubocop:disable Rails/SkipsModelValidations
|
||||||
|
update_column(:views, views? ? views + 1 : 1)
|
||||||
|
# rubocop:enable Rails/SkipsModelValidations
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def ensure_account_id
|
def ensure_account_id
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ RSpec.describe 'Public Articles API', type: :request do
|
|||||||
describe 'GET /public/api/v1/portals/:slug/articles/:id' do
|
describe 'GET /public/api/v1/portals/:slug/articles/:id' do
|
||||||
it 'Fetch article with the id' do
|
it 'Fetch article with the id' do
|
||||||
get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/#{article.id}"
|
get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/#{article.id}"
|
||||||
|
|
||||||
expect(response).to have_http_status(:success)
|
expect(response).to have_http_status(:success)
|
||||||
|
expect(article.reload.views).to eq 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,6 +7,6 @@ FactoryBot.define do
|
|||||||
content { 'MyText' }
|
content { 'MyText' }
|
||||||
description { 'MyDescrption' }
|
description { 'MyDescrption' }
|
||||||
status { 1 }
|
status { 1 }
|
||||||
views { 1 }
|
views { 0 }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user