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

View File

@@ -1,35 +1,17 @@
import React from "react"; 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 Stanovi from "../subcategories/nekretnine/Stanovi";
import Kuce from "../subcategories/nekretnine/Kuce"; import Kuce from "../subcategories/nekretnine/Kuce";
const options = [{ value: 23, label: "Stanovi" }, { value: 24, label: "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 { export const properties = {
handleChange = selectedOption => { options,
this.props.onSubCategoryChanged(selectedOption); depth,
}; childrenComponents
};
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);

View File

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

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"; import { SUBCATEGORY_SELECT, CATEGORY_SELECT } from "constants/actionTypes";
export default (state = null, action) => { export default (state = {}, action) => {
switch (action.type) { switch (action.type) {
case SUBCATEGORY_SELECT: case SUBCATEGORY_SELECT:
return action.option; return { ...state, [action.option.depth]: action.option.selectedOption };
case CATEGORY_SELECT: case CATEGORY_SELECT:
return null; return {};
default: default:
return state; return state;
} }
}; };

View File

@@ -6,10 +6,16 @@ const mapOptionToLink = (options, option) =>
}&` }&`
: ""; : "";
export const createOlxLink = (category, subcategory, options) => export const createOlxLink = (category, subcategory, options) => {
subcategory.value const deepSubCategory =
Boolean(Object.keys(subcategory).length) &&
subcategory[
Object.keys(subcategory).reduce((max, key) => (max < key ? key : max))
];
return deepSubCategory
? Object.keys(options).reduce( ? Object.keys(options).reduce(
(link, option) => link + mapOptionToLink(options, option), (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) { export const areObjectEqual = function checkEquality(objectA, objectB) {
return ( return (
Object.keys(objectA).length === Object.keys(objectB).length && Object.keys(objectA).length === Object.keys(objectB).length &&