-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
33 additions
and
32 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
32 changes: 12 additions & 20 deletions
32
support-frontend/assets/helpers/abTests/sessionStorage.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,38 +1,30 @@ | ||
import * as storage from '../storage/storage'; | ||
import type { Participations } from './models'; | ||
|
||
function getParticipationsFromSession( | ||
key: string = 'abParticipations', | ||
): Participations | undefined { | ||
const PARTICIPATIONS_KEY = 'abParticipations'; | ||
const LANDING_PAGE_PARTICIPATIONS_KEY = 'landingPageParticipations'; | ||
type Key = typeof PARTICIPATIONS_KEY | typeof LANDING_PAGE_PARTICIPATIONS_KEY; | ||
|
||
function getSessionParticipations(key: Key): Participations | undefined { | ||
const participations = storage.getSession(key); | ||
if (participations) { | ||
try { | ||
return JSON.parse(participations) as Participations; | ||
} catch (error) { | ||
console.error( | ||
'Failed to parse abParticipations from session storage', | ||
error, | ||
); | ||
console.error(`Failed to parse ${key} from session storage`, error); | ||
return undefined; | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
const landingPageParticipationsKey = 'landingPageParticipations'; | ||
function getLandingPageParticipationsFromSession(): Participations | undefined { | ||
return getParticipationsFromSession(landingPageParticipationsKey); | ||
} | ||
|
||
function setLandingPageParticipations(participations: Participations) { | ||
storage.setSession( | ||
landingPageParticipationsKey, | ||
JSON.stringify(participations), | ||
); | ||
function setSessionParticipations(participations: Participations, key: Key) { | ||
storage.setSession(key, JSON.stringify(participations)); | ||
} | ||
|
||
export { | ||
getParticipationsFromSession, | ||
getLandingPageParticipationsFromSession, | ||
setLandingPageParticipations, | ||
getSessionParticipations, | ||
setSessionParticipations, | ||
PARTICIPATIONS_KEY, | ||
LANDING_PAGE_PARTICIPATIONS_KEY, | ||
}; |