Skip to content

Commit 459fa61

Browse files
committed
Refactored Sort Container
1 parent 83701d9 commit 459fa61

File tree

7 files changed

+82
-24
lines changed

7 files changed

+82
-24
lines changed

src/App.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ function createDriver() {
1717
searchKey,
1818
engineName,
1919
initialState: {
20-
resultsPerPage: 20
20+
resultsPerPage: 20,
21+
sort: {
22+
value: "",
23+
direction: ""
24+
}
2125
},
2226
searchOptions: buildSearchOptionsFromConfig()
2327
});

src/app-search/AppSearchDriver.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,14 @@ export default class AppSearchDriver {
118118
}
119119
};
120120

121-
if (Object.keys(sort).length > 0) {
122-
searchOptions.sort = sort;
121+
if (
122+
Object.keys(sort).length > 0 &&
123+
sort.value.length > 1 &&
124+
sort.direction.length > 1
125+
) {
126+
searchOptions.sort = {
127+
[sort.value]: sort.direction
128+
};
123129
}
124130

125131
return this.client.search(searchTerm, searchOptions).then(resultList => {

src/components/Body.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,41 @@ import Paging from "../containers/Paging";
66
import Results from "../containers/Results";
77
import ResultsPerPage from "../containers/ResultsPerPage";
88
import Sort from "../containers/Sort";
9+
import { create } from "../types/SortOption";
910

1011
export default function Body() {
1112
return (
1213
<div className="search-demo__body">
1314
<div className="search-results">
14-
<Sort />
15+
<Sort
16+
sortOptions={[
17+
create({
18+
name: "None",
19+
value: "",
20+
direction: ""
21+
}),
22+
create({
23+
name: "Name ASC",
24+
value: "name",
25+
direction: "asc"
26+
}),
27+
create({
28+
name: "Name DESC",
29+
value: "name",
30+
direction: "desc"
31+
}),
32+
create({
33+
name: "License ASC",
34+
value: "license",
35+
direction: "asc"
36+
}),
37+
create({
38+
name: "License DESC",
39+
value: "license",
40+
direction: "desc"
41+
})
42+
]}
43+
/>
1544
<Facets />
1645
<div className="results">
1746
<div className="results__header">

src/components/Sort.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ function Sort({ onChange, options, value }) {
55
return (
66
<div className="sort">
77
<select name="sort" value={value} onChange={onChange}>
8-
<option value="">None</option>
98
{options.map(option => (
109
<option key={option.value} value={option.value}>
1110
{option.name}

src/config/engine.json.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@
1212
"titleField": "title",
1313
"urlField": "url",
1414
"urlFieldTemplate": "http://www.example.com/{{id}}",
15+
"sortFields": [
16+
"title",
17+
"anotherField"
18+
]
1519
"facets": ["anotherField", "someField"]
1620
}

src/containers/Sort.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,41 @@ import React, { Component } from "react";
33

44
import withAppSearch from "../app-search/withAppSearch";
55
import Sort from "../components/Sort";
6+
import SortOption from "../types/SortOption";
67

7-
function serialize(sortObject) {
8-
const keys = Object.keys(sortObject);
9-
if (keys.length === 0) return "";
10-
return `${[keys[0]]}|||${sortObject[[keys[0]]]}`;
8+
function findSortOption(sortOptions, sortString) {
9+
const [value, direction] = sortString.split("|||");
10+
return sortOptions.find(
11+
option => option.value === value && option.direction === direction
12+
);
1113
}
1214

13-
function deSerialize(sortString) {
14-
if (!sortString) return {};
15-
const [sortBy, sortDirection] = sortString.split("|||");
15+
function formatOption(sortOption) {
1616
return {
17-
[sortBy]: sortDirection
17+
name: sortOption.name,
18+
value: `${sortOption.value}|||${sortOption.direction}`
1819
};
1920
}
20-
2121
class SortContainer extends Component {
2222
static propTypes = {
23+
// Injected
2324
setSort: PropTypes.func.isRequired,
2425
sort: PropTypes.shape({ name: PropTypes.string, value: PropTypes.string })
25-
.isRequired
26+
.isRequired,
27+
// Passed
28+
sortOptions: PropTypes.arrayOf(SortOption).isRequired
2629
};
2730

2831
render() {
29-
const { setSort, sort } = this.props;
32+
const { setSort, sort, sortOptions } = this.props;
3033

3134
return (
3235
<Sort
3336
onChange={e => {
34-
setSort(deSerialize(e.currentTarget.value));
37+
setSort(findSortOption(sortOptions, e.currentTarget.value));
3538
}}
36-
options={[
37-
{ name: "Name ASC", value: "name|||asc" },
38-
{ name: "Name DESC", value: "name|||desc" },
39-
{ name: "License ASC", value: "license|||asc" },
40-
{ name: "License DESC", value: "license|||desc" }
41-
]}
42-
value={serialize(sort)}
39+
options={sortOptions.map(formatOption)}
40+
value={formatOption(sort).value}
4341
/>
4442
);
4543
}

src/types/SortOption.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import PropTypes from "prop-types";
2+
3+
export default PropTypes.shape({
4+
// A display name, like "Name"
5+
name: PropTypes.string,
6+
// A field name, like "name".
7+
value: PropTypes.string,
8+
// asc or desc
9+
direction: PropTypes.oneOf(["asc", "desc", ""])
10+
});
11+
12+
export function create({ name, value, direction }) {
13+
return {
14+
name,
15+
value,
16+
direction
17+
};
18+
}

0 commit comments

Comments
 (0)