mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-20 14:54:15 +01:00
(feat) count products in categories
This commit is contained in:
parent
a260f88555
commit
e51592061a
@ -51,7 +51,7 @@ export const ProductCategoriesItem: React.FC<ProductCategoriesItemProps> = ({ pr
|
||||
</button>
|
||||
</div>}
|
||||
<p className='itemInfo-name'>{category.name}</p>
|
||||
<span className='itemInfo-count'>[count]</span>
|
||||
<span className='itemInfo-count'>{category.products_count}</span>
|
||||
</div>
|
||||
<div className='actions'>
|
||||
{!isDragging &&
|
||||
|
@ -255,7 +255,16 @@ const Store: React.FC<StoreProps> = ({ onError, onSuccess, currentUser, uiRouter
|
||||
{categoriesTree.map(c =>
|
||||
<div key={c.parent.id} className={`parent ${selectedCategory?.id === c.parent.id || selectedCategory?.parent_id === c.parent.id ? 'is-active' : ''}`}>
|
||||
<p onClick={() => filterCategory(c.parent)}>
|
||||
{c.parent.name}<span>(count)</span>
|
||||
{c.parent.name}
|
||||
<span>
|
||||
{/* here we add the parent count with the sum of all children counts */}
|
||||
{
|
||||
c.parent.products_count +
|
||||
c.children
|
||||
.map(ch => ch.products_count)
|
||||
.reduce((sum, val) => sum + val, 0)
|
||||
}
|
||||
</span>
|
||||
</p>
|
||||
{c.children.length > 0 &&
|
||||
<div className='children'>
|
||||
@ -263,7 +272,7 @@ const Store: React.FC<StoreProps> = ({ onError, onSuccess, currentUser, uiRouter
|
||||
<p key={ch.id}
|
||||
className={selectedCategory?.id === ch.id ? 'is-active' : ''}
|
||||
onClick={() => filterCategory(ch)}>
|
||||
{ch.name}<span>(count)</span>
|
||||
{ch.name}<span>{ch.products_count}</span>
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
@ -4,4 +4,5 @@ export interface ProductCategory {
|
||||
slug: string,
|
||||
parent_id?: number,
|
||||
position: number,
|
||||
products_count: number
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ class ProductCategory < ApplicationRecord
|
||||
validates :slug, uniqueness: true
|
||||
|
||||
belongs_to :parent, class_name: 'ProductCategory'
|
||||
has_many :children, class_name: 'ProductCategory', foreign_key: :parent_id
|
||||
has_many :children, class_name: 'ProductCategory', foreign_key: :parent_id, inverse_of: :parent, dependent: :nullify
|
||||
|
||||
has_many :products
|
||||
has_many :products, dependent: :nullify
|
||||
|
||||
acts_as_list scope: :parent, top_of_list: 0
|
||||
end
|
||||
|
@ -3,7 +3,9 @@
|
||||
# Provides methods for ProductCategory
|
||||
class ProductCategoryService
|
||||
def self.list
|
||||
ProductCategory.all.order(parent_id: :asc, position: :asc)
|
||||
ProductCategory.left_outer_joins(:products)
|
||||
.select('product_categories.*, count(products.*) as products_count')
|
||||
.group('product_categories.id')
|
||||
end
|
||||
|
||||
def self.destroy(product_category)
|
||||
|
@ -1,3 +1,3 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
json.extract! product_category, :id, :name, :slug, :parent_id, :position
|
||||
json.extract! product_category, :id, :name, :slug, :parent_id, :position, :products_count
|
||||
|
@ -0,0 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# ProductCategory's slugs should validate uniqness in database
|
||||
class AddIndexOnProductCategorySlug < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_index :product_categories, :slug, unique: true
|
||||
end
|
||||
end
|
@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2022_09_20_131912) do
|
||||
ActiveRecord::Schema.define(version: 2022_10_03_133019) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "fuzzystrmatch"
|
||||
@ -642,6 +642,7 @@ ActiveRecord::Schema.define(version: 2022_09_20_131912) do
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["parent_id"], name: "index_product_categories_on_parent_id"
|
||||
t.index ["slug"], name: "index_product_categories_on_slug", unique: true
|
||||
end
|
||||
|
||||
create_table "product_stock_movements", force: :cascade do |t|
|
||||
|
Loading…
x
Reference in New Issue
Block a user