Skip to content

Commit 15cb2af

Browse files
committed
[FIX] data is null for first try
1 parent 37fa5f2 commit 15cb2af

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

src/components/UserList/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ class UserList extends Component {
1818
};
1919

2020
renderUserRow = users => {
21-
return users.map(item =>
22-
<UserItem
23-
key={`${item.id}-${item.nickname}`}
24-
{...item}
25-
onEdit={this.props.onEdit}
26-
onRemove={this.props.onRemove}
27-
/>
21+
return (
22+
users &&
23+
users.length > 0 &&
24+
users.map(item =>
25+
<UserItem
26+
key={`${item.id}-${item.nickname}`}
27+
{...item}
28+
onEdit={this.props.onEdit}
29+
onRemove={this.props.onRemove}
30+
/>
31+
)
2832
);
2933
};
3034

src/containers/Users/index.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ import React, { Component } from 'react';
22
import { Flex, Box, Button } from 'rebass';
33
import UserList from '../../components/UserList';
44
import UserCreation from '../../components/UserCreation';
5-
import {
6-
getUsers,
7-
addUser,
8-
updateUser,
9-
removeUser,
10-
initFakeData
11-
} from '../../store/user';
5+
import { getUsers, addUser, updateUser, removeUser } from '../../store/user';
126
import logger from '../../utils/logger';
137

148
class Users extends Component {
@@ -92,15 +86,14 @@ class Users extends Component {
9286

9387
componentWillMount() {
9488
logger.warn('componentWillMount');
95-
initFakeData();
9689
this.setState({
9790
users: getUsers()
9891
});
9992
}
10093

10194
render() {
10295
const { users, isAdding, newUser } = this.state;
103-
console.warn('Users render', users, isAdding, newUser);
96+
logger.warn('Users render', users, isAdding, newUser);
10497
return (
10598
<Flex column align="center">
10699
<Box m="auto">

src/store/user.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,38 @@ function getRandomIntInclusive(min, max) {
2020
return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
2121
}
2222

23-
function initFakeData() {
23+
if (hasSupportLocalStorage()) {
2424
try {
2525
data = JSON.parse(localStorage.getItem(DB_KEY));
26+
// fallback to array when its null
27+
if (!data) {
28+
data = [];
29+
}
2630
} catch (error) {
2731
data = [];
2832
}
29-
if (data && data.length === 0) {
30-
for (let index = 0; index < 5; index++) {
31-
data.push({
32-
id: faker.random.uuid(),
33-
name: faker.name.findName(),
34-
age: getRandomIntInclusive(20, 40),
35-
nickname: faker.internet.userName()
36-
});
33+
}
34+
35+
function initFakeData() {
36+
const dev = process.env.NODE_ENV !== 'production';
37+
if (dev) {
38+
if (data && data.length === 0) {
39+
for (let index = 0; index < 5; index++) {
40+
data.push({
41+
id: faker.random.uuid(),
42+
name: faker.name.findName(),
43+
age: getRandomIntInclusive(20, 40),
44+
nickname: faker.internet.userName()
45+
});
46+
}
47+
hasSupportLocalStorage() &&
48+
localStorage.setItem(DB_KEY, JSON.stringify(data));
3749
}
38-
hasSupportLocalStorage() &&
39-
localStorage.setItem(DB_KEY, JSON.stringify(data));
4050
}
4151
}
4252

53+
initFakeData();
54+
4355
function addUser(user, callback) {
4456
const newUser = { id: faker.random.uuid(), ...user };
4557
data.push(newUser);

0 commit comments

Comments
 (0)