Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

add search field (#177)

Guilherme Oenning037e84e38adb
diff --git a/public/components/IdeaFilter.tsx b/public/components/IdeaFilter.tsx
index d3d7ea10b..b542ddeae 100644
--- a/public/components/IdeaFilter.tsx
+++ b/public/components/IdeaFilter.tsx
@@ -5,7 +5,7 @@ export type IdeaFilterFunction = (ideas: Idea[]) => Idea[];
interface IdeaFilterProps {
activeFilter: string;
- filterChanged: (name: string, filter: IdeaFilterFunction) => void;
+ filterChanged: (name: string) => void;
}
const filterers: {[key: string]: (idea: Idea) => boolean} = {
@@ -50,7 +50,7 @@ export class IdeaFilter extends React.Component<IdeaFilterProps, {}> {
public componentDidMount() {
$(this.element).dropdown({
onChange: (value: string) => {
- this.props.filterChanged(value, IdeaFilter.getFilter(value));
+ this.props.filterChanged(value);
}
});
}
diff --git a/public/pages/site/CompleteSignInProfilePage.tsx b/public/pages/site/CompleteSignInProfilePage.tsx
index de1e0709c..8258b8326 100644
--- a/public/pages/site/CompleteSignInProfilePage.tsx
+++ b/public/pages/site/CompleteSignInProfilePage.tsx
@@ -30,7 +30,7 @@ export class CompleteSignInProfilePage extends React.Component<{}, CompleteSignI
private async submit() {
const key = getQueryString('k');
- const result = await this.service.completeProfile(key!, this.state.name);
+ const result = await this.service.completeProfile(key, this.state.name);
if (result.ok) {
location.href = '/';
} else if (result.error) {
diff --git a/public/pages/site/SiteHomePage.scss b/public/pages/site/SiteHomePage.scss
index bf13521fc..bfe220c57 100644
--- a/public/pages/site/SiteHomePage.scss
+++ b/public/pages/site/SiteHomePage.scss
@@ -29,6 +29,15 @@
}
}
+ .filter-column {
+ display: flex;
+ align-items: center;
+ }
+
+ .no-ideas-found {
+ margin-top: 10px;
+ }
+
#new-idea-input {
margin-bottom: 20px;
border: none;
diff --git a/public/pages/site/SiteHomePage.tsx b/public/pages/site/SiteHomePage.tsx
index b76227f39..8e0b93d53 100644
--- a/public/pages/site/SiteHomePage.tsx
+++ b/public/pages/site/SiteHomePage.tsx
@@ -8,6 +8,7 @@ import { IdeaFilter, IdeaFilterFunction } from '@fider/components/IdeaFilter';
import { inject, injectables } from '@fider/di';
import { Session } from '@fider/services';
+import { getBaseUrl, getQueryString } from '@fider/utils/page';
import './SiteHomePage.scss';
@@ -16,8 +17,18 @@ const defaultShowCount = 20;
interface SiteHomePageState {
ideas: Idea[];
showCount: number;
+ searching: boolean;
+ search?: string;
+ activeFilter: string;
}
+const EmptyList = () => {
+ return <div className="center">
+ <p><i className="icon lightbulb" aria-hidden="true"></i></p>
+ <p>It's lonely out here. Start by sharing an idea!</p>
+ </div>;
+};
+
const ListIdeaItem = (props: { idea: Idea, user?: CurrentUser }) => {
return <div className="item">
<SupportCounter user={props.user} idea={props.idea} />
@@ -39,7 +50,6 @@ export class SiteHomePage extends React.Component<{}, SiteHomePageState> {
private tenant: Tenant;
private allIdeas: Idea[];
private filter: HTMLDivElement;
- private activeFilter: string;
@inject(injectables.Session)
public session: Session;
@@ -50,18 +60,57 @@ export class SiteHomePage extends React.Component<{}, SiteHomePageState> {
this.tenant = this.session.getCurrentTenant();
this.allIdeas = this.session.get<Idea[]>('ideas') || [];
- this.activeFilter = window.location.hash.substring(1);
+ const search = getQueryString('q');
+ const activeFilter = window.location.hash.substring(1);
this.state = {
- ideas: IdeaFilter.getFilter(this.activeFilter)(this.allIdeas),
- showCount: defaultShowCount
+ ideas: this.filterIdeas(activeFilter, search),
+ showCount: defaultShowCount,
+ activeFilter,
+ searching: !!search,
+ search
};
}
- private filterChanged(name: string, filter: IdeaFilterFunction) {
- window.location.hash = `#${name}`;
+ private containsAll(str: string, substrings: string[]): boolean {
+ for (let i = 0; i !== substrings.length; i++) {
+ if (str.indexOf(substrings[i]) === - 1) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private filterIdeas(activeFilter: string, search?: string): Idea[] {
+ let newUrl = getBaseUrl();
+ let ideas = [];
+
+ if (search) {
+ const s = search.trim().toLowerCase();
+ newUrl += `?q=${encodeURIComponent(s).replace(/%20/g, '+')}`;
+ ideas = this.allIdeas.filter((idea) => {
+ const terms = s.split(' ').filter((x) => x.length >= 2);
+ return (
+ this.containsAll(idea.title.toLowerCase(), terms) ||
+ this.containsAll(idea.description.toLowerCase(), terms) ||
+ (idea.response && this.containsAll(idea.response.text.toLowerCase(), terms))
+ );
+ });
+ } else {
+ newUrl += activeFilter ? `#${activeFilter}` : '';
+ ideas = IdeaFilter.getFilter(activeFilter)(this.allIdeas);
+ }
+
+ if (history.replaceState) {
+ window.history.replaceState({ path: newUrl }, '', newUrl);
+ }
+ return ideas;
+ }
+
+ private filterChanged(name: string) {
this.setState({
- ideas: filter(this.allIdeas),
- showCount: defaultShowCount
+ ideas: this.filterIdeas(name, this.state.search),
+ showCount: defaultShowCount,
+ activeFilter: name,
});
}
@@ -72,6 +121,21 @@ export class SiteHomePage extends React.Component<{}, SiteHomePageState> {
});
}
+ private resetSearch() {
+ this.setState({
+ search: '',
+ searching: false,
+ ideas: this.filterIdeas(this.state.activeFilter, ''),
+ });
+ }
+
+ private searchIdea(input: string): void {
+ this.setState({
+ search: input,
+ ideas: this.filterIdeas(this.state.activeFilter, input)
+ });
+ }
+
public render() {
const ideasToList = this.state.ideas.slice(0, this.state.showCount);
@@ -87,7 +151,7 @@ export class SiteHomePage extends React.Component<{}, SiteHomePageState> {
</h5>
}
</div>
- : <p>No ideas found for given filter.</p>;
+ : <p className="no-ideas-found">No ideas found for given filter.</p>;
const welcomeMessage = this.tenant.welcomeMessage ||
`## Welcome to our feedback forum!
@@ -106,12 +170,31 @@ We'd love to hear what you're thinking about. What can we do better? This is the
<div className="ten wide column">
{
this.allIdeas.length === 0
- ? <div className="center">
- <p><i className="icon lightbulb" aria-hidden="true"></i></p>
- <p>It's lonely out here. Start by sharing an idea!</p>
- </div>
+ ? <EmptyList />
: <div>
- <IdeaFilter activeFilter={ this.activeFilter } filterChanged={ this.filterChanged.bind(this) } />
+ <div className="ui grid">
+ { !this.state.searching && <div className="ten wide mobile ten wide tablet twelve wide computer column filter-column">
+ <IdeaFilter activeFilter={ this.state.activeFilter } filterChanged={ this.filterChanged.bind(this) } />
+ </div> }
+ <div className={ !this.state.searching ? `six wide mobile six wide tablet four wide computer column` : 'column' }>
+ <div className="ui search">
+ <div className="ui icon fluid input">
+ <input
+ onFocus={ () => this.setState({ searching: true }) }
+ onBlur={ (x) => this.setState({ searching: !!this.state.search }) }
+ onChange={ (x) => this.searchIdea(x.currentTarget.value) }
+ value={ this.state.search }
+ type="text"
+ placeholder="Search..." />
+ {
+ this.state.searching
+ ? <i onClick={ () => this.resetSearch() } className="cancel link icon"></i>
+ : <i className="search icon"></i>
+ }
+ </div>
+ </div>
+ </div>
+ </div>
{ displayIdeas }
</div>
}
diff --git a/public/utils/page.ts b/public/utils/page.ts
index 427ca34d1..f86ede980 100644
--- a/public/utils/page.ts
+++ b/public/utils/page.ts
@@ -28,17 +28,13 @@ export function hideSignIn(): void {
hideModal('#signin-modal');
}
-export function getQueryString(name: string) {
+export function getQueryString(name: string): string {
const url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
const results = regex.exec(url);
- if (!results) {
- return null;
- }
-
- if (!results[2]) {
+ if (!results || !results[2]) {
return '';
}