u
This commit is contained in:
@@ -11,6 +11,9 @@
|
||||
*/
|
||||
|
||||
import { Automat } from 'react-automat';
|
||||
import categoryAutomat from './categoryAutomat';
|
||||
import { buildCategoryPath, resolveCategoryIdFromSlugPath } from '../components/category/categoryRoute';
|
||||
import { buildSearchUrl } from '../components/article/artikelQuery';
|
||||
|
||||
/**
|
||||
* @typedef {Object} NavigationState
|
||||
@@ -18,6 +21,7 @@ import { Automat } from 'react-automat';
|
||||
* @property {string|null} lastArticleSku Last article SKU visited by the user.
|
||||
* @property {string|null} lastSearchTerm Last article search query term.
|
||||
* @property {string|null} lastArticleSearchPath Full last search path (e.g. `/artikel?target=apfel`).
|
||||
* @property {string|null} lastCategoryId Last visited category ID.
|
||||
* @property {string|null} lastCategorySlug Last visited category slug (e.g. `obst-gemuese/obst`).
|
||||
* @property {string|null} lastCategoryPath Full last category path (e.g. `/kategorien/obst-gemuese/obst`).
|
||||
*/
|
||||
@@ -28,6 +32,7 @@ const initialState = {
|
||||
lastArticleSku: null,
|
||||
lastSearchTerm: null,
|
||||
lastArticleSearchPath: null,
|
||||
lastCategoryId: null,
|
||||
lastCategorySlug: null,
|
||||
lastCategoryPath: null,
|
||||
};
|
||||
@@ -70,7 +75,8 @@ const navigationAutomat = new Automat(
|
||||
*/
|
||||
setLastSearchTerm(term, path = null) {
|
||||
const trimmed = term != null ? String(term) : '';
|
||||
const searchPath = path ?? (trimmed ? `/artikel?target=${encodeURIComponent(trimmed)}` : '/artikel');
|
||||
const currentCatId = navigationAutomat.state.lastCategoryId ?? '';
|
||||
const searchPath = path ?? buildSearchUrl(trimmed, currentCatId, 0);
|
||||
navigationAutomat.setState({
|
||||
lastSearchTerm: trimmed || null,
|
||||
lastArticleSearchPath: searchPath,
|
||||
@@ -91,6 +97,41 @@ const navigationAutomat = new Automat(
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the active category ID and keep categoryPath & articleSearchPath in sync.
|
||||
*
|
||||
* @param {string|number|null} categoryId Category ID or null for root.
|
||||
* @param {string|null} [path] Full category path if already known.
|
||||
* @returns {void}
|
||||
*/
|
||||
setLastCategoryId(categoryId, path = null) {
|
||||
const id = categoryId != null ? String(categoryId) : null;
|
||||
const categories = categoryAutomat.state?.categories;
|
||||
const rootCategoryId = categoryAutomat.state?.rootCategoryId;
|
||||
const isRoot = !id || (rootCategoryId != null && id === String(rootCategoryId));
|
||||
|
||||
let categoryPath = path;
|
||||
if (!categoryPath) {
|
||||
if (!isRoot && categories && rootCategoryId != null) {
|
||||
categoryPath = buildCategoryPath(categories, rootCategoryId, id);
|
||||
} else {
|
||||
categoryPath = '/kategorien';
|
||||
}
|
||||
}
|
||||
|
||||
const cleanSlug = categoryPath.replace(/^\/kategorien\/?/, '').replace(/\/+$/, '') || null;
|
||||
const currentTerm = navigationAutomat.state.lastSearchTerm ?? '';
|
||||
const filter = isRoot ? '' : id;
|
||||
const searchPath = buildSearchUrl(currentTerm, filter, 0);
|
||||
|
||||
navigationAutomat.setState({
|
||||
lastCategoryId: isRoot ? null : id,
|
||||
lastCategorySlug: isRoot ? null : cleanSlug,
|
||||
lastCategoryPath: categoryPath,
|
||||
lastArticleSearchPath: searchPath,
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the last visited category slug.
|
||||
*
|
||||
@@ -100,10 +141,7 @@ const navigationAutomat = new Automat(
|
||||
setLastCategorySlug(slug) {
|
||||
const clean = slug ? String(slug).replace(/^\/+|\/+$/g, '') : '';
|
||||
const path = clean ? `/kategorien/${clean}` : '/kategorien';
|
||||
navigationAutomat.setState({
|
||||
lastCategorySlug: clean || null,
|
||||
lastCategoryPath: path,
|
||||
});
|
||||
navigationAutomat.actions.setLastCategoryPath(path);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -114,10 +152,24 @@ const navigationAutomat = new Automat(
|
||||
*/
|
||||
setLastCategoryPath(path) {
|
||||
const cleanPath = path ? String(path).replace(/^\/+/, '/') : '/kategorien';
|
||||
const slug = cleanPath.replace(/^\/kategorien\/?/, '').replace(/\/+$/, '');
|
||||
const slug = cleanPath.replace(/^\/kategorien\/?/, '').replace(/\/+$/, '') || null;
|
||||
const categories = categoryAutomat.state?.categories;
|
||||
const rootCategoryId = categoryAutomat.state?.rootCategoryId;
|
||||
const resolvedId = slug && categories && rootCategoryId != null
|
||||
? resolveCategoryIdFromSlugPath(categories, rootCategoryId, slug)
|
||||
: null;
|
||||
const isRoot = !resolvedId || (rootCategoryId != null && String(resolvedId) === String(rootCategoryId));
|
||||
const catId = isRoot ? null : String(resolvedId);
|
||||
|
||||
const currentTerm = navigationAutomat.state.lastSearchTerm ?? '';
|
||||
const filter = catId ?? '';
|
||||
const searchPath = buildSearchUrl(currentTerm, filter, 0);
|
||||
|
||||
navigationAutomat.setState({
|
||||
lastCategorySlug: slug || null,
|
||||
lastCategoryId: catId,
|
||||
lastCategorySlug: isRoot ? null : slug,
|
||||
lastCategoryPath: cleanPath,
|
||||
lastArticleSearchPath: searchPath,
|
||||
});
|
||||
},
|
||||
|
||||
@@ -142,6 +194,7 @@ export const {
|
||||
setLastArticleSku,
|
||||
setLastSearchTerm,
|
||||
setLastArticleSearchPath,
|
||||
setLastCategoryId,
|
||||
setLastCategorySlug,
|
||||
setLastCategoryPath,
|
||||
clearNavigation,
|
||||
|
||||
Reference in New Issue
Block a user