github.com/replicatedhq/ship@v0.55.0/web/init/src/redux/data/appRoutes/actions.js (about) 1 import "isomorphic-fetch"; 2 import { loadingData } from "../../ui/main/actions"; 3 import { receiveCurrentStep } from "../determineSteps/actions"; 4 5 export const constants = { 6 RECEIVE_ROUTES: "RECEIVE_ROUTES", 7 SET_PHASE: "SET_PHASE", 8 SET_PROGRESS: "SET_PROGRESS", 9 POLLING: "POLLING", 10 SHUTDOWN_APP: "SHUTDOWN_APP", 11 }; 12 13 export function receiveRoutes(routes) { 14 return { 15 type: constants.RECEIVE_ROUTES, 16 payload: routes 17 }; 18 } 19 20 export function setPhase(phase) { 21 return { 22 type: constants.SET_PHASE, 23 payload: phase 24 } 25 } 26 27 export function polling(isPolling) { 28 return { 29 type: constants.POLLING, 30 payload: isPolling, 31 }; 32 } 33 34 export function setProgress(progress) { 35 return { 36 type: constants.SET_PROGRESS, 37 payload: progress, 38 }; 39 } 40 41 export function shutdownApp() { 42 return { 43 type: constants.SHUTDOWN_APP 44 } 45 } 46 47 export function getRoutes() { 48 return async (dispatch, getState) => { 49 const { apiEndpoint } = getState(); 50 let response; 51 dispatch(loadingData("routes", true)); 52 try { 53 const url = `${apiEndpoint}/navcycle`; 54 response = await fetch(url, { 55 method: "GET", 56 headers: { 57 "Accept": "application/json", 58 }, 59 }); 60 if (!response.ok) { 61 dispatch(loadingData("routes", false)); 62 return; 63 } 64 const body = await response.json(); 65 dispatch(loadingData("routes", false)); 66 dispatch(receiveRoutes(body)); 67 } catch (error) { 68 console.log(error); 69 return; 70 } 71 }; 72 } 73 74 export async function fetchContentForStep(apiEndpoint, stepId) { 75 const url = `${apiEndpoint}/navcycle/step/${stepId}`; 76 const response = await fetch(url, { 77 method: "GET", 78 headers: { 79 "Accept": "application/json", 80 }, 81 }); 82 const body = await response.json(); 83 return body; 84 } 85 86 export function pollContentForStep(stepId, cb) { 87 return async(dispatch, getState) => { 88 dispatch(polling(true)); 89 90 const { apiEndpoint } = getState(); 91 const intervalId = setInterval(async() => { 92 const body = await fetchContentForStep(apiEndpoint, stepId).catch(() => { 93 dispatch(polling(false)); 94 clearInterval(intervalId); 95 return; 96 }); 97 // Ignore Gateway Timeout errors 98 if (body.status === 504) { 99 return; 100 } 101 102 dispatch(setProgress(body.progress)); 103 104 const { progress } = body; 105 const { detail } = progress; 106 const { status: parsedDetailStatus } = JSON.parse(detail); 107 108 const finishedStatus = parsedDetailStatus === "success"; 109 const messageStatus = parsedDetailStatus === "message"; 110 const errorStatus = parsedDetailStatus === "error"; 111 112 if (finishedStatus) { 113 dispatch(polling(false)); 114 clearInterval(intervalId); 115 return cb(); 116 } 117 if (errorStatus || messageStatus) { 118 dispatch(polling(false)); 119 clearInterval(intervalId); 120 return; 121 } 122 }, 1000); 123 return; 124 }; 125 } 126 127 export function getContentForStep(stepId) { 128 return async (dispatch, getState) => { 129 const { apiEndpoint } = getState(); 130 131 let response; 132 dispatch(loadingData("getCurrentStep", true)); 133 try { 134 const url = `${apiEndpoint}/navcycle/step/${stepId}`; 135 response = await fetch(url, { 136 method: "GET", 137 headers: { 138 "Accept": "application/json", 139 }, 140 }); 141 if (!response.ok) { 142 dispatch(loadingData("getCurrentStep", false)); 143 if (response.status === 400) { 144 const body = await response.json(); 145 if (body) { 146 dispatch(receiveCurrentStep(body)); 147 } 148 } 149 return; 150 } 151 const body = await response.json(); 152 dispatch(loadingData("getCurrentStep", false)); 153 let resp = body; 154 if(!body.currentStep) { 155 resp["currentStep"] = {} 156 } 157 dispatch(receiveCurrentStep(resp)); 158 } catch (error) { 159 console.log(error); 160 return; 161 } 162 }; 163 } 164 165 export function finalizeStep(payload) { 166 const { uri, method, body } = payload.action.onclick; 167 return async (dispatch, getState) => { 168 const { apiEndpoint } = getState(); 169 let response; 170 dispatch(loadingData("submitAction", true)); 171 try { 172 const url = `${apiEndpoint}${uri}`; 173 response = await fetch(url, { 174 method, 175 body, 176 headers: { 177 "Accept": "application/json", 178 "Content-Type": "application/json" 179 }, 180 }); 181 if (!response.ok) { 182 dispatch(loadingData("submitAction", false)); 183 } 184 dispatch(loadingData("submitAction", false)); 185 // dispatch(getCurrentStep("getCurrentStep", nextStep)); 186 } catch (error) { 187 console.log(error); 188 dispatch(loadingData("postConfirm", false)); 189 return; 190 } 191 }; 192 } 193 194 export function initializeStep(stepId) { 195 return async(dispatch, getState) => { 196 const { apiEndpoint } = getState(); 197 try { 198 const url = `${apiEndpoint}/navcycle/step/${stepId}`; 199 await fetch(url, { 200 method: "POST", 201 headers: { 202 "Accept": "application/json", 203 }, 204 }); 205 } catch (error) { 206 console.log(error); 207 return; 208 } 209 }; 210 }