Created
February 11, 2016 10:54
-
-
Save ppsirius/9ebbe32f76c15e86b840 to your computer and use it in GitHub Desktop.
giphy refresh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use strict"; | |
| import React from "react"; | |
| import Czas from 'absolvent/React/Czas' | |
| function padNumbers(number) { | |
| return String(100 + number).substr(1, 2); | |
| } | |
| export default class Timer extends React.Component { | |
| static propTypes = { | |
| additionalTime: React.PropTypes.number.isRequired, | |
| countdown: React.PropTypes.number.isRequired | |
| }; | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| timeLeft: props.countdown, | |
| additionalTime: props.additionalTime, | |
| pause: false, | |
| buttonName: "Pauza", | |
| img: "" | |
| }; | |
| this.myBind("handleClickButton", "handleAdditionalTime", "refresh"); | |
| } | |
| refresh() { | |
| let that = this; | |
| fetch('http://api.giphy.com/v1/gifs/random?q=funny+cat&api_key=dc6zaTOxFJmzC').then(function (response) { | |
| return response.json(); | |
| }).then(function (json) { | |
| console.log(json.data); | |
| that.setState({img:json.data.image_original_url}) | |
| }) | |
| } | |
| componentDidMount() { | |
| this.setDecrementCountdownInterval(); | |
| this.refresh(); | |
| } | |
| componentWillUnmount() { | |
| this.clearCountdownInterval(); | |
| } | |
| componentWillReceiveProps(nextProps) { | |
| this.setState({ | |
| pause: nextProps.pausee | |
| }); | |
| } | |
| // ES6 - manualnie przekazuje `this` do metody | |
| // rest parameter - tablica wartosci od 0 do methods.length (ES6) | |
| myBind(...methods) { | |
| methods.forEach((method) => this[method] = this[method].bind(this)); | |
| } | |
| setDecrementCountdownInterval() { | |
| this.decrementCountdownInterval = setInterval(() => { | |
| return this.decrementCountdown(); | |
| }, 1000); | |
| } | |
| clearCountdownInterval() { | |
| clearInterval(this.decrementCountdownInterval); | |
| } | |
| decrementCountdown() { | |
| if (this.state.timeLeft < 1) { | |
| this.clearCountdownInterval(); | |
| } else { | |
| this.setState({ | |
| timeLeft: this.state.timeLeft - 1 | |
| }); | |
| } | |
| } | |
| handleClickButton() { | |
| if (this.state.pause === false) { | |
| this.setState({ | |
| pause: true, | |
| buttonName: "Wznów" | |
| }); | |
| this.clearCountdownInterval(); | |
| } else { | |
| this.setState({ | |
| pause: false, | |
| buttonName: "Pauza" | |
| }); | |
| this.setDecrementCountdownInterval(); | |
| } | |
| } | |
| handleAdditionalTime() { | |
| this.setState({ | |
| timeLeft: this.state.timeLeft + this.state.additionalTime | |
| }); | |
| } | |
| render() { | |
| if (this.state.timeLeft < 1) { | |
| return ( | |
| <div style={{ | |
| backgroundColor: "#f55", | |
| color: "#fff" | |
| }}> | |
| time is up | |
| </div> | |
| ); | |
| } | |
| return ( | |
| <div> | |
| <button onClick={this.refresh}>Refresh</button> | |
| <br /> | |
| <img src={this.state.img} width="300"/> | |
| <p> | |
| {padNumbers(this.state.timeLeft / 3600)}: | |
| {padNumbers(this.state.timeLeft / 60)}: | |
| {padNumbers(this.state.timeLeft % 60)} | |
| </p> | |
| <Czas pausee={this.state.pause} /> | |
| </div> | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment