|
| 1 | +import * from "../../types" |
| 2 | +import format from "date-fns/format"; |
| 3 | +import * as types from "../actions/actionTypes"; |
| 4 | + |
| 5 | +const initialState = { |
| 6 | + currentTab: "First Tab"; |
| 7 | + reqResArray: []; |
| 8 | + history: []; |
| 9 | + collections: []; |
| 10 | + warningMessage: ""; |
| 11 | + newRequestFields: { |
| 12 | + protocol: ""; |
| 13 | + url: "http://"; |
| 14 | + method: "GET"; |
| 15 | + graphQL: false; |
| 16 | + gRPC: false; |
| 17 | + }; |
| 18 | + newRequestHeaders: { |
| 19 | + headersArr: []; |
| 20 | + count: 0; |
| 21 | + }; |
| 22 | + newRequestStreams: { |
| 23 | + streamsArr: []; |
| 24 | + count: 0; |
| 25 | + streamContent: []; |
| 26 | + selectedPackage: null; |
| 27 | + selectedRequest: null; |
| 28 | + selectedService: null; |
| 29 | + selectedStreamingType: null; |
| 30 | + initialQuery: null; |
| 31 | + queryArr: null; |
| 32 | + protoPath: null; |
| 33 | + services: null; |
| 34 | + protoContent: ""; |
| 35 | + }; |
| 36 | + newRequestCookies: { |
| 37 | + cookiesArr: []; |
| 38 | + count: 0; |
| 39 | + }; |
| 40 | + newRequestBody: { |
| 41 | + bodyContent: ""; |
| 42 | + bodyVariables: ""; |
| 43 | + bodyType: "raw"; |
| 44 | + rawType: "Text (text/plain)"; |
| 45 | + JSONFormatted: true; |
| 46 | + }; |
| 47 | + newRequestSSE: { |
| 48 | + isSSE: false; |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +const businessReducer = (state = initialState, action) => { |
| 53 | + switch (action.type) { |
| 54 | + case types.GET_HISTORY: { |
| 55 | + return { |
| 56 | + ...state, |
| 57 | + history: action.payload, |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + case types.DELETE_HISTORY: { |
| 62 | + const deleteId = action.payload.id; |
| 63 | + const deleteDate = format(action.payload.created_at, "MM/DD/YYYY"); |
| 64 | + const newHistory = JSON.parse(JSON.stringify(state.history)); |
| 65 | + newHistory.forEach((obj, i) => { |
| 66 | + if (obj.date === deleteDate) |
| 67 | + obj.history = obj.history.filter((hist) => hist.id !== deleteId); |
| 68 | + if (obj.history.length === 0) { |
| 69 | + newHistory.splice(i, 1); |
| 70 | + } |
| 71 | + }); |
| 72 | + return { |
| 73 | + ...state, |
| 74 | + history: newHistory, |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + case types.CLEAR_HISTORY: { |
| 79 | + return { |
| 80 | + ...state, |
| 81 | + history: [], |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + case types.GET_COLLECTIONS: { |
| 86 | + return { |
| 87 | + ...state, |
| 88 | + collections: action.payload, |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + case types.DELETE_COLLECTION: { |
| 93 | + const deleteId = action.payload.id; |
| 94 | + const newCollections = JSON.parse(JSON.stringify(state.collections)); |
| 95 | + newCollections.forEach((obj, i) => { |
| 96 | + if (obj.id === deleteId) { |
| 97 | + newCollections.splice(i, 1); |
| 98 | + } |
| 99 | + }); |
| 100 | + return { |
| 101 | + ...state, |
| 102 | + collections: newCollections, |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + case types.COLLECTION_TO_REQRES: { |
| 107 | + const reqResArray = [...action.payload]; |
| 108 | + return { |
| 109 | + ...state, |
| 110 | + reqResArray, |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + case types.COLLECTION_ADD: { |
| 115 | + //add to collection to array in state |
| 116 | + return { |
| 117 | + ...state, |
| 118 | + collections: [action.payload, ...state.collections], |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + case types.REQRES_CLEAR: { |
| 123 | + return { |
| 124 | + ...state, |
| 125 | + reqResArray: [], |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + case types.REQRES_ADD: { |
| 130 | + const reqResArray = JSON.parse(JSON.stringify(state.reqResArray)); |
| 131 | + reqResArray.push(action.payload); |
| 132 | + const addDate = format(action.payload.created_at, "MM/DD/YYYY"); |
| 133 | + const newHistory = JSON.parse(JSON.stringify(state.history)); |
| 134 | + let updated = false; |
| 135 | + //if there is history for added date, add query to beginning of history |
| 136 | + newHistory.forEach((obj) => { |
| 137 | + if (obj.date === addDate) { |
| 138 | + obj.history.unshift(action.payload); |
| 139 | + updated = true; |
| 140 | + } |
| 141 | + }); |
| 142 | + //if there is not history at added date, create new history with new query |
| 143 | + if (!updated) { |
| 144 | + newHistory.unshift({ |
| 145 | + date: addDate, |
| 146 | + history: [action.payload], |
| 147 | + }); |
| 148 | + } |
| 149 | + return { |
| 150 | + ...state, |
| 151 | + reqResArray, |
| 152 | + history: newHistory, |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | + case types.REQRES_DELETE: { |
| 157 | + const deleteId = action.payload.id; |
| 158 | + const newReqResArray = state.reqResArray.filter( |
| 159 | + (reqRes) => reqRes.id !== deleteId |
| 160 | + ); |
| 161 | + return { |
| 162 | + ...state, |
| 163 | + reqResArray: newReqResArray, |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + case types.SET_CHECKS_AND_MINIS: { |
| 168 | + return { |
| 169 | + ...state, |
| 170 | + reqResArray: JSON.parse(JSON.stringify(action.payload)), |
| 171 | + }; |
| 172 | + } |
| 173 | + |
| 174 | + case types.REQRES_UPDATE: { |
| 175 | + const reqResDeepCopy = JSON.parse(JSON.stringify(state.reqResArray)); |
| 176 | + let indexToBeUpdated; |
| 177 | + reqResDeepCopy.forEach((reqRes, index) => { |
| 178 | + if (reqRes.id === action.payload.id) indexToBeUpdated = index; |
| 179 | + }); |
| 180 | + if (indexToBeUpdated !== undefined) { |
| 181 | + action.payload.checked = state.reqResArray[indexToBeUpdated].checked; |
| 182 | + action.payload.minimized = |
| 183 | + state.reqResArray[indexToBeUpdated].minimized; |
| 184 | + reqResDeepCopy.splice( |
| 185 | + indexToBeUpdated, |
| 186 | + 1, |
| 187 | + JSON.parse(JSON.stringify(action.payload)) |
| 188 | + ); //FOR SOME REASON THIS IS NECESSARY, MESSES UP CHECKS OTHERWISE |
| 189 | + } |
| 190 | + return { |
| 191 | + ...state, |
| 192 | + reqResArray: reqResDeepCopy, |
| 193 | + }; |
| 194 | + } |
| 195 | + |
| 196 | + case types.SET_COMPOSER_WARNING_MESSAGE: { |
| 197 | + return { |
| 198 | + ...state, |
| 199 | + warningMessage: action.payload, |
| 200 | + }; |
| 201 | + } |
| 202 | + |
| 203 | + case types.SET_NEW_REQUEST_FIELDS: { |
| 204 | + return { |
| 205 | + ...state, |
| 206 | + newRequestFields: action.payload, |
| 207 | + }; |
| 208 | + } |
| 209 | + |
| 210 | + case types.SET_NEW_REQUEST_HEADERS: { |
| 211 | + return { |
| 212 | + ...state, |
| 213 | + newRequestHeaders: action.payload, |
| 214 | + }; |
| 215 | + } |
| 216 | + |
| 217 | + case types.SET_NEW_REQUEST_STREAMS: { |
| 218 | + return { |
| 219 | + ...state, |
| 220 | + newRequestStreams: action.payload, |
| 221 | + }; |
| 222 | + } |
| 223 | + |
| 224 | + case types.SET_NEW_REQUEST_BODY: { |
| 225 | + return { |
| 226 | + ...state, |
| 227 | + newRequestBody: action.payload, |
| 228 | + }; |
| 229 | + } |
| 230 | + |
| 231 | + case types.SET_NEW_REQUEST_COOKIES: { |
| 232 | + return { |
| 233 | + ...state, |
| 234 | + newRequestCookies: action.payload, |
| 235 | + }; |
| 236 | + } |
| 237 | + |
| 238 | + case types.SET_NEW_REQUEST_SSE: { |
| 239 | + return { |
| 240 | + ...state, |
| 241 | + newRequestSSE: { isSSE: action.payload }, |
| 242 | + }; |
| 243 | + } |
| 244 | + |
| 245 | + case types.SET_CURRENT_TAB: { |
| 246 | + return { |
| 247 | + ...state, |
| 248 | + currentTab: action.payload, |
| 249 | + }; |
| 250 | + } |
| 251 | + |
| 252 | + default: |
| 253 | + return state; |
| 254 | + } |
| 255 | +}; |
| 256 | + |
| 257 | +export default businessReducer; |
0 commit comments