( state: ImporterState, action: ImporterAction )
| 42 | } |
| 43 | |
| 44 | export const reducer = ( |
| 45 | state: ImporterState, |
| 46 | action: ImporterAction |
| 47 | ): ImporterState => { |
| 48 | switch (action.type) { |
| 49 | case 'ENTER_DATA_MANUALLY': { |
| 50 | const emptyData = state.sheetDefinitions.map((sheet) => ({ |
| 51 | sheetId: sheet.id, |
| 52 | rows: Array.from( |
| 53 | { length: action.payload.amountOfEmptyRowsToAdd }, |
| 54 | () => ({}) |
| 55 | ), |
| 56 | })); |
| 57 | |
| 58 | return { ...state, mode: 'preview', sheetData: emptyData }; |
| 59 | } |
| 60 | case 'FILE_PARSED': |
| 61 | return { |
| 62 | ...state, |
| 63 | parsedFile: action.payload.parsed, |
| 64 | rowFile: action.payload.rowFile, |
| 65 | mode: 'mapping', |
| 66 | }; |
| 67 | case 'UPLOAD': |
| 68 | return { ...state, mode: 'upload' }; |
| 69 | case 'COLUMN_MAPPING_CHANGED': { |
| 70 | return { |
| 71 | ...state, |
| 72 | columnMappings: action.payload.mappings, |
| 73 | }; |
| 74 | } |
| 75 | case 'DATA_MAPPED': { |
| 76 | return { |
| 77 | ...state, |
| 78 | sheetData: applyTransformations( |
| 79 | state.sheetDefinitions, |
| 80 | action.payload.mappedData |
| 81 | ), |
| 82 | mode: 'preview', |
| 83 | }; |
| 84 | } |
| 85 | case 'CELL_CHANGED': { |
| 86 | const currentData = state.sheetData; |
| 87 | |
| 88 | const newData = currentData.map((sheet) => { |
| 89 | if (sheet.sheetId === action.payload.sheetId) { |
| 90 | const newRows = [...sheet.rows]; |
| 91 | |
| 92 | newRows[action.payload.rowIndex] = recalculateCalculatedColumns( |
| 93 | action.payload.value, |
| 94 | action.payload, |
| 95 | state |
| 96 | ); |
| 97 | |
| 98 | return { ...sheet, rows: newRows }; |
| 99 | } else { |
| 100 | return sheet; |
| 101 | } |
no test coverage detected