Skip to content

Commit

Permalink
* added wave delete mutation
Browse files Browse the repository at this point in the history
* separate wave delete from comment delete
  • Loading branch information
aliseyalvi committed Jul 5, 2024
1 parent 12dcede commit 31d2c2c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
16 changes: 9 additions & 7 deletions src/components/comments/container/commentsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import CommentsView from '../view/commentsView';
import { updateCommentCache } from '../../../redux/actions/cacheActions';
import { CacheStatus } from '../../../redux/reducers/cacheReducer';
import { postQueries } from '../../../providers/queries';
import { useDeleteWaveMutation } from '../../../providers/queries/postQueries/wavesQueries';
import { PostTypes } from '../../../constants/postTypes';

const CommentsContainer = ({
Expand Down Expand Up @@ -55,10 +54,10 @@ const CommentsContainer = ({
handleOnReplyPress,
handleOnCommentsLoaded,
postType,
handleWaveDelete,
}) => {
const navigation = useNavigation();
const postsCachePrimer = postQueries.usePostsCachePrimer();
const deleteWaveMutation = useDeleteWaveMutation();

const [lcomments, setLComments] = useState([]);
const [propComments, setPropComments] = useState(comments);
Expand Down Expand Up @@ -207,7 +206,14 @@ const CommentsContainer = ({

const _handleDeleteComment = (_permlink) => {
let filteredComments;

if (postType === PostTypes.WAVE && handleWaveDelete) {
handleWaveDelete({
currentAccount,
pinCode,
_permlink,
});
return;
}
deleteComment(currentAccount, pinCode, _permlink).then(() => {
let deletedItem = null;

Expand All @@ -224,10 +230,6 @@ const CommentsContainer = ({
setLComments(filteredComments);
} else {
filteredComments = propComments.filter(_applyFilter);
console.log('filteredComments : ', filteredComments);
if (postType === PostTypes.WAVE) {
deleteWaveMutation.mutate(filteredComments);
}
setPropComments(filteredComments);
}

Expand Down
62 changes: 35 additions & 27 deletions src/providers/queries/postQueries/wavesQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
import { useEffect, useMemo, useRef, useState } from 'react';

import { unionBy, isArray } from 'lodash';
import { getDiscussionCollection, getAccountPosts } from '../../hive/dhive';
import { useDispatch } from 'react-redux';
import { useIntl } from 'react-intl';
import { getDiscussionCollection, getAccountPosts, deleteComment } from '../../hive/dhive';

import QUERIES from '../queryKeys';
import { delay } from '../../../utils/editor';
Expand All @@ -18,9 +20,12 @@ import {
mapDiscussionToThreads,
} from '../../../utils/postParser';
import { useAppSelector } from '../../../hooks';
import { toastNotification } from '../../../redux/actions/uiAction';

export const useWavesQuery = (host: string) => {
const queryClient = useQueryClient();
const dispatch = useDispatch();
const intl = useIntl();

const cache = useAppSelector((state) => state.cache);
const mutes = useAppSelector((state) => state.account.currentAccount.mutes);
Expand Down Expand Up @@ -266,13 +271,42 @@ export const useWavesQuery = (host: string) => {
return _newWaves;
};

// wave delete mutation to delete wave and update query
const deleteWave = async ({ currentAccount, pinCode, _permlink }: any) => {
const response = await deleteComment(currentAccount, pinCode, _permlink);

if (!response?.id) {
throw new Error('Failed to delete the wave');
}
return _permlink;
};

const deleteMutation = useMutation(deleteWave, {
onSuccess: (_permlink) => {
const waveQueries = queryClient.getQueriesData([QUERIES.WAVES.GET, host]);
// filter out the deleted wave from queries data
waveQueries.forEach(([queryKey, oldData]) => {
if (oldData) {
const newData = oldData?.filter((wave: { permlink: any }) => wave.permlink !== _permlink);
queryClient.setQueryData(queryKey, newData);
}
});
dispatch(toastNotification(intl.formatMessage({ id: 'alert.success' })));
},
onError: (error) => {
console.log('Failed to delete wave:', error);
dispatch(toastNotification(intl.formatMessage({ id: 'alert.error' })));
},
});

return {
data: _filteredData,
isRefreshing,
isLoading: isLoading || _lastItem?.isLoading || _lastItem?.isFetching,
fetchNextPage: _fetchNextPage,
latestWavesFetch: _lastestWavesFetch,
refresh: _refresh,
deleteWave: deleteMutation.mutate,
};
};

Expand Down Expand Up @@ -355,29 +389,3 @@ export const fetchLatestWavesContainer = async (host) => {

return _latestPost;
};

export const useDeleteWaveMutation = () => {
const queryClient = useQueryClient();

const _mutationFn = async (wavePermlink: any) => {
if (wavePermlink) {
return wavePermlink;
}

throw new Error('invalid mutations data');
};

const _options: UseMutationOptions<string, unknown, string, void> = {
onMutate: async (filteredWavesData: any) => {
await queryClient.cancelQueries(QUERIES.WAVES.GET);
queryClient.setQueryData([QUERIES.WAVES.GET], [...filteredWavesData]);

return filteredWavesData;
},
onSuccess: () => {
queryClient.invalidateQueries([QUERIES.WAVES.GET]);
},
};

return useMutation(_mutationFn, _options);
};
9 changes: 9 additions & 0 deletions src/screens/waves/screen/wavesScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ const WavesScreen = ({ route }) => {
}
};

const _handleWaveDelete = ({ currentAccount, pinCode, _permlink }: any) => {
wavesQuery.deleteWave({
currentAccount,
pinCode,
_permlink,
});
};

// scrolls to top, blocks scroll popup for 2 seconds to reappear after scroll
const _scrollTop = () => {
if (postsListRef.current) {
Expand Down Expand Up @@ -138,6 +146,7 @@ const WavesScreen = ({ route }) => {
postType={PostTypes.WAVE}
comments={_data}
handleOnOptionsPress={_handleOnOptionsPress}
handleWaveDelete={_handleWaveDelete}
flatListProps={{
ref: postsListRef,
onEndReached: _fetchData,
Expand Down

0 comments on commit 31d2c2c

Please sign in to comment.