-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: re-fetch campaign details after campaign is sent (#1116)
* fix(campaign): correct Date types to string Even though sent_at, created_at, and status_updated_at are represented as Dates in the backend, these fields will be serialized to an ISO string which the frontend then receives. Hence, correct the type definition of these fields. * refactor(dashboard): refactor onModalConfirm to a util function * fix(dashboard): update local sentAt when sending campaign When sending a campaign for the first time, the `sentAt` field for the in-memory representation of `Campaign` is still undefined. This causes "invalid_Date" to appear in the filename of delivery reports. Fix this by updating the `sentAt` field of the in-memory `Campaign` to the current date when sending the campaign for the first time. Fixes issue #1113 * fix(campaign): mark sentAt and statusUpdatedAt as possibly undefined * chore: refactor campaign stats polling to a custom hook Note: make `refreshCampaignStats` a memoized callback to satisfy the useEffect dependencies. * Revert "fix(dashboard): update local sentAt when sending campaign" Instead, we'll get the actual sentAt from the source. This reverts commit 2abf7b3. * fix: fall back to the current date if sentAt is undefined * fix: fetch finalized campaign details after campaign is sent
- Loading branch information
Showing
11 changed files
with
132 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
frontend/src/components/custom-hooks/use-poll-campaign-stats.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { CampaignStats, Status } from 'classes' | ||
import { useState, useEffect, useCallback, useContext } from 'react' | ||
import { getCampaignStats, getCampaignDetails } from 'services/campaign.service' | ||
import { CampaignContext } from 'contexts/campaign.context' | ||
|
||
function usePollCampaignStats() { | ||
const { campaign, setCampaign } = useContext(CampaignContext) | ||
const { id } = campaign | ||
const [stats, setStats] = useState(new CampaignStats({})) | ||
|
||
const refreshCampaignStats = useCallback( | ||
async (forceRefresh = false) => { | ||
const updatedStats = await getCampaignStats(id, forceRefresh) | ||
setStats(updatedStats) | ||
return updatedStats | ||
}, | ||
[id] | ||
) | ||
|
||
useEffect(() => { | ||
let timeoutId: NodeJS.Timeout | ||
|
||
async function poll() { | ||
const { status } = await refreshCampaignStats() | ||
if (status !== Status.Sent) { | ||
timeoutId = setTimeout(poll, 2000) | ||
} else { | ||
const updatedCampaign = await getCampaignDetails(id) | ||
setCampaign(updatedCampaign) | ||
} | ||
} | ||
poll() | ||
|
||
return () => clearTimeout(timeoutId) | ||
}, [stats.status, refreshCampaignStats, id, setCampaign]) | ||
|
||
return { stats, refreshCampaignStats } | ||
} | ||
|
||
export default usePollCampaignStats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.