-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save donation flow inside sessionStorage (#1533)
* one-time-donation: Save donation steps inside sessionStorage Saves the selected values from formik inside sessionStorage. The values are saved until either step 3 of donation has been reached, or until the browser tab has been closed. * FormikStepper.tsx: Clear session only on successful donation
- Loading branch information
1 parent
553b732
commit fe4fd8a
Showing
4 changed files
with
88 additions
and
8 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
65 changes: 65 additions & 0 deletions
65
src/components/client/one-time-donation/helpers/donateSession.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,65 @@ | ||
import { OneTimeDonation } from 'gql/donations' | ||
import { useState } from 'react' | ||
|
||
const PREFIX = 'donation-session-' | ||
|
||
type DonationSession = { | ||
values: OneTimeDonation | ||
step: number | ||
} | ||
|
||
function createSessionObject( | ||
campaignSlug: string, | ||
values: OneTimeDonation, | ||
step: number, | ||
): DonationSession { | ||
const donationSession = sessionStorage.getItem(PREFIX + campaignSlug) | ||
//Session already exists do nothing | ||
if (donationSession) return JSON.parse(donationSession) | ||
const sessionObj = { values, step } | ||
sessionStorage.setItem(PREFIX + campaignSlug, JSON.stringify(sessionObj)) | ||
return sessionObj | ||
} | ||
|
||
function updateSessionObject( | ||
campaignSlug: string, | ||
values: OneTimeDonation, | ||
step: number, | ||
): DonationSession { | ||
const donationSession = sessionStorage.getItem(PREFIX + campaignSlug) | ||
if (!donationSession) { | ||
// if session does not exist create new one. | ||
return createSessionObject(campaignSlug, values, step) | ||
} | ||
|
||
const newSessionObj = { values, step } | ||
sessionStorage.setItem(PREFIX + campaignSlug, JSON.stringify(newSessionObj)) | ||
return newSessionObj | ||
} | ||
|
||
function getSessionObject(campaignSlug: string): DonationSession | null { | ||
const donationSession = sessionStorage.getItem(PREFIX + campaignSlug) | ||
if (!donationSession) return null | ||
return JSON.parse(donationSession) | ||
} | ||
|
||
function destroySessionObject(campaignSlug: string): void { | ||
sessionStorage.removeItem(PREFIX + campaignSlug) | ||
} | ||
|
||
export function useDonationStepSession(campaignSlug: string) { | ||
const sessionObj = getSessionObject(campaignSlug) | ||
const [donationSession, setDonationSession] = useState<DonationSession | null>(sessionObj) | ||
|
||
const updateDonationSession = (value: OneTimeDonation, step: number) => { | ||
const newSessionObj = updateSessionObject(campaignSlug, value, step) | ||
setDonationSession(newSessionObj) | ||
} | ||
|
||
const clearDonationSession = () => { | ||
destroySessionObject(campaignSlug) | ||
setDonationSession(null) | ||
} | ||
|
||
return [donationSession, { updateDonationSession, clearDonationSession }] as const | ||
} |
3 changes: 3 additions & 0 deletions
3
src/components/client/one-time-donation/helpers/stepperContext.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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { createContext } from 'react' | ||
import { CampaignResponse } from 'gql/campaigns' | ||
import { OneTimeDonation } from 'gql/donations' | ||
|
||
type Steps = { | ||
step: number | ||
setStep: React.Dispatch<React.SetStateAction<number>> | ||
campaign: CampaignResponse | ||
updateDonationSession: (value: OneTimeDonation, step: number) => void | ||
clearDonationSession: () => void | ||
} | ||
export const StepsContext = createContext<Steps>({} as Steps) |