()
| 53 | } |
| 54 | |
| 55 | export const useStopBoost = (): UseStopBoost => { |
| 56 | const { user } = useAuthContext(); |
| 57 | const client = useQueryClient(); |
| 58 | const { showPrompt } = usePrompt(); |
| 59 | const { onCancelBoost, isLoadingCancel } = useCampaignMutation({}); |
| 60 | |
| 61 | const clearBoostedFlag = (campaign: Campaign) => { |
| 62 | const id = campaign.referenceId; |
| 63 | |
| 64 | if (campaign.type === CampaignType.Post) { |
| 65 | updatePostCache(client, id, (old) => ({ |
| 66 | flags: withoutCampaign(old.flags), |
| 67 | })); |
| 68 | |
| 69 | client.setQueryData<PostData>(getPostByIdKey(id), (old) => |
| 70 | old |
| 71 | ? { |
| 72 | ...old, |
| 73 | post: { ...old.post, flags: withoutCampaign(old.post.flags) }, |
| 74 | } |
| 75 | : old, |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | if (campaign.type === CampaignType.Squad) { |
| 80 | client.setQueryData<Squad>( |
| 81 | generateQueryKey(RequestKey.Squad, user, campaign.source?.handle), |
| 82 | (old) => (old ? { ...old, flags: withoutCampaign(old.flags) } : old), |
| 83 | ); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | const onStopBoost = async (campaign: Campaign) => { |
| 88 | if (!(await showPrompt(stopBoostPromptOptions))) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // The mutation already shows a toast when it fails |
| 93 | const data = await onCancelBoost(campaign).catch(() => null); |
| 94 | |
| 95 | if (data?.transactionId) { |
| 96 | clearBoostedFlag(campaign); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | return { onStopBoost, isStopping: isLoadingCancel }; |
| 101 | }; |
no test coverage detected