Skip to content

Commit 996f036

Browse files
author
Edvinas Jurele
committed
Fix eslint errors
1 parent 256ec16 commit 996f036

File tree

13 files changed

+129
-79
lines changed

13 files changed

+129
-79
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ node_modules
33

44
# Project specific
55
/public
6+
/src/App.test.js
7+
/src/registerServiceWorker.js

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"react/default-props-match-prop-types": [0],
1717
"import/no-named-as-default": [0],
1818
"import/prefer-default-export": [0],
19-
"jsx-a11y/label-has-for": [0]
19+
"jsx-a11y/label-has-for": [0],
20+
"jsx-a11y/click-events-have-key-events": [0]
2021
}
2122
}

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@
1818
"build-js": "react-scripts build",
1919
"build": "npm-run-all build-css build-js",
2020
"test": "react-scripts test --env=jsdom",
21-
"eject": "react-scripts eject"
21+
"eject": "react-scripts eject",
22+
"lint": "eslint src"
2223
},
2324
"devDependencies": {
25+
"classnames": "^2.2.5",
2426
"eslint": "^4.19.1",
2527
"eslint-config-airbnb": "^16.1.0",
2628
"eslint-config-prettier": "^2.9.0",
2729
"eslint-plugin-import": "^2.10.0",
2830
"eslint-plugin-jsx-a11y": "^6.0.3",
2931
"eslint-plugin-prettier": "^2.6.0",
3032
"eslint-plugin-react": "^7.7.0",
31-
"prettier": "^1.11.1"
33+
"prettier": "^1.11.1",
34+
"prop-types": "^15.6.1",
35+
"react-required-if": "^1.0.3"
3236
}
3337
}

src/App.js

+48-34
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from './components';
1212
import { handleLottoInput } from './utils';
1313
import { DEMO_TICKETS } from './constants';
14+
1415
const STORAGE_KEY = 'appState';
1516

