Skip to content

Commit 9b73b27

Browse files
Add resouces dta
1 parent 4e55e85 commit 9b73b27

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

src/components/Form.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class Form extends Component {
55
super(props);
66

77
this.state = {
8-
area: "",
8+
location: "",
99
};
1010
this.handleChange = this.handleChange.bind(this);
1111
this.handleSubmit = this.handleSubmit.bind(this);
@@ -17,18 +17,20 @@ export default class Form extends Component {
1717

1818
handleSubmit(e) {
1919
e.preventDefault();
20-
this.setState({ area: "" });
20+
this.props.handleQuery(this.state.location);
21+
this.setState({ location: "" });
2122
}
2223

2324
render() {
2425
return (
2526
<form onSubmit={this.handleSubmit}>
2627
<input
27-
name="area"
28+
name="location"
2829
type="text"
29-
value={this.state.area}
30-
placeholder="Enter any area"
30+
value={this.state.location}
31+
placeholder="Enter any location"
3132
onChange={this.handleChange}
33+
required
3234
/>
3335
<button>Get Help</button>
3436
</form>

src/components/Help.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,75 @@ import React, { Component } from "react";
22
import { withStyles } from "@material-ui/styles";
33
import styles from "../styles/HelpStyles";
44
import Form from "./Form";
5+
import axios from "axios";
56

67
class Help extends Component {
8+
constructor(props) {
9+
super(props);
10+
this.state = {
11+
query: "",
12+
data: {},
13+
};
14+
this.handleQuery = this.handleQuery.bind(this);
15+
this.getData = this.getData.bind(this);
16+
}
17+
18+
componentDidMount() {
19+
this.fetchResources();
20+
}
21+
22+
async fetchResources() {
23+
try {
24+
const [response] = await Promise.all([
25+
axios.get("https://api.covid19india.org/resources/resources.json"),
26+
]);
27+
const hashmap = {};
28+
response.data.resources.forEach((x) => {
29+
if (typeof hashmap[x["state"]] === "undefined")
30+
hashmap[x["state"]] = {};
31+
if (typeof hashmap[x["state"]][x["city"]] === "undefined")
32+
hashmap[x["state"]][x["city"]] = {};
33+
34+
if (
35+
typeof hashmap[x["state"]][x["city"]][x["category"]] === "undefined"
36+
)
37+
hashmap[x["state"]][x["city"]][x["category"]] = [];
38+
if (Array.isArray(hashmap[x["state"]][x["city"]][x["category"]]))
39+
hashmap[x["state"]][x["city"]][x["category"]].push(x);
40+
});
41+
this.setState({ data: hashmap });
42+
} catch (err) {}
43+
}
44+
45+
handleQuery(query) {
46+
this.setState({ query: query }, this.getData);
47+
}
48+
49+
getData() {
50+
let resources = {};
51+
let locationType;
52+
for (const key of Object.keys(this.state.data)) {
53+
if (key === this.state.query) {
54+
resources = this.state.data[key];
55+
locationType = "multiple";
56+
} else {
57+
for (const dist of Object.keys(this.state.data[key])) {
58+
if (dist === this.state.query) {
59+
resources = this.state.data[key][dist];
60+
locationType = "single";
61+
}
62+
}
63+
}
64+
}
65+
this.setState({ currentResources: resources, locationType: locationType });
66+
}
67+
768
render() {
869
return (
970
<div>
1071
<h1>Help Page</h1>
1172
<p>For Help Please Contact</p>
12-
<Form />
73+
<Form handleQuery={this.handleQuery} />
1374
</div>
1475
);
1576
}

0 commit comments

Comments
 (0)