Commit 610867df authored by Kushal Pandya's avatar Kushal Pandya

Use GraphQL mutation to toggle Epic subscription

Use `epicSetSubscription` mutation to update epic
subscription.
parent 206d1980
mutation epicSetSubscription($epicSetSubscriptionInput: EpicSetSubscriptionInput!) {
epicSetSubscription(input: $epicSetSubscriptionInput) {
clientMutationId
errors
}
}
...@@ -8,6 +8,7 @@ import epicUtils from '../utils/epic_utils'; ...@@ -8,6 +8,7 @@ import epicUtils from '../utils/epic_utils';
import { statusType, statusEvent, dateTypes } from '../constants'; import { statusType, statusEvent, dateTypes } from '../constants';
import updateEpic from '../queries/updateEpic.mutation.graphql'; import updateEpic from '../queries/updateEpic.mutation.graphql';
import epicSetSubscription from '../queries/epicSetSubscription.mutation.graphql';
import * as types from './mutation_types'; import * as types from './mutation_types';
...@@ -182,12 +183,26 @@ export const requestEpicSubscriptionToggleFailure = ({ commit, state }) => { ...@@ -182,12 +183,26 @@ export const requestEpicSubscriptionToggleFailure = ({ commit, state }) => {
}; };
export const toggleEpicSubscription = ({ state, dispatch }) => { export const toggleEpicSubscription = ({ state, dispatch }) => {
dispatch('requestEpicSubscriptionToggle'); dispatch('requestEpicSubscriptionToggle');
axios epicUtils.gqClient
.post(state.toggleSubscriptionPath) .mutate({
.then(() => { mutation: epicSetSubscription,
variables: {
epicSetSubscriptionInput: {
iid: `${state.epicId}`,
groupPath: state.groupPath,
subscribedState: !state.subscribed,
},
},
})
.then(({ data }) => {
if (!data?.epicSetSubscription?.errors.length) {
dispatch('requestEpicSubscriptionToggleSuccess', { dispatch('requestEpicSubscriptionToggleSuccess', {
subscribed: !state.subscribed, subscribed: !state.subscribed,
}); });
} else {
// eslint-disable-next-line @gitlab/i18n/no-non-i18n-strings
throw new Error('An error occurred while toggling to notifications.');
}
}) })
.catch(() => { .catch(() => {
dispatch('requestEpicSubscriptionToggleFailure'); dispatch('requestEpicSubscriptionToggleFailure');
......
...@@ -8,7 +8,6 @@ export default () => ({ ...@@ -8,7 +8,6 @@ export default () => ({
labelsPath: '', labelsPath: '',
todoPath: '', todoPath: '',
todoDeletePath: '', todoDeletePath: '',
toggleSubscriptionPath: '',
// URLs to use with links // URLs to use with links
epicsWebUrl: '', epicsWebUrl: '',
......
...@@ -769,6 +769,13 @@ describe('Epic Store Actions', () => { ...@@ -769,6 +769,13 @@ describe('Epic Store Actions', () => {
describe('toggleEpicSubscription', () => { describe('toggleEpicSubscription', () => {
let mock; let mock;
const mockEpicSetSubscriptionRes = {
epicSetSubscription: {
clientMutationId: null,
errors: [],
__typename: 'EpicSetSubscriptionPayload',
},
};
const stateSubscribed = { const stateSubscribed = {
subscribed: false, subscribed: false,
}; };
...@@ -784,6 +791,11 @@ describe('Epic Store Actions', () => { ...@@ -784,6 +791,11 @@ describe('Epic Store Actions', () => {
describe('success', () => { describe('success', () => {
it('dispatches requestEpicSubscriptionToggle and requestEpicSubscriptionToggleSuccess with param `subscribed` when request is complete', done => { it('dispatches requestEpicSubscriptionToggle and requestEpicSubscriptionToggleSuccess with param `subscribed` when request is complete', done => {
mock.onPost(/(.*)/).replyOnce(200, {}); mock.onPost(/(.*)/).replyOnce(200, {});
spyOn(epicUtils.gqClient, 'mutate').and.returnValue(
Promise.resolve({
data: mockEpicSetSubscriptionRes,
}),
);
testAction( testAction(
actions.toggleEpicSubscription, actions.toggleEpicSubscription,
...@@ -807,6 +819,16 @@ describe('Epic Store Actions', () => { ...@@ -807,6 +819,16 @@ describe('Epic Store Actions', () => {
describe('failure', () => { describe('failure', () => {
it('dispatches requestEpicSubscriptionToggle and requestEpicSubscriptionToggleFailure when request fails', done => { it('dispatches requestEpicSubscriptionToggle and requestEpicSubscriptionToggleFailure when request fails', done => {
mock.onPost(/(.*)/).replyOnce(500, {}); mock.onPost(/(.*)/).replyOnce(500, {});
spyOn(epicUtils.gqClient, 'mutate').and.returnValue(
Promise.resolve({
data: {
epicSetSubscription: {
...mockEpicSetSubscriptionRes,
errors: [{ foo: 'bar' }],
},
},
}),
);
testAction( testAction(
actions.toggleEpicSubscription, actions.toggleEpicSubscription,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment