All files / publisher/release/actions defaultTrack.js

86.95% Statements 20/23
50% Branches 1/2
90% Functions 9/10
86.95% Lines 20/23

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          4x 4x   4x 2x                         2x     2x         1x 1x 1x   1x 1x       1x     1x                         1x 1x 1x   1x   1x       1x                                             1x            
import "whatwg-fetch";
 
import { CLOSE_MODAL } from "./modal";
import { showNotification } from "./globalNotification";
 
export const SET_DEFAULT_TRACK_SUCCESS = "SET_DEFAULT_TRACK_SUCCESS";
export const SET_DEFAULT_TRACK_FAILURE = "SET_DEFAULT_TRACK_FAILURE";
 
const fetchDefaultTrack = (snapName, csrfToken, track) => {
  return fetch(`/${snapName}/releases/default-track`, {
    method: "POST",
    mode: "cors",
    cache: "no-cache",
    credentials: "same-origin",
    headers: {
      "Content-Type": "application/json; charset=utf-8",
      "X-CSRFToken": csrfToken,
    },
    redirect: "follow",
    referrer: "no-referrer",
    body: JSON.stringify({ default_track: track }),
  }).then((response) => {
    Iif (!response.ok) {
      return Promise.reject(response);
    }
    return response.json();
  });
};
 
export function clearDefaultTrack() {
  return (dispatch, getState) => {
    const { options } = getState();
    const { snapName, csrfToken } = options;
 
    return fetchDefaultTrack(snapName, csrfToken, null).then(() => {
      dispatch({
        type: SET_DEFAULT_TRACK_SUCCESS,
        payload: null,
      });
      dispatch({
        type: CLOSE_MODAL,
      });
      dispatch(
        showNotification({
          status: "success",
          appearance: "positive",
          content: `The default track for ${snapName} has been removed. All new installations without a specified track (e.g. \`sudo snap install ${snapName}\`) will receive updates from latest track.`,
          canDismiss: true,
        }),
      );
    });
  };
}
 
export function setDefaultTrack() {
  return (dispatch, getState) => {
    const { options, currentTrack } = getState();
    const { snapName, csrfToken } = options;
 
    return fetchDefaultTrack(snapName, csrfToken, currentTrack)
      .then(() => {
        dispatch({
          type: SET_DEFAULT_TRACK_SUCCESS,
          payload: currentTrack,
        });
        dispatch(
          showNotification({
            status: "success",
            appearance: "positive",
            content: `The default track for ${snapName} has been set to ${currentTrack}. All new installations without a specified track (e.g. \`sudo snap install ${snapName}\`) will receive updates from the newly defined default track.`,
            canDismiss: true,
          }),
        );
      })
      .catch((errorResponse) => {
        dispatch({
          type: SET_DEFAULT_TRACK_FAILURE,
        });
        dispatch(
          showNotification({
            status: "error",
            appearance: "negative",
            content: `Failed to set the default track for ${snapName}. (${errorResponse.status}: ${errorResponse.statusText})`,
            canDismiss: true,
          }),
        );
      })
      .finally(() => {
        dispatch({
          type: CLOSE_MODAL,
        });
      });
  };
}