deep subcategory solution

This commit is contained in:
egradanin
2019-01-16 21:00:14 +01:00
parent ae446d5333
commit 2945ca8b70
7 changed files with 75 additions and 73 deletions

View File

@@ -6,8 +6,9 @@ import { hoc, areObjectEqual } from "utils/helpers";
import { createOlxLink } from "utils/createOlxLink";
import axios from "axios";
import Vozila from "./categories/Vozila";
import Nekretnine from "./categories/Nekretnine";
import * as Vozila from "./categories/Vozila";
import * as Nekretnine from "./categories/Nekretnine";
import DeepCategoryWrapper from "components/widgets/DeepCategoryWrapper";
import ItemsContainer from "./items/itemscontainer/ItemsContainer";
const options = [
@@ -77,8 +78,8 @@ class App extends React.Component {
options={options}
/>
{hoc(category && category.value, {
Vozila: <Vozila />,
Nekretnine: <Nekretnine />
Vozila: <DeepCategoryWrapper {...Vozila.properties} />,
Nekretnine: <DeepCategoryWrapper {...Nekretnine.properties} />
})}
<ItemsContainer />
</div>

View File

@@ -1,35 +1,17 @@
import React from "react";
import Select from "react-select";
import { subcategorywrapper } from "utils/subcategorywrapper";
import { hoc } from "utils/helpers";
import Stanovi from "../subcategories/nekretnine/Stanovi";
import Kuce from "../subcategories/nekretnine/Kuce";
const options = [{ value: 23, label: "Stanovi" }, { value: 24, label: "Kuce" }];
const depth = 0;
const childrenComponents = {
23: <Stanovi />,
24: <Kuce />
};
class Nekretnine extends React.Component {
handleChange = selectedOption => {
this.props.onSubCategoryChanged(selectedOption);
};
render() {
const { subcategory } = this.props;
return (
<div>
<Select
value={subcategory}
onChange={this.handleChange}
options={options}
/>
{hoc(subcategory && subcategory.value, {
23: <Stanovi />,
24: <Kuce />
})}
</div>
);
}
}
export default subcategorywrapper(Nekretnine);
export const properties = {
options,
depth,
childrenComponents
};

View File

@@ -1,37 +1,20 @@
import React from "react";
import Select from "react-select";
import Automobili from "../subcategories/vozila/Automobili";
import Motocikli from "../subcategories/vozila/Motocikli";
import { subcategorywrapper } from "utils/subcategorywrapper";
import { hoc } from "utils/helpers";
const options = [
{ value: 18, label: "Automobili" },
{ value: 21, label: "Motocikli" }
];
const depth = 0;
const childrenComponents = {
18: <Automobili />,
21: <Motocikli />
};
class Vozila extends React.Component {
handleChange = selectedOption => {
this.props.onSubCategoryChanged(selectedOption);
};
render() {
const { subcategory } = this.props;
return (
<div>
<Select
value={subcategory}
onChange={this.handleChange}
options={options}
/>
{hoc(subcategory && subcategory.value, {
18: <Automobili />,
21: <Motocikli />
})}
</div>
);
}
}
export default subcategorywrapper(Vozila);
export const properties = {
options,
depth,
childrenComponents
};

View File

@@ -0,0 +1,30 @@
import React from "react";
import Select from "react-select";
import { subcategorywrapper } from "utils/subcategorywrapper";
import { hoc } from "utils/helpers";
class DeepCategoryWrapper extends React.Component {
handleOptionChange = selectedOption => {
const { depth, onSubCategoryChanged } = this.props;
onSubCategoryChanged({ selectedOption, depth });
};
render() {
const { options, depth, childrenComponents } = this.props;
const {
subcategory: { [depth]: deepSubCategory }
} = this.props;
return (
<div>
<Select
value={deepSubCategory || null}
onChange={this.handleOptionChange}
options={options}
/>
{hoc(deepSubCategory && deepSubCategory.value, childrenComponents)}
</div>
);
}
}
export default subcategorywrapper(DeepCategoryWrapper);

View File

@@ -1,12 +1,12 @@
import { SUBCATEGORY_SELECT, CATEGORY_SELECT } from "constants/actionTypes";
export default (state = null, action) => {
switch (action.type) {
case SUBCATEGORY_SELECT:
return action.option;
case CATEGORY_SELECT:
return null;
default:
return state;
}
export default (state = {}, action) => {
switch (action.type) {
case SUBCATEGORY_SELECT:
return { ...state, [action.option.depth]: action.option.selectedOption };
case CATEGORY_SELECT:
return {};
default:
return state;
}
};

View File

@@ -6,10 +6,16 @@ const mapOptionToLink = (options, option) =>
}&`
: "";
export const createOlxLink = (category, subcategory, options) =>
subcategory.value
export const createOlxLink = (category, subcategory, options) => {
const deepSubCategory =
Boolean(Object.keys(subcategory).length) &&
subcategory[
Object.keys(subcategory).reduce((max, key) => (max < key ? key : max))
];
return deepSubCategory
? Object.keys(options).reduce(
(link, option) => link + mapOptionToLink(options, option),
`kategorija=${subcategory.value}&`
`kategorija=${deepSubCategory.value}&`
)
: "";
};

View File

@@ -1,4 +1,4 @@
export const hoc = (option, componentList) => componentList[option] || null;
export const hoc = (option, componentList) => componentList[option];
export const areObjectEqual = function checkEquality(objectA, objectB) {
return (
Object.keys(objectA).length === Object.keys(objectB).length &&