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

AutoComplete for search bar (#251) #287

Open
wants to merge 1 commit into
base: latest
Choose a base branch
from
Open
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
116 changes: 81 additions & 35 deletions Origami/src/components/home/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ import {
Col,
Input,
Select,
Modal
Modal,
AutoComplete
} from "antd";
import toastr from "toastr";
import { SocialDialog } from "../social/SocialDialog";
import { trimAndPad } from "../../utils/generalUtils";
import { DEMO_CARD_DESCRIP_MAX_LEN } from "../../constants";
import { selectUser } from "../../actions/user_profile_action";
import userApi from "../../api/Github/userApi";
import Search from "antd/lib/input/Search";

const { Header, Content, Footer } = Layout;
const Option = Select.Option;
Expand All @@ -53,14 +55,17 @@ class HomePage extends React.Component {
is_cloudcv: false,
rootData: {},
allDeployed: [],
showDeployed: [],

demoBeingShown: {},
permalinkHolder: {},
shareModalOpen: false,
searchBy: "demo",
demoLoading: true,
logged: false,
profile: {},
loaded: false
loaded: false,
dataSource: []
};

this.handleShareModal = this.handleShareModal.bind(this);
Expand All @@ -76,6 +81,7 @@ class HomePage extends React.Component {
.then(alldeployedRepos => {
let tmp = JSON.parse(alldeployedRepos);
let allDeployed = [];

let profile = {};
for (let i = 0; i < tmp.length; i++) {
let uname = tmp[i].username;
Expand All @@ -89,8 +95,8 @@ class HomePage extends React.Component {
while (tmp.length) {
allDeployed.push(tmp.splice(0, 3));
}
this.setState({ allDeployed });
this.setState({ demoLoading: false, profile });
this.setState({ allDeployed, showDeployed: allDeployed });
})
.then(() => {
const stateToPut = {};
Expand Down Expand Up @@ -146,38 +152,75 @@ class HomePage extends React.Component {
}

findDemo(search_term) {
getSearchedDemos(this.state.searchBy, search_term)
.then(allRepos => {
if (Object.keys(JSON.parse(allRepos)).length > 0) {
let tmp = JSON.parse(allRepos);
let allDeployed = [];
while (tmp.length) {
allDeployed.push(tmp.splice(0, 3));
this.setState({ dataSource: [] });

if (search_term === "") {
this.setState({ showDeployed: this.state.allDeployed });
return;
}
let placeHolder = [];
if (this.state.searchBy === "demo") {
this.state.allDeployed.map(row => {
row.map(demo => {
let res = demo.name.search(search_term);

if (res !== -1 && search_term !== "") {
if (placeHolder.includes(demo.name)) {
placeHolder.push(demo.name + " ~by " + demo.username);
} else {
placeHolder.push(demo.name);
}
this.setState({ dataSource: placeHolder });
}
this.setState({
allDeployed
});
} else {
this.setState({ allDeployed: [] });
}
})
.then(() => {
const stateToPut = {};
getAllPermalink().then(data => {
JSON.parse(data).map(perma => {
if (!stateToPut[perma.user_id]) {
stateToPut[perma.user_id] = {};
});
});
} else if (this.state.searchBy === "user") {
this.state.allDeployed.map(row => {
row.map(demo => {
let res = demo.username.search(search_term);

if (res !== -1 && search_term !== "") {
if (placeHolder.includes(demo.username) === false) {
placeHolder.push(demo.username);
this.setState({ dataSource: placeHolder });
}
stateToPut[perma.project_id] = perma;
this.setState({
permalinkHolder: Object.assign({}, stateToPut)
});
});
}
});
})
.catch(err => {
toastr.error(err);
});
}
}

showQueryDemos(value) {
let by_user;
let selectedQuery;
if (value.includes("~by")) {
selectedQuery = value.slice(0, value.indexOf("~"));
by_user = value.slice(value.indexOf("~") + 4);
} else {
selectedQuery = value;
}

let filteredDemos = [];
let finalFliteredList = [];

this.state.allDeployed.map(row => {
row.map(demo => {
if (this.state.searchBy === "demo") {
if (demo.name === selectedQuery) {
filteredDemos.push(demo);
}
} else {
if (demo.username === selectedQuery) {
filteredDemos.push(demo);
}
}
});
});

while (filteredDemos.length) {
finalFliteredList.push(filteredDemos.splice(0, 3));
}
this.setState({ showDeployed: finalFliteredList });
}

handleClick(e) {
Expand Down Expand Up @@ -211,10 +254,13 @@ class HomePage extends React.Component {
<h2 id="logo-title">Origami</h2>
</Col>
<Col span={12} offset={3}>
<Input.Search
<AutoComplete
dataSource={this.state.dataSource}
style={{ width: "100%" }}
id="search"
placeholder="Search for demos, users"
placeholder={`Search for ${this.state.searchBy}s`}
onSearch={value => this.findDemo(value)}
onSelect={value => this.showQueryDemos(value)}
/>
</Col>
<Col span={3} offset={0}>
Expand Down Expand Up @@ -246,8 +292,8 @@ class HomePage extends React.Component {
</div>
) : (
<Row>
{Object.keys(this.state.allDeployed).length > 0 ? (
this.state.allDeployed.map(row => (
{Object.keys(this.state.showDeployed).length > 0 ? (
this.state.showDeployed.map(row => (
<div key={Math.random()}>
<Row>
{row.map(demo => (
Expand Down