1617
class App extends Component {
@@ -39,41 +40,63 @@ class App extends Component {
3940
return initialState;
4041
};
4142

43+
componentDidMount() {
44+
if (window.localStorage) {
45+
const cache = JSON.parse(window.localStorage.getItem(STORAGE_KEY));
46+
if (cache != null) {
47+
this.setState({ ...cache, value: '' }); // eslint-disable-line react/no-did-mount-set-state
48+
}
49+
}
50+
window.addEventListener('beforeunload', this.componentUnmount);
51+
}
52+
53+
componentWillUnmount() {
54+
this.componentUnmount();
55+
}
56+
4257
getDemoTickets = () => {
4358
this.setState({ tickets: DEMO_TICKETS });
4459
this.disableEditMode();
4560
};
4661

47-
addTicket = () => {
48-
console.log('TODO: Add a ticket functionality');
62+
setStatus = status => {
63+
this.setState({ status });
64+
};
65+
66+
resetStatus = () => {
67+
this.setState({ status: this.getInitialState().status });
4968
};
5069

5170
resetState = () => {
5271
this.setState(this.getInitialState());
5372
};
5473

5574
removeLocalStorage = () => {
56-
localStorage.removeItem(STORAGE_KEY);
75+
if (window.localStorage) window.localStorage.removeItem(STORAGE_KEY);
5776
};
5877

5978
pushStateToStorage = () => {
6079
this.removeLocalStorage();
61-
localStorage.setItem(STORAGE_KEY, JSON.stringify(this.state));
80+
if (window.localStorage)
81+
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(this.state));
6282
};
6383

64-
setStatus = status => {
65-
this.setState({ status });
66-
};
67-
68-
resetStatus = () => {
69-
this.setState({ status: this.getInitialState().status });
84+
addTicket = () => {
85+
// eslint-disable-next-line no-console
86+
console.log('TODO: Add a ticket functionality');
7087
};
7188

7289
countOccurencies = number => {
7390
let occurenciesCount = 0;
7491
this.state.tickets.map(ticket => {
7592
const arr = ticket.values.toString().split(',');
76-
occurenciesCount += arr.reduce((a, v) => (v === number ? ++a : a), 0);
93+
occurenciesCount += arr.reduce((a, v) => {
94+
if (v === number) {
95+
const increment = a + 1;
96+
return increment;
97+
}
98+
return a;
99+
}, 0);
77100
return true;
78101
});
79102
return occurenciesCount;
@@ -104,7 +127,7 @@ class App extends Component {
104127

105128
handleEnteredNumberCrossout = e => {
106129
e.preventDefault();
107-
const value = this.state.value;
130+
const { value } = this.state;
108131
if (
109132
value.length >= 1 &&
110133
value >= 1 &&
@@ -118,6 +141,7 @@ class App extends Component {
118141

119142
invalidateCache = () => {
120143
if (
144+
// eslint-disable-next-line no-alert
121145
window.confirm('Ar tikrai? Bus ištrinti bilietai ir kamuoliukų istorija.')
122146
) {
123147
this.removeLocalStorage();
@@ -127,6 +151,7 @@ class App extends Component {
127151

128152
removeLastRolledValue = () => {
129153
if (
154+
// eslint-disable-next-line no-alert
130155
window.confirm('Ar tikrai norite pašalinti paskutinį ridentą kamuoliuką?')
131156
) {
132157
this.setState({ rolledValues: this.state.rolledValues.slice(0, -1) });
@@ -163,12 +188,15 @@ class App extends Component {
163188
};
164189

165190
handleEditModeChange = () => {
166-
this.state.options.isEditMode
167-
? this.disableEditMode()
168-
: this.enableEditMode();
191+
if (this.state.options.isEditMode) {
192+
this.disableEditMode();
193+
} else {
194+
this.enableEditMode();
195+
}
169196
};
170197

171198
removeTicketByIndex = id => {
199+
// eslint-disable-next-line no-alert
172200
if (window.confirm('Ar tikrai? Bus ištrintas pasirinktas bilietas.')) {
173201
this.setState({
174202
tickets: this.state.tickets.filter((item, index) => index !== id),
@@ -181,18 +209,6 @@ class App extends Component {
181209
window.removeEventListener('beforeunload', this.componentUnmount);
182210
};
183211

184-
componentWillMount() {
185-
var cache = JSON.parse(localStorage.getItem(STORAGE_KEY));
186-
if (cache != null) {
187-
this.setState({ ...cache, value: '' });
188-
}
189-
window.addEventListener('beforeunload', this.componentUnmount);
190-
}
191-
192-
componentWillUnmount() {
193-
this.componentUnmount();
194-
}
195-
196212
openCreateTicketModal = () => {
197213
this.setState({ modalCreateTicketOpen: true });
198214
};
@@ -260,10 +276,10 @@ class App extends Component {
260276
{rolledValues.length ? (
261277
<div className="d-flex align-items-center w-100">
262278
<div style={{ flex: 1 }}>
263-
{rolledValues.map((value, index) => (
279+
{rolledValues.map((rolledValue, index) => (
264280
<Sphere
265281
key={index}
266-
value={value}
282+
value={rolledValue}
267283
className="d-inline-block mr-1 my-1"
268284
/>
269285
))}
@@ -288,16 +304,14 @@ class App extends Component {
288304
{tickets.map((ticket, index) => (
289305
<div key={index} className="col-12 col-md-6 col-lg-4">
290306
<Ticket
291-
index={index}
307+
ticketIndex={index}
292308
onTicketRemove={
293-
isEditMode
294-
? value => this.removeTicketByIndex(value)
295-
: undefined
309+
isEditMode ? this.removeTicketByIndex : undefined
296310
}
297311
className="d-inline-block text-center my-2"
298312
rolledValues={this.state.rolledValues}
299313
isClickable={isTicketsClickable}
300-
clickHandler={value => this.crossOutNumber(value)}
314+
clickHandler={this.crossOutNumber}
301315
// isEditable
302316
{...ticket}
303317
/>

src/components/EditableInput/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ EditableInput.propTypes = {
2424
placeholder: PropTypes.string,
2525
id: PropTypes.string.isRequired,
2626
onChange: PropTypes.func.isRequired,
27+
isTransparent: PropTypes.bool,
2728
};
2829

2930
EditableInput.defaultProps = {
3031
placeholder: undefined,
32+
isTransparent: undefined,
3133
};
3234

3335
export default EditableInput;

src/components/Modal/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ const ReactModal = ({ children, onRequestClose, ...props }) => (
2626
overlayClassName="Overlay"
2727
{...props}
2828
>
29-
<div className="Modal__close" onClick={() => onRequestClose()}>
29+
<div
30+
className="Modal__close"
31+
onClick={() => onRequestClose()}
32+
role="presentation"
33+
>
3034
3135
</div>
3236
{children}
@@ -38,4 +42,8 @@ ReactModal.propTypes = {
3842
children: PropTypes.node.isRequired,
3943
};
4044

45+
ReactModal.defaultProps = {
46+
onRequestClose: undefined,
47+
};
48+
4149
export default ReactModal;

src/components/ModalCreateTicket.js

+15-31
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,21 @@ import Modal from './Modal';
44

55
import Ticket from './Ticket';
66

7-
class ModalCreateTicket extends React.Component {
8-
state = {
9-
ticket: {
10-
values: [
11-
[7, 27, 39, 51, 66],
12-
[11, 20, 33, 49, 68],
13-
[8, 18, 45, 57, 75],
14-
[13, 21, 36, 53, 65],
15-
[10, 30, 37, 52, 72],
16-
],
17-
number: '0657387',
18-
},
19-
};
20-
21-
render() {
22-
const { addTicket } = this.props;
23-
return (
24-
<Modal {...this.props}>
25-
<div className="text-center">
26-
<Ticket isEditable />
27-
<button
28-
type="button"
29-
className="btn btn-outline-secondary my-2"
30-
onClick={() => addTicket()}
31-
>
32-
+ Prideti
33-
</button>
34-
</div>
35-
</Modal>
36-
);
37-
}
7+
function ModalCreateTicket({ addTicket, ...props }) {
8+
return (
9+
<Modal {...props}>
10+
<div className="text-center">
11+
<Ticket isEditable />
12+
<button
13+
type="button"
14+
className="btn btn-outline-secondary my-2"
15+
onClick={() => addTicket()}
16+
>
17+
+ Prideti
18+
</button>
19+
</div>
20+
</Modal>
21+
);
3822
}
3923

4024
ModalCreateTicket.propTypes = {

src/components/Sphere/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import './index.css';
66
import { padZero } from '../../utils';
77

88
const Sphere = ({ value, className }) => {
9-
const getSphereColor = color => {
9+
const getSphereColor = () => {
1010
if (value > 0 && value < 16) return 'Sphere--blue';
1111
if (value >= 16 && value < 31) return 'Sphere--black';
1212
if (value >= 31 && value < 46) return 'Sphere--red';
1313
if (value >= 46 && value < 61) return 'Sphere--yellow';
1414
if (value >= 61 && value < 76) return 'Sphere--green';
15+
return '';
1516
};
1617

1718
return (

src/components/Status/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const Status = ({ color, message }) => {
1313
};
1414

1515
Status.propTypes = {
16+
message: PropTypes.string.isRequired,
1617
color: PropTypes.oneOf(['green', 'default']).isRequired,
1718
};
1819

src/components/Ticket/index.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3+
import requiredIf from 'react-required-if';
34
import cx from 'classnames';
4-
import { TicketField, EditableInput } from '../';
55

6+
import { TicketField, EditableInput } from '../';
67
import './index.css';
78

89
import { handleLottoInput, handleLottoTicketInput, padZero } from '../../utils';
@@ -26,11 +27,11 @@ class Ticket extends React.Component {
2627
handleInputChange = inputData => {
2728
const { id, value } = inputData;
2829
const [x, y] = id.split('_');
29-
const newValues = this.state.editableTicket.values;
30+
const newValues = [...this.state.editableTicket.values];
3031
newValues[x][y] = handleLottoInput(value);
31-
this.setState({ ...this.state.editableTicket, values: newValues });
32-
console.log(inputData);
33-
console.log(this.state.editableTicket);
32+
this.setState({ ...this.state.editableTicket });
33+
// console.log(inputData);
34+
// console.log(this.state.editableTicket);
3435
};
3536

3637
handleTicketNumberChange = e =>
@@ -43,7 +44,7 @@ class Ticket extends React.Component {
4344

4445
render() {
4546
const {
46-
index,
47+
ticketIndex,
4748
values,
4849
rolledValues,
4950
number,
@@ -60,7 +61,7 @@ class Ticket extends React.Component {
6061
<a
6162
href="#remove"
6263
className="Ticket__close"
63-
onClick={() => onTicketRemove(index)}
64+
onClick={() => onTicketRemove(ticketIndex)}
6465
>
6566
6667
</a>
@@ -117,15 +118,25 @@ class Ticket extends React.Component {
117118
}
118119

119120
Ticket.propTypes = {
121+
ticketIndex: PropTypes.number,
120122
values: PropTypes.arrayOf(PropTypes.array.isRequired).isRequired,
121123
rolledValues: PropTypes.arrayOf(PropTypes.string),
122124
number: PropTypes.string.isRequired,
123125
className: PropTypes.string,
126+
isClickable: PropTypes.bool,
127+
isEditable: PropTypes.bool,
128+
clickHandler: requiredIf(PropTypes.func, props => props.isEditable),
129+
onTicketRemove: requiredIf(PropTypes.func, props => props.isEditable),
124130
};
125131

126132
Ticket.defaultProps = {
127133
rolledValues: [],
134+
ticketIndex: undefined,
128135
className: undefined,
136+
isClickable: undefined,
137+
isEditable: undefined,
138+
clickHandler: undefined,
139+
onTicketRemove: undefined,
129140
};
130141

131142
export default Ticket;

0 commit comments

Comments
 (0)