1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

(bug) products filter: is_available

previously, we misunderstand the behavior or this filter and
we used the filter is_active instead
This commit is contained in:
Sylvain 2022-10-03 16:32:32 +02:00
parent e51592061a
commit db86769d42
5 changed files with 33 additions and 10 deletions

View File

@ -32,6 +32,19 @@ import { CaretDoubleDown } from 'phosphor-react';
declare const Application: IApplication;
const storeInitialFilters = {
...initialFilters,
is_active: true
};
const storeInitialResources = {
...initialResources,
filters: {
data: storeInitialFilters,
ready: false
}
};
interface StoreProps {
onError: (message: string) => void,
onSuccess: (message: string) => void,
@ -54,7 +67,7 @@ const Store: React.FC<StoreProps> = ({ onError, onSuccess, currentUser, uiRouter
const [products, setProducts] = useState<Array<Product>>([]);
// this includes the resources fetch from the API (machines, categories) and from the URL (filters)
const [resources, setResources] = useImmer<ProductResourcesFetching>(initialResources);
const [resources, setResources] = useImmer<ProductResourcesFetching>(storeInitialResources);
const [machinesModule, setMachinesModule] = useState<boolean>(false);
const [categoriesTree, setCategoriesTree] = useState<CategoryTree[]>([]);
const [filtersPanel, setFiltersPanel] = useState<boolean>(false);
@ -83,7 +96,7 @@ const Store: React.FC<StoreProps> = ({ onError, onSuccess, currentUser, uiRouter
return {
...draft,
filters: {
data: ProductLib.readFiltersFromUrl(uiRouter.globals.params, resources.machines.data, resources.categories.data),
data: ProductLib.readFiltersFromUrl(uiRouter.globals.params, resources.machines.data, resources.categories.data, storeInitialFilters),
ready: true
}
};
@ -142,7 +155,7 @@ const Store: React.FC<StoreProps> = ({ onError, onSuccess, currentUser, uiRouter
filters: {
...draft.filters,
data: {
...initialFilters,
...storeInitialFilters,
categories: draft.filters.data.categories
}
}
@ -172,7 +185,7 @@ const Store: React.FC<StoreProps> = ({ onError, onSuccess, currentUser, uiRouter
* Filter: toggle non-available products visibility
*/
const toggleVisible = (checked: boolean) => {
ProductLib.updateFilter(setResources, 'is_active', checked);
ProductLib.updateFilter(setResources, 'is_available', checked);
};
/**
@ -304,7 +317,7 @@ const Store: React.FC<StoreProps> = ({ onError, onSuccess, currentUser, uiRouter
selectOptions={buildOptions()}
onSelectOptionsChange={handleSorting}
switchLabel={t('app.public.store.products.in_stock_only')}
switchChecked={resources.filters.data.is_active}
switchChecked={resources.filters.data.is_available}
selectValue={resources.filters.data.sort}
onSwitch={toggleVisible}
/>

View File

@ -143,12 +143,12 @@ export default class ProductLib {
/**
* Parse the provided URL and return a ready-to-use filter object
*/
static readFiltersFromUrl = (params: StateParams, machines: Array<Machine>, categories: Array<ProductCategory>): ProductIndexFilter => {
const res: ProductIndexFilter = { ...initialFilters };
static readFiltersFromUrl = (params: StateParams, machines: Array<Machine>, categories: Array<ProductCategory>, defaultFilters = initialFilters): ProductIndexFilter => {
const res: ProductIndexFilter = { ...defaultFilters };
for (const key in params) {
if (['#', 'categoryTypeUrl'].includes(key) || !Object.prototype.hasOwnProperty.call(params, key)) continue;
const value = ParsingLib.parse(params[key]) || initialFilters[key];
const value = ParsingLib.parse(params[key]) || defaultFilters[key];
switch (key) {
case 'category': {
const parents = categories?.filter(c => (value as Array<string>)?.includes(c.slug));

View File

@ -7,6 +7,7 @@ export type ProductSortOption = 'name-asc' | 'name-desc' | 'amount-asc' | 'amoun
export interface ProductIndexFilter {
is_active?: boolean,
is_available?: boolean,
page?: number,
categories?: ProductCategory[],
machines?: Machine[],
@ -40,6 +41,7 @@ export const initialFilters: ProductIndexFilter = {
keywords: [],
machines: [],
is_active: false,
is_available: false,
stock_type: 'internal',
stock_from: 0,
stock_to: 0,

View File

@ -626,7 +626,7 @@ angular.module('application.router', ['ui.router'])
// store
.state('app.public.store', {
url: '/store/:categoryTypeUrl/:category?{machines:string}{keywords:string}{is_active:string}{page:string}{sort:string}',
url: '/store/:categoryTypeUrl/:category?{machines:string}{keywords:string}{is_active:string}{is_available:string}{page:string}{sort:string}',
abstract: !Fablab.storeModule,
views: {
'main@': {
@ -639,7 +639,8 @@ angular.module('application.router', ['ui.router'])
category: { dynamic: true, type: 'path', raw: true, value: null, squash: true },
machines: { array: true, dynamic: true, type: 'query', raw: true },
keywords: { dynamic: true, type: 'query' },
is_active: { dynamic: true, type: 'query', value: 'false', squash: true },
is_active: { dynamic: true, type: 'query', value: 'true', squash: true },
is_available: { dynamic: true, type: 'query', value: 'false', squash: true },
page: { dynamic: true, type: 'query', value: '1', squash: true },
sort: { dynamic: true, type: 'query' }
}

View File

@ -8,6 +8,7 @@ class ProductService
def list(filters, operator)
products = Product.includes(:product_images)
products = filter_by_active(products, filters)
products = filter_by_available(products, filters, operator)
products = filter_by_categories(products, filters)
products = filter_by_machines(products, filters)
products = filter_by_keyword_or_reference(products, filters)
@ -89,6 +90,12 @@ class ProductService
products.where(is_active: state)
end
def filter_by_available(products, filters, operator)
return products if filters[:is_available].blank? || filters[:is_available] == 'false'
filter_by_stock(products, { stock_type: 'external', stock_from: '1' }, operator)
end
def filter_by_categories(products, filters)
return products if filters[:categories].blank?