|
| 1 | +let id = 1; |
| 2 | + |
| 3 | +function _random(max) { |
| 4 | + return Math.round(Math.random()*1000)%max; |
| 5 | +} |
| 6 | + |
| 7 | +function buildData(count) { |
| 8 | + count = count || 1000; |
| 9 | + const adjectives = ["pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", "cheap", "expensive", "fancy"]; |
| 10 | + const colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"]; |
| 11 | + const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"]; |
| 12 | + |
| 13 | + return new Array(count).fill('').map(() => { |
| 14 | + return { |
| 15 | + id: id++, |
| 16 | + label: `${adjectives[_random(adjectives.length)]} ${colours[_random(colours.length)]} ${nouns[_random(nouns.length)]}` |
| 17 | + }; |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +module.exports = { |
| 22 | + state: { |
| 23 | + data: [], |
| 24 | + selected: false |
| 25 | + }, |
| 26 | + reducers: { |
| 27 | + run: function(state) { |
| 28 | + return { data: buildData(1000), selected: undefined }; |
| 29 | + }, |
| 30 | + add: function(state) { |
| 31 | + return { |
| 32 | + data: state.data.slice().concat(buildData(1000)), |
| 33 | + selected: undefined |
| 34 | + }; |
| 35 | + }, |
| 36 | + runLots: function(state) { |
| 37 | + return { data: buildData(10000), selected: undefined }; |
| 38 | + }, |
| 39 | + clear: function(state) { |
| 40 | + return { data: [], selected: undefined }; |
| 41 | + }, |
| 42 | + update: function(state) { |
| 43 | + return { |
| 44 | + data: state.data.slice() |
| 45 | + .map((d, i) => { |
| 46 | + if(i % 10 === 0) { |
| 47 | + d.label = `${d.label} !!!`; |
| 48 | + } |
| 49 | + return d; |
| 50 | + }), |
| 51 | + selected: undefined |
| 52 | + }; |
| 53 | + }, |
| 54 | + swapRows: function(state) { |
| 55 | + if(state.data.length > 10) { |
| 56 | + const newData = state.data.slice(); |
| 57 | + const a = newData[4]; |
| 58 | + newData[4] = newData[9]; |
| 59 | + newData[9] = a; |
| 60 | + |
| 61 | + return { |
| 62 | + data: newData, |
| 63 | + selected: state.selected |
| 64 | + }; |
| 65 | + } else { |
| 66 | + return state; |
| 67 | + } |
| 68 | + }, |
| 69 | + select: function(state, data) { |
| 70 | + return { |
| 71 | + data: state.data, |
| 72 | + selected: data.id |
| 73 | + }; |
| 74 | + }, |
| 75 | + delete: function(state, data) { |
| 76 | + return { |
| 77 | + data: state.data.filter((d) => d.id !== data.id), |
| 78 | + selected: data.id |
| 79 | + }; |
| 80 | + } |
| 81 | + } |
| 82 | +}; |
0 commit comments