Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sprint#23/search history #493

Merged
merged 5 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.8.1] - 2018-XX-XX
### Added
- [Search] History

## [0.8.0] - 2018-06-29
### Added
- Push Notifications
Expand Down
7 changes: 7 additions & 0 deletions src/graphql/mutations/local/searchHistoryAdd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import gql from "graphql-tag";

export default gql`
mutation searchHistoryAdd($term: String!) {
searchHistoryAdd(term: $term) @client
}
`;
9 changes: 9 additions & 0 deletions src/graphql/queries/local/searchHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import gql from "graphql-tag";

export default gql`
query {
searchHistory @client {
term
}
}
`;
37 changes: 36 additions & 1 deletion src/graphql/resolvers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import VOTES_LOCAL from "../queries/votesLocal";
import IS_INSTRUCTIONS_SHOWN from "../queries/isInstructionShown";
import GET_NETWORK_STATUS from "../queries/getNetworkStatus";
import SEARCH_HISTORY from "../queries/local/searchHistory";

import ViewedProcedures from "../../services/ViewedProcedures";

Expand All @@ -16,7 +17,8 @@ export const defaults = {
searchTerm: {
__typename: "SearchTerm",
term: ""
}
},
searchHistory: []
};

export const resolvers = {
Expand Down Expand Up @@ -88,6 +90,39 @@ export const resolvers = {
term
}
};
cache.writeData({ data });
return null;
},
searchHistoryAdd: (_, { term }, { cache }) => {
let previous = cache.readQuery({ query: SEARCH_HISTORY });

if (!previous) {
previous = { searchHistory: [] };
}

const index = previous.searchHistory.findIndex(
({ term: t }) => t === term
);

let data;

if (index === -1) {
data = {
searchHistory: [
{ term, __typename: "SearchHistoryTerm" },
...previous.searchHistory
].slice(0, 3)
};
} else {
previous.searchHistory.splice(index, 1);
data = {
searchHistory: [
{ term, __typename: "SearchHistoryTerm" },
...previous.searchHistory
].slice(0, 3)
};
}

cache.writeData({ data });
return null;
}
Expand Down
5 changes: 5 additions & 0 deletions src/graphql/schemas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ export default `
status: String!
}

type SearchTerm {
term: String!
}

type Mutation {
voteLocal(procedure: ID!, selection: VoteSelection!): VoteSelection
viewProcedure(procedureId: String!, status: String!): ListStatus
searchHistoryAdd(term: String!): [SearchTerm]
}

type Query {
Expand Down
7 changes: 7 additions & 0 deletions src/screens/Search/Header.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { graphql, compose } from "react-apollo";
import client from "../../graphql/client";

import finishSearch from "../../graphql/mutations/finishSearch";
import SEARCH_HISTORY_ADD from "../../graphql/mutations/local/searchHistoryAdd";
import searchTerm from "../../graphql/queries/local/searchTerm";
import changeSearchTerm from "../../graphql/mutations/local/changeSearchTerm";

Expand Down Expand Up @@ -85,6 +86,12 @@ class Header extends Component {
term: this.state.term
}
});
client.mutate({
mutation: SEARCH_HISTORY_ADD,
variables: {
term: this.state.term
}
});
};

render() {
Expand Down
7 changes: 7 additions & 0 deletions src/screens/Search/Header.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { graphql, compose } from "react-apollo";
import client from "../../graphql/client";

import finishSearch from "../../graphql/mutations/finishSearch";
import SEARCH_HISTORY_ADD from "../../graphql/mutations/local/searchHistoryAdd";
import searchTerm from "../../graphql/queries/local/searchTerm";
import changeSearchTerm from "../../graphql/mutations/local/changeSearchTerm";

Expand Down Expand Up @@ -88,6 +89,12 @@ class Header extends Component {
term: this.state.term
}
});
client.mutate({
mutation: SEARCH_HISTORY_ADD,
variables: {
term: this.state.term
}
});
};

render() {
Expand Down
33 changes: 28 additions & 5 deletions src/screens/Search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import client from "../../graphql/client";

import searchProcedures from "../../graphql/queries/searchProcedures";
import mostSearched from "../../graphql/queries/mostSearched";
import finishSearch from "../../graphql/mutations/finishSearch";
import searchTerm from "../../graphql/queries/local/searchTerm";
import SEARCH_HISTORY from "../../graphql/queries/local/searchHistory";
import finishSearch from "../../graphql/mutations/finishSearch";
import changeSearchTerm from "../../graphql/mutations/local/changeSearchTerm";
import SEARCH_HISTORY_ADD from "../../graphql/mutations/local/searchHistoryAdd";

import preventNavStackDuplicate from "../../hocs/preventNavStackDuplicate";

Expand Down Expand Up @@ -143,6 +145,11 @@ class SearchScreen extends Component {
term: item
}
});
this.props.addToSearchHistory({
variables: {
term: item
}
});
this.onChangeTerm(item);
}
};
Expand All @@ -166,7 +173,7 @@ class SearchScreen extends Component {

render() {
const { loading } = this.state;
const { mostSearchedTerms, searchTerm: term } = this.props;
const { mostSearchedTerms, searchTerm: term, searchHistory } = this.props;

if (loading) {
return (
Expand All @@ -179,6 +186,10 @@ class SearchScreen extends Component {
let sectionData = [];
if (!term) {
sectionData = [
{
title: "Zuletzt gesucht",
data: searchHistory
},
{
title: "Meistgesucht",
data: mostSearchedTerms
Expand Down Expand Up @@ -206,6 +217,7 @@ class SearchScreen extends Component {
{title === "Ergebnisse" && (
<VoteListItem {...item} date={item.voteDate} />
)}
{title === "Zuletzt gesucht" && <ListText>{item}</ListText>}
{title === "Vorschläge" && <ListText>{item}</ListText>}
{title === "Meistgesucht" && <ListText>{item}</ListText>}
</ListRow>
Expand Down Expand Up @@ -236,12 +248,15 @@ SearchScreen.propTypes = {
finishSearch: PropTypes.func.isRequired,
updateSearchTerm: PropTypes.func.isRequired,
mostSearchedTerms: PropTypes.arrayOf(PropTypes.shape()),
searchTerm: PropTypes.string.isRequired
searchTerm: PropTypes.string.isRequired,
addToSearchHistory: PropTypes.func.isRequired,
searchHistory: PropTypes.arrayOf(PropTypes.string )
};

SearchScreen.defaultProps = {
navigator: undefined,
mostSearchedTerms: []
mostSearchedTerms: [],
searchHistory: []
};

export default withApollo(
Expand Down Expand Up @@ -269,10 +284,18 @@ export default withApollo(
? { searchTerm: searchTermData.term }
: { searchTerm: "" }
}),
graphql(SEARCH_HISTORY, {
props: ({ data: { searchHistory } }) => ({
searchHistory: searchHistory
? searchHistory.map(({ term }) => term)
: []
})
}),

// Mutations
graphql(finishSearch, { name: "finishSearch" }),
graphql(changeSearchTerm, { name: "updateSearchTerm" })
graphql(changeSearchTerm, { name: "updateSearchTerm" }),
graphql(SEARCH_HISTORY_ADD, { name: "addToSearchHistory" })
)(SearchScreen)
)
);