github.com/replicatedhq/ship@v0.55.0/web/init/src/redux/data/determineSteps/actions.js (about)

     1  import "isomorphic-fetch";
     2  import { loadingData } from "../../ui/main/actions";
     3  import { Utilities } from "../../../utilities/utilities";
     4  
     5  export const constants = {
     6    RECEIVE_CURRENT_STEP: "RECEIVE_CURRENT_STEP",
     7    SET_STEP_ERROR: "SET_STEP_ERROR"
     8  };
     9  
    10  export function receiveCurrentStep(step) {
    11    return {
    12      type: constants.RECEIVE_CURRENT_STEP,
    13      payload: step
    14    };
    15  }
    16  
    17  export function setStepError(message) {
    18    return {
    19      type: constants.SET_STEP_ERROR,
    20      payload: message
    21    }
    22  }
    23  
    24  export function getCurrentStep(loaderType = "getCurrentStep") {
    25    return async (dispatch, getState) => {
    26      const { apiEndpoint } = getState();
    27      let response;
    28      dispatch(loadingData(loaderType, true));
    29      try {
    30        const url = `${apiEndpoint}/lifecycle/current`;
    31        response = await fetch(url, {
    32          method: "GET",
    33          headers: {
    34            "Accept": "application/json",
    35          },
    36        });
    37        if (!response.ok) {
    38          dispatch(loadingData(loaderType, false));
    39          return;
    40        }
    41        const body = await response.json();
    42        dispatch(loadingData(loaderType, false));
    43        dispatch(receiveCurrentStep(body));
    44      } catch (error) {
    45        console.log(error);
    46        if (Utilities.isFailedToFetchErr(error.message)) {
    47          dispatch(receiveCurrentStep({ currentStep: {}, phase: "loading"}));
    48        } else {
    49          dispatch(setStepError(error.message));
    50        }
    51        return;
    52      }
    53    };
    54  }
    55  
    56  export function submitAction(payload) {
    57    const { uri, method, body } = payload.action.onclick;
    58    return async (dispatch, getState) => {
    59      const { apiEndpoint } = getState();
    60      dispatch(loadingData("submitAction", true));
    61      try {
    62        const url = `${apiEndpoint}${uri}`;
    63        await fetch(url, {
    64          method,
    65          body,
    66          headers: {
    67            "Accept": "application/json",
    68            "Content-Type": "application/json"
    69          },
    70        });
    71        dispatch(loadingData("submitAction", false));
    72        dispatch(getCurrentStep());
    73      } catch (error) {
    74        console.log(error);
    75        dispatch(loadingData("postConfirm", false));
    76        return;
    77      }
    78    };
    79  }