@@ -2,14 +2,75 @@ import React, { Component } from "react";
2
2
import { withStyles } from "@material-ui/styles" ;
3
3
import styles from "../styles/HelpStyles" ;
4
4
import Form from "./Form" ;
5
+ import axios from "axios" ;
5
6
6
7
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
+
7
68
render ( ) {
8
69
return (
9
70
< div >
10
71
< h1 > Help Page</ h1 >
11
72
< p > For Help Please Contact</ p >
12
- < Form />
73
+ < Form handleQuery = { this . handleQuery } />
13
74
</ div >
14
75
) ;
15
76
}
0 commit comments