-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardContainer.js
More file actions
147 lines (145 loc) · 4.42 KB
/
Copy pathCardContainer.js
File metadata and controls
147 lines (145 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import React, { Component } from 'react';
import Card from "../../card/components/Card";
import Modal from 'react-responsive-modal';
import '../styles/style.css';
class CardContainer extends Component {
constructor() {
super();
this.state= {
open: false,
userContactDetails: null,
filterType: "all"
}
}
/**
* Sets state of the modal
* @param {Boolean} state modal state
*/
setModalState(state) {
this.setState({
open: state
});
}
/**
* Returns user contact details
*/
getAddressField(data, type) {
if(!data || data.length === 0) {
return null;
}
const modData = Array.isArray(data) ? data.join(", ") : data;
return(
<div>
<span><u>{type}: </u>{`${modData}`}</span><br/>
</div>
);
}
/**
* Returns modal with all the necessary contact information
*/
getUserDetailsModal() {
const { open, userContactDetails } = this.state;
if(!open) {
return null;
}
const { address, email, phone } = userContactDetails;
const { city, state, street, zip } = address;
return (
<Modal open={open} onClose={this.onCloseModal.bind(this)} little>
<h1>Contact Me!</h1>
<div className="detailsCont">
<address className="address">
{this.getAddressField(street, "Street")}
{this.getAddressField(city, "City")}
{this.getAddressField(state, "State")}
{this.getAddressField(zip, "Zip code")}
{this.getAddressField(email, "Email Address")}
{this.getAddressField(phone, "Phone number")}
</address>
</div>
</Modal>
);
}
/**
* Sets state on closing of modal
*/
onCloseModal() {
this.setState({
open: false,
userContactDetails: null
});
}
/**
* sets user infor in componnet state
* @param {Array} details user contact details
*/
setUserCOntactDetails(details) {
this.setState({
userContactDetails: details
});
}
/**
* Sets filter type info in state
* @param {String} state
*/
setFilter(state) {
this.setState({
filterType: state
})
}
/**
* Iterates over user object and returns component based on filter set
*/
getUserCards() {
const { allUsers } = this.props;
const { filterType } = this.state;
return(
allUsers.map((user, index) => {
if(user.gender !== filterType && filterType !== "all") {
return null;
}
return (
<Card
user={user}
key={index}
contactButtonClicked={(details) => {
this.setModalState(true);
this.setUserCOntactDetails(details);
}}
/>)
})
)
}
/**
* Changes the filter button class
* @param {String} type type of filter button
*/
getAdditionalClass(type) {
const { filterType } = this.state;
if(type === filterType) {
return "btn-selected";
}
return null;
}
/**
* Renders the component
*/
render() {
return (
<div className="container">
<div className="heading-cont">
<div className="filterParent">
<button className={`btn ${this.getAdditionalClass("all")}`}onClick={()=>{ this.setFilter("all") }}> Remove filter</button>
<button className={`btn ${this.getAdditionalClass("male")}`} onClick={()=>{ this.setFilter("male") }}> Male</button>
<button className={`btn ${this.getAdditionalClass("female")}`} onClick={()=>{ this.setFilter("female") }}> Female</button>
</div>
</div>
{this.getUserDetailsModal()}
<div className="card-grid">
{this.getUserCards()}
</div>
</div>
);
}
}
export default CardContainer;