All files / publisher/pages/Releases/actions defaultTrack.ts

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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                4x 4x                               4x 2x                         2x     2x         1x 1x 1x   1x 1x       1x     1x                         1x 1x 1x   1x   1x       1x                                             1x            
import { CLOSE_MODAL } from "./modal";
import { showNotification } from "./globalNotification";
import {
  GenericReleasesAction,
  ReleasesReduxState,
  DispatchFn,
} from "../../../types/releaseTypes";
 
export const SET_DEFAULT_TRACK_SUCCESS = "SET_DEFAULT_TRACK_SUCCESS";
export const SET_DEFAULT_TRACK_FAILURE = "SET_DEFAULT_TRACK_FAILURE";
 
export type SetDefaultTrackSuccessAction = GenericReleasesAction<
  typeof SET_DEFAULT_TRACK_SUCCESS,
  ReleasesReduxState["defaultTrack"]
>;
 
export type SetDefaultTrackFailureAction = GenericReleasesAction<
  typeof SET_DEFAULT_TRACK_FAILURE,
  never
>;
 
export type DefaultTrackAction =
  | SetDefaultTrackSuccessAction
  | SetDefaultTrackFailureAction;
 
const fetchDefaultTrack = (snapName: string, track: string | null) => {
  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": (window as any).CSRF_TOKEN,
    },
    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: DispatchFn, getState: () => ReleasesReduxState) => {
    const { options } = getState();
    const { snapName } = options;
 
    return fetchDefaultTrack(snapName, null).then(() => {
      dispatch({
        type: SET_DEFAULT_TRACK_SUCCESS,
        payload: null,
      });
      dispatch({
        type: CLOSE_MODAL,
      } as any);
      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: DispatchFn, getState: () => ReleasesReduxState) => {
    const { options, currentTrack } = getState();
    const { snapName } = options;
 
    return fetchDefaultTrack(snapName, 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: Response) => {
        dispatch({
          type: SET_DEFAULT_TRACK_FAILURE,
        } as any);
        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,
        } as any);
      });
  };
}