I have functions createSwipe, fetchUserSwipes, and fetchSwipedUserSwipes. I call fetchUserSwipes and fetchSwipedUserSwipes inside createSwipe using these:
await dispatch('fetchSwipedUserSwipes', swipedUserId);
await dispatch('fetchUserSwipes', userId);
They don’t seem to be called because the console logs I included in those functions are not printing in the console. These are the console logs:
//in fetchUserSwipes
console.log('Fetched User swipes:', data.userSwipes);
//in fetchSwipedUserSwipes
console.log('Fetched Swiped User swipes:', data.userSwipes);
Here is the rest of the code:
import { gql } from '@apollo/client/core';
import { apolloProvider } from '@/apollo-config';
const GET_TOTAL_SWIPES = gql`
query {
totalSwipes
}
`;
const CREATE_SWIPE = gql`
mutation($input: CreateSwipeInput!) {
createSwipe(input: $input) {
id
userId
swipedUserId
likedBack
createdAt
updatedAt
}
}
`;
const GET_USER_SWIPES = gql`
query GetUserSwipes($userId: ID!) {
userSwipes(userId: $userId) {
id
userId
swipedUserId
likedBack
}
}
`;
const state = {
totalSwipes: 0,
userSwipes: {},
swipedUserSwipes: {},
errorMessage: '',
};
const getters = {
getTotalSwipes: state => state.totalSwipes,
getUserSwipes: state => state.userSwipes,
getSwipedUserSwipes: state => state.swipedUserSwipes,
getSwipesCountErrorMessage: state => state.errorMessage,
getCreateSwipeErrorMessage: state => state.errorMessage,
};
const actions = {
async fetchTotalSwipes({ commit }) {
try {
const { data } = await apolloProvider.defaultClient.query({
query: GET_TOTAL_SWIPES,
});
commit('SET_TOTAL_SWIPES', data.totalSwipes);
console.log('Total Swipes:', data.totalSwipes);
} catch (error) {
commit('SET_ERROR_MESSAGE', error.message);
}
},
async createSwipe({ commit, dispatch, getters }, { userId, swipedUserId }) {
try {
const { data } = await apolloProvider.defaultClient.mutate({
mutation: CREATE_SWIPE,
variables: { input: { userId, swipedUserId } },
});
const swipe = data.createSwipe;
// Fetch swipes for the swiped user
await dispatch('fetchSwipedUserSwipes', swipedUserId);
await dispatch('fetchUserSwipes', userId);
// Get the swiped user's swipes from the getter
const swipedUserSwipes = getters.getSwipedUserSwipes[swipedUserId] || [];
const matchingSwipe = swipedUserSwipes.find(swipe => swipe.userId === userId);
if (matchingSwipe) {
// Set likedBack to true for both users
matchingSwipe.likedBack = true;
swipe.likedBack = true;
// Create matches for both users
await dispatch('matches/createMatch', { userId, matchedUserId: swipedUserId }, { root: true });
await dispatch('matches/createMatch', { userId: swipedUserId, matchedUserId: userId }, { root: true });
}
} catch (error) {
commit('SET_ERROR_MESSAGE', error.message);
}
},
async fetchSwipedUserSwipes({ commit }, userId) {
try {
const { data } = await apolloProvider.defaultClient.query({
query: GET_USER_SWIPES,
variables: { userId },
});
commit('SET_SWIPED_USER_SWIPES', { userId, swipes: data.userSwipes });
console.log('Fetched Swiped User swipes:', data.userSwipes);
} catch (error) {
commit('SET_ERROR_MESSAGE', error.message);
}
},
async fetchUserSwipes({ commit }, userId) {
try {
const { data } = await apolloProvider.defaultClient.query({
query: GET_USER_SWIPES,
variables: { userId },
});
commit('SET_USER_SWIPES', { userId, swipes: data.userSwipes });
console.log('Fetched User swipes:', data.userSwipes);
} catch (error) {
commit('SET_ERROR_MESSAGE', error.message);
}
},
};
const mutations = {
SET_TOTAL_SWIPES(state, totalSwipes) {
state.totalSwipes = totalSwipes;
},
SET_USER_SWIPES(state, { userId, swipes }) {
state.userSwipes = {
...state.userSwipes,
[userId]: swipes,
};
},
SET_SWIPED_USER_SWIPES(state, { userId, swipes }) {
state.swipedUserSwipes = {
...state.swipedUserSwipes,
[userId]: swipes,
};
},
SET_ERROR_MESSAGE(state, message) {
state.errorMessage = message;
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};
Any help would be greatly appreciated. Thank you so much.