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