Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 1x 1x 3x 3x 3x 3x 2x 2x 1x 1x 10x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import {Activity} from "../generated/api"; import { Action, ADD_ACTIVITY, ADD_ACTIVITY_RQ, AddActivityAction, EDIT_ACTIVITY, EDIT_ACTIVITY_RQ, FETCH_ACTIVITY_RQ, ProgressInfo, REMOVE_ACTIVITY, REMOVE_ACTIVITY_RQ, SET_ACTIVITIES } from "../type"; /** * The application state stored */ export type ActivityState = { /** * List of the activities */ activities: Activity[], /** * The progress info, useful for managing the transition states */ progress: ProgressInfo, } /** * Initial state */ export const initialState: ActivityState = { activities: [], progress: { add: false, fetch: false, edit: [], remove: [], } } /** * Find the index of the activity * @param state the redux state * @param id the id to look for */ const findActivityIndex = (state: ActivityState, id: string) => { let start = -1; for (let i = 0; i < state.activities.length; i++){ const a = state.activities[i]; if (a.id === id) { start = i; return start; } } return start; } /** * Main reducer * @param state the current application state * @param action the action dispatched * @return the modified {@link ActivityState} */ const activityReducer = ( state: ActivityState = initialState, action: Action ): ActivityState => { switch (action.type) { /// Transition STATES case ADD_ACTIVITY_RQ: return { ...state, progress: { ...state.progress, add: true } } case FETCH_ACTIVITY_RQ: return { ...state, progress: { ...state.progress, fetch: true } } case EDIT_ACTIVITY_RQ: return { ...state, progress: { ...state.progress, edit: state.progress.edit.concat(action.payload.id!) } } case REMOVE_ACTIVITY_RQ: return { ...state, progress: { ...state.progress, remove: state.progress.remove.concat(action.payload.id!) } } ///// case EDIT_ACTIVITY: let start = findActivityIndex(state, action.payload.id!); if (start === -1) { return state } const c = [...state.activities]; c.splice(start, 1, action.payload); const e = [...state.progress.edit]; e.splice(state.progress.edit.indexOf(action.payload.id!), 1) return { ...state, progress: { ...state.progress, edit: e }, activities: c, } case SET_ACTIVITIES: return { ...state, progress: { ...state.progress, fetch: false }, activities: action.payload.sort((a1,a2) => a2.date! - a1.date!), } case ADD_ACTIVITY: const newArticle: Activity = { id: (action as AddActivityAction).payload.id, date: (action as AddActivityAction).payload.date, description: (action as AddActivityAction).payload.description, done: (action as AddActivityAction).payload.done } return { ...state, progress: { ...state.progress, add: false }, activities: state.activities.concat(newArticle).sort((a1,a2) => a2.date! - a1.date!), } case REMOVE_ACTIVITY: let idx = findActivityIndex(state, action.payload.id!); Iif (idx === -1) { return state } const cloned = [...state.activities]; cloned.splice(idx, 1); const r = [...state.progress.remove]; r.splice(state.progress.remove.indexOf(action.payload.id!), 1) return { ...state, progress: { ...state.progress, remove: r }, activities: cloned, } } return state } export default activityReducer |