-
Notifications
You must be signed in to change notification settings - Fork 843
/
Copy pathdata_store.js
150 lines (133 loc) ยท 3.95 KB
/
data_store.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
import { Comparators } from '../../../../src/services/sort';
import { Random } from '../../../../src/services/random';
import { times } from '../../../../src/services/utils';
const random = new Random();
const createCountries = () => [
{ code: 'NL', name: 'Netherlands', flag: '๐ณ๐ฑ' },
{ code: 'CZ', name: 'Czech Republic', flag: '๐จ๐ฟ' },
{ code: 'ZA', name: 'South Africa', flag: '๐ฟ๐ฆ' },
{ code: 'US', name: 'United States', flag: '๐บ๐ฒ' },
{ code: 'AU', name: 'Australia', flag: '๐ฆ๐บ' },
{ code: 'IL', name: 'Israel', flag: '๐ฎ๐ฑ' },
{ code: 'NO', name: 'Norway', flag: '๐ณ๐ด' },
{ code: 'IT', name: 'Italy', flag: '๐ฎ๐น' },
{ code: 'CA', name: 'Canada', flag: '๐จ๐ฆ' },
{ code: 'CG', name: 'Congo', flag: '๐จ๐ฌ' },
{ code: 'CL', name: 'Chile', flag: '๐จ๐ฑ' },
{ code: 'FJ', name: 'Fiji', flag: '๐ซ๐ฏ' },
{ code: 'GB', name: 'United Kingdom', flag: '๐ฌ๐ง' },
{ code: 'GR', name: 'Greece', flag: '๐ฌ๐ท' },
{ code: 'HT', name: 'Haiti', flag: '๐ญ๐น' },
{ code: 'LB', name: 'Lebanon', flag: '๐ฑ๐ง' },
{ code: 'MM', name: 'Myanmar', flag: '๐ฒ๐ฒ' },
{ code: 'MX', name: 'Mexico', flag: '๐ฒ๐ฝ' },
{ code: 'NG', name: 'Nigeria', flag: '๐ณ๐ฌ' },
{ code: 'SG', name: 'Singapore', flag: '๐ธ๐ฌ' },
{ code: 'SO', name: 'Somalia', flag: '๐ธ๐ด' },
{ code: 'TN', name: 'Tunisia', flag: '๐น๐ณ' },
{ code: 'VE', name: 'Venezuela', flag: '๐ป๐ช' },
{ code: 'ZM', name: 'Zambia', flag: '๐ฟ๐ฒ' },
];
const firstNames = [
'Very long first name that will wrap or be truncated',
'Another very long first name which will wrap or be truncated',
'Clinton',
'Igor',
undefined,
'Drew',
null,
'Rashid',
undefined,
'John',
];
const lastNames = [
'Very long last name that will wrap or be truncated',
'Another very long last name which will wrap or be truncated',
'Gormley',
'Motov',
'Minarik',
'Raines',
'Krรกl',
'Khan',
'Sissel',
'Dorlus',
];
const github = [
'martijnvg',
'elissaw',
'clintongormley',
'imotov',
'karmi',
'drewr',
'HonzaKral',
'rashidkpc',
'jordansissel',
'silne30',
];
const dob = new Date(1980, 1, 1);
const createUsers = countries => {
return times(20, index => {
return {
id: index,
firstName: index < 10 ? firstNames[index] : firstNames[index - 10],
lastName: index < 10 ? lastNames[index] : lastNames[index - 10],
github: index < 10 ? github[index] : github[index - 10],
dateOfBirth: dob,
nationality: random.oneToOne(
countries.map(country => country.code),
index
),
online: index % 2 === 0,
};
});
};
export const createDataStore = () => {
const countries = createCountries();
const users = createUsers(countries);
return {
countries,
users,
findUsers: (pageIndex, pageSize, sortField, sortDirection) => {
let items;
if (sortField) {
items = users
.slice(0)
.sort(
Comparators.property(sortField, Comparators.default(sortDirection))
);
} else {
items = users;
}
let pageOfItems;
if (!pageIndex && !pageSize) {
pageOfItems = items;
} else {
const startIndex = pageIndex * pageSize;
pageOfItems = items.slice(
startIndex,
Math.min(startIndex + pageSize, items.length)
);
}
return {
pageOfItems,
totalItemCount: items.length,
};
},
deleteUsers: (...ids) => {
ids.forEach(id => {
const index = users.findIndex(user => user.id === id);
if (index >= 0) {
users.splice(index, 1);
}
});
},
cloneUser: id => {
const index = users.findIndex(user => user.id === id);
if (index >= 0) {
const user = users[index];
users.splice(index, 0, { ...user, id: users.length });
}
},
getCountry: code => countries.find(country => country.code === code),
};
};