|
1 | 1 | <script lang="ts" setup> |
2 | | -defineProps<{ |
| 2 | +import { useQueryClient, type InfiniteData } from '@tanstack/vue-query'; |
| 3 | +import { deleteTweet } from '~/services/tweet/actionButtonsService'; |
| 4 | +import { showToaster } from '~/utils/showToaster'; |
| 5 | +import type { Tweet } from '~~/shared/types/tweets'; |
| 6 | +
|
| 7 | +const props = defineProps<{ |
3 | 8 | tweet: Tweet; |
| 9 | + username: string; |
4 | 10 | }>(); |
| 11 | +
|
| 12 | +const queryClient = useQueryClient(); |
| 13 | +
|
| 14 | +interface TweetPage { |
| 15 | + data: Tweet[]; |
| 16 | + [key: string]: unknown; |
| 17 | +} |
| 18 | +
|
| 19 | +function removeTweetFromInfiniteData( |
| 20 | + data: InfiniteData<TweetPage> | undefined, |
| 21 | + tweetId: string, |
| 22 | +): InfiniteData<TweetPage> | undefined { |
| 23 | + if (!data || !data.pages) return data; |
| 24 | + return { |
| 25 | + ...data, |
| 26 | + pages: data.pages.map((page) => ({ |
| 27 | + ...page, |
| 28 | + data: page.data.filter((t: Tweet) => t.id !== tweetId), |
| 29 | + })), |
| 30 | + }; |
| 31 | +} |
| 32 | +
|
| 33 | +async function handleDelete() { |
| 34 | + try { |
| 35 | + await deleteTweet(props.tweet.id); |
| 36 | + showToaster('success', $t('tweet.delete-success')); |
| 37 | +
|
| 38 | + const queryKeys = [ |
| 39 | + ['for-you'], |
| 40 | + ['following'], |
| 41 | + ['profile', props.tweet.author.username, 'tweets'], |
| 42 | + ['profile', props.tweet.author.username, 'tweets-replies'], |
| 43 | + ['profile', props.tweet.author.username, 'tweets-media'], |
| 44 | + ['profile', props.tweet.author.username, 'tweets-likes'], |
| 45 | + ]; |
| 46 | +
|
| 47 | + queryKeys.forEach((key) => { |
| 48 | + queryClient.setQueriesData<InfiniteData<TweetPage>>({ queryKey: key }, (oldData) => |
| 49 | + removeTweetFromInfiniteData(oldData, props.tweet.id), |
| 50 | + ); |
| 51 | + }); |
| 52 | + } catch { |
| 53 | + showToaster('error', $t('tweet.delete-error')); |
| 54 | + } |
| 55 | +} |
5 | 56 | </script> |
6 | 57 | <template> |
7 | | - <UiDropdownMenu> |
8 | | - <UiDropdownMenuTrigger as-child> |
9 | | - <slot /> |
10 | | - </UiDropdownMenuTrigger> |
11 | | - <UiDropdownMenuContent align="end"> |
12 | | - <UiDropdownMenuItem as-child> |
13 | | - <NuxtLink :to="`/profile/${tweet.author.username}/status/${tweet.id}/likes`"> |
14 | | - <Icon name="ion:stats-chart" />{{ $t('tweet.engagement.label') }} |
15 | | - </NuxtLink> |
16 | | - </UiDropdownMenuItem> |
17 | | - </UiDropdownMenuContent> |
18 | | - </UiDropdownMenu> |
| 58 | + <UiAlertDialog> |
| 59 | + <UiDropdownMenu> |
| 60 | + <UiDropdownMenuTrigger as-child> |
| 61 | + <slot /> |
| 62 | + </UiDropdownMenuTrigger> |
| 63 | + <UiDropdownMenuContent align="end"> |
| 64 | + <UiDropdownMenuItem as-child> |
| 65 | + <NuxtLink |
| 66 | + :to="`/profile/${props.tweet.author.username}/status/${props.tweet.id}/likes`" |
| 67 | + class="ltr:flex-row rtl:flex-row-reverse" |
| 68 | + > |
| 69 | + <Icon name="ion:stats-chart" />{{ $t('tweet.engagement.label') }} |
| 70 | + </NuxtLink> |
| 71 | + </UiDropdownMenuItem> |
| 72 | + <UiDropdownMenuItem as-child @select.prevent> |
| 73 | + <UiAlertDialogTrigger as-child class="text-destructive ltr:flex-row rtl:flex-row-reverse"> |
| 74 | + <div v-if="props.username === props.tweet.author.username"> |
| 75 | + <Icon name="mi:delete" /> |
| 76 | + <p>{{ $t('tweet.delete-tweet') }}</p> |
| 77 | + </div> |
| 78 | + </UiAlertDialogTrigger> |
| 79 | + </UiDropdownMenuItem> |
| 80 | + </UiDropdownMenuContent> |
| 81 | + </UiDropdownMenu> |
| 82 | + |
| 83 | + <UiAlertDialogContent> |
| 84 | + <UiAlertDialogHeader> |
| 85 | + <UiAlertDialogTitle>{{ $t('tweet.delete-dialog.title') }}</UiAlertDialogTitle> |
| 86 | + <UiAlertDialogDescription> |
| 87 | + {{ $t('tweet.delete-dialog.description') }} |
| 88 | + </UiAlertDialogDescription> |
| 89 | + </UiAlertDialogHeader> |
| 90 | + <UiAlertDialogFooter> |
| 91 | + <UiAlertDialogAction |
| 92 | + class="bg-destructive text-destructive-foreground hover:bg-destructive/90" |
| 93 | + @click="handleDelete" |
| 94 | + > |
| 95 | + {{ $t('ui.delete') }} |
| 96 | + </UiAlertDialogAction> |
| 97 | + <UiAlertDialogCancel>{{ $t('ui.cancel') }}</UiAlertDialogCancel> |
| 98 | + </UiAlertDialogFooter> |
| 99 | + </UiAlertDialogContent> |
| 100 | + </UiAlertDialog> |
19 | 101 | </template> |
0 commit comments