/
Testimonial.js
235 lines (198 loc) · 5.18 KB
/
Testimonial.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import React, { Component } from 'react';
import { string, number, shape, func, array } from 'prop-types'
import '../Styles/style.css';
import {API_URL} from '../constants'
import axios from 'axios';
class CarouselIndicator extends Component {
render() {
return (
<li>
<a
className={
this.props.index === this.props.activeIndex
? 'carousel__indicator carousel__indicator--active'
: 'carousel__indicator'
}
onClick={this.props.onClick}
/>
</li>
);
}
}
class CarouselSlide extends Component {
render() {
return (
<li
className={
this.props.index === this.props.activeIndex
? 'carousel__slide carousel__slide--active '
: 'carousel__slide'
}
>
<div className="carousel-slide__content">
{this.props.slide.content}
</div>
<div className="author-source-container">
<small className="carousel-slide__source">
<div>
<strong className="carousel-slide__author">
{this.props.slide.author}
</strong>
</div>
{this.props.slide.source}
</small>
</div>
</li>
);
}
}
class CarouselLeftArrow extends Component {
render() {
return (
<a
href="#"
className="carousel__arrow carousel__arrow--left padding-on-left"
onClick={this.props.onClick}
>
<i className="fa fa-2x fa-angle-left" />
</a>
);
}
}
class CarouselRightArrow extends Component {
render() {
return (
<a
href="#"
className="carousel__arrow carousel__arrow--right padding-on-right"
onClick={this.props.onClick}
>
<i className="fa fa-2x fa-angle-right" />
</a>
);
}
}
// Carousel wrapper component
class Testimonial extends Component {
constructor(props) {
super(props);
this.goToSlide = this.goToSlide.bind(this);
this.goToPrevSlide = this.goToPrevSlide.bind(this);
this.goToNextSlide = this.goToNextSlide.bind(this);
this.timer = this.timer.bind(this);
this.getData = this.getData.bind(this);
this.state = {
activeIndex: 0,
};
}
componentWillMount(){
this.getData()
}
getData(){
this.props.get_testimonial_data()
}
componentDidMount() {
this.getData()
const interval = setInterval(this.timer, 4000);
}
componentWillUnmount() {
clearInterval(this.state.interval);
}
timer() {
// const {data} = this.props.state.TestimonialReducer[0];
if (this.state.activeIndex === this.props.state.TestimonialReducer[0].data.length - 1) {
this.setState({ activeIndex: -1 });
}
this.setState({ activeIndex: this.state.activeIndex + 1 });
}
goToSlide(index) {
this.setState({
activeIndex: index,
});
}
goToPrevSlide(e) {
e.preventDefault();
let index = this.state.activeIndex;
const slides = this.props.state.TestimonialReducer[0].data
const slidesLength = slides.length;
if (index < 1) {
index = slidesLength;
}
--index;
this.setState({
activeIndex: index,
});
}
goToNextSlide(e) {
e.preventDefault();
let index = this.state.activeIndex;
const slides = this.props.state.TestimonialReducer[0].data;
const slidesLength = slides.length - 1;
if (index === slidesLength) {
index = -1;
}
++index;
this.setState({
activeIndex: index,
});
}
render() {
const { activeIndex } = this.state;
const { goToPrevSlide, goToSlide, goToNextSlide } = this;
return (
<div className="carousel">
<div>
<CarouselLeftArrow onClick={e => goToPrevSlide(e)} />
</div>
<div>
<ul className="carousel__slides container">
{this.props.state.TestimonialReducer[0]?
this.props.state.TestimonialReducer[0].data.map((slide, index) => (
<CarouselSlide
key={index}
index={index}
activeIndex={activeIndex}
slide={slide}
/>
)): null}
</ul>
<ul className="carousel__indicators">
{this.props.state.TestimonialReducer[0]?
this.props.state.TestimonialReducer[0].data.map((slide, index) => (
<CarouselIndicator
key={index}
index={index}
activeIndex={activeIndex}
isActive={activeIndex === index}
onClick={e => goToSlide(index)}
/>
)): null}
</ul>
</div>
<div className="arrow-fix">
<CarouselRightArrow onClick={e => goToNextSlide(e)} />
</div>
</div>
);
}
}
CarouselIndicator.propTypes = {
index: number.isRequired,
activeIndex: number.isRequired
};
CarouselSlide.propTypes = {
index: number.isRequired,
activeIndex: number.isRequired,
slide: shape({
content: string,
author: string,
source: string
}).isRequired
};
CarouselLeftArrow.propTypes = {
onClick: func.isRequired
}
CarouselRightArrow.propTypes = {
onClick: func.isRequired
}
export default Testimonial;