npm install -g create-react-app
-g lo utilizamos para indicar que será una instalación global.
create-react-app pomodorito
cd pomodorito/
npm start
yarn start
cd src
mkdir components
cd components
mkdir Counter
Nuestro componente por estándar se define en CamelCase.
cd Counter
touch Counter.js
touch index.js
ReactJS cuando hace un import de un folder, busca un index.js
import React from 'react';
const Counter = () => {
return (
00:00
);
}
export default Counter;
import Counter from './Counter';
export default Counter;
import Counter from './components/Counter';
import Counter from './components/Counter';
...
render() {
return (
...
);
}
...
const Counter = (props) => {
return (
{props.minutes}:{props.seconds}
);
}
npm install prop-types --save
import PropTypes from 'prop-types';
Describimos nuestros props
Counter.propTypes = {
time: PropTypes.number.isRequired,
}
const Counter = ({minutes, seconds}) => {
return (
{minutes}:{seconds}
);
}
cd components
mkdir Pomodoro
Nuestro componente por estándar se define en CamelCase.
cd Pomodoro
touch Pomodoro.js
touch index.js
ReactJS cuando hace un import de un folder, busca un index.js
import React from 'react';
import Pomodoro from '../Pomodoro';
const Pomodoro = () => {
return (
{ /*...botones...*/ }
);
}
export default Pomodoro;
import Pomodoro from './Pomodoro';
export default Pomodoro;
import React, { Component } from 'react';
import Pomodoro from './components/Pomodoro';
...
class App extends Component {
render() {
return (
...
...
);
}
}
export default App;
npm install moment --save
--save es para que se guarde como dependencia en nuestro proyecto.
import React from 'react';
import moment from 'moment';
const Counter = ({time}) => {
return (
{moment(time).format('mm:ss')}
);
}
export default Counter;
El tiempo es en milisegundos.
import React from 'react';
import Counter from '../Counter';
class Pomodoro extends React.Component {
render() {
return (
{ /*botones*/ }
);
}
}
export default Pomodoro;
constructor(props) {
super(props);
this.state = {
intervalId: null, //ID for the interval to update the time
breaksCount: 0, //Count for each "time break"
currentBreak: 'pomodoro', //Type of current "time break"
}
}
setDuration(minutes) {
return moment.duration(minutes * 60, 'seconds');
}
La agregamos en nuestra clase Pomodoro.
No olvides importar el módulo de moment.
currentDuration: this.setDuration(props.pomodoro),
onClick={this.handleStartClick}
Y en nuestra clase agregamos el método
handleStartClick = () => {
const intervalId = setInterval( () => {
console.log('corriendo intervalo');
}, 1000);
this.setState({ intervalId });
}
timer = () => {
this.setState((state) => {
const seconds = state.currentDuration.asSeconds();
if (seconds <= 1) {
clearInterval(state.intervalId);
// Debemos cambiar al nuevo break y aumentar el contador de breaks
}
return {
currentDuration: moment.duration(seconds - 1, 'seconds')
};
});
}
Cuando retornas en la función setState el objeto se va a mergear con el state actual.
handleStartClick = () => {
const intervalId = setInterval( this.timer, 1000);
this.setState({ intervalId });
}
getNextTime = () => {
if (this.state.currentBreak === 'pomodoro') {
if (this.state.breaksCount === MAX_BREAKS) {
return 'longBreak';
}
return 'shortBreak';
}
return 'pomodoro';
}
finishTime = () => {
let breaksCount = this.state.breaksCount;
if (this.state.currentBreak === 'shortBreak') {
breaksCount += 1;
} else if (breaksCount === MAX_BREAKS) {
breaksCount = 0;
}
const nextTime = this.getNextTime();
return {
currentBreak: nextTime,
currentDuration: this.setDuration(this.props[nextTime]),
breaksCount,
};
}
return this.finishTime();
npm install push.js --save
handleTimerFinish = (finishedTime) => {
Push.create(`Terminó tu ${finishedTime}`, {
body: 'Da click aquí para ir al Pomodorito',
icon: 'icon.png',
timeout: 4000,
onClick: function() {
window.focus();
this.close();
}
});
}
y no olvides agregarlo a tus PropTypes
onTimerFinish: PropTypes.func,
this.props.onTimerFinish(this.state.currentBreak);
const styles = {
fontSize: "42px",
backgroundColor: "rgb(225, 225, 225)",
padding: "35px",
display: "inline-block",
margin: "40px",
}
{moment(time).format('mm:ss')}