Repository navigation
PM-3357 - show profile completeness banner #402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,12 @@ | |
| import { getAppContext } from 'lib/app-context'; | ||
| import Toastr from './components/Toastr.svelte'; | ||
| import styles from './Nudge.module.scss'; | ||
| import type { ToastType } from 'lib/config/profile-toasts.config'; | ||
| import { toastsMeta, type ToastType } from 'lib/config/profile-toasts.config'; | ||
| import { getToast, hideToast } from './toast-manager'; | ||
| import Sticky from 'lib/components/sticky/Sticky.svelte'; | ||
| import type { ProfileCompletionData } from 'lib/app-context/profile-completion.model'; | ||
| import { getBanner, hide as hideBanner } from './banner-manager'; | ||
| import Banner from './components/Banner.svelte'; | ||
|
|
||
| const ctx = getAppContext(); | ||
|
|
||
|
|
@@ -16,20 +18,42 @@ | |
| } = $ctx.auth) | ||
|
|
||
| let toast: ToastType | undefined; | ||
| let banner: {key: string, date: Date} | undefined; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
|
|
||
| function dismissToast() { | ||
| toast = undefined; | ||
| hideToast(); | ||
| } | ||
|
|
||
| function dismissBanner() { | ||
| banner = undefined; | ||
| hideBanner(); | ||
| } | ||
|
|
||
| $: toast = isReady ? getToast(profileCompletionData as ProfileCompletionData) : undefined; | ||
| $: banner = isReady ? getBanner(profileCompletionData as ProfileCompletionData) : undefined; | ||
| </script> | ||
|
|
||
| {#if banner} | ||
| <Sticky class={styles.bannerOuter} delayYOffset={92}> | ||
| <div class={styles.bannerWrap}> | ||
| <div class={styles.bannerInner}> | ||
| <Banner | ||
| userHandle={user?.handle ?? ''} | ||
| banner={banner} | ||
| redirect={toastsMeta[banner.key].ctaLink(user?.handle ?? '')} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| on:dismiss={dismissBanner} | ||
| /> | ||
| </div> | ||
| </div> | ||
| </Sticky> | ||
| {/if} | ||
|
|
||
| {#if toast} | ||
| <Sticky class={styles.nudgeOuter} yOffset={10}> | ||
| <div class={styles.nudgeWrap}> | ||
| <div class={styles.nudgeInner}> | ||
| <Toastr userhandle={user.handle} toast={toast} on:dismiss={dismissToast} /> | ||
| <Toastr userhandle={user?.handle ?? ''} toast={toast} on:dismiss={dismissToast} /> | ||
| </div> | ||
| </div> | ||
| </Sticky> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import type { ProfileCompletionData } from 'lib/app-context/profile-completion.model'; | ||
| import { checkCookie, getCookieValue, setCookie } from '../utils/cookies'; | ||
| import { dismissNudgesBasedOnHost } from '../functions/profile-nudges'; | ||
| import { PROFILE_HOST } from 'lib/config'; | ||
|
|
||
| const COOKIE_NAME = 'uni-profilereminder-banner-shown'; | ||
| const COOKIE_ACTIVE_PERIOD_DAYS = 3; | ||
| const PROFILE_UPDATE_REMINDER_PERIOD_DAYS = 3*30; | ||
|
|
||
| const DAY = 24 * 3600 * 1000; | ||
| const _30DAYS = 30 * DAY; | ||
|
|
||
| function isDismissed() { | ||
| return checkCookie(COOKIE_NAME, 'hidden'); | ||
| } | ||
|
|
||
| function getLastSeen() { | ||
| return getCookieValue(COOKIE_NAME); | ||
| } | ||
|
|
||
| const isOlderThanTreshold = (date: Date | number, treshold: number): boolean => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [💡 |
||
| const diffInDays = Math.floor((Date.now() - +date) / DAY); | ||
| return diffInDays >= treshold; | ||
| } | ||
|
|
||
| /** | ||
| * @param completednessData | ||
| * @returns | ||
| */ | ||
| export const getBanner = (completednessData: ProfileCompletionData) => { | ||
| if (dismissNudgesBasedOnHost(PROFILE_HOST) || !completednessData || isDismissed()) { | ||
| return; | ||
| } | ||
|
|
||
| const fields = completednessData.dateFields ?? []; | ||
| const updatableProfileFields: [string, Date][] = fields | ||
| .filter(([k]) => k.endsWith('LastUpdateDate')) | ||
| .map(([k, d]) => [k.replace(/LastUpdateDate$/, ''), d]); | ||
| const lastProfileConfirmationDate = fields.find(([k]) => k === 'lastProfileConfirmationDate')?.[1]; | ||
|
|
||
| const sorted = updatableProfileFields | ||
| .sort((a, b) => +a[1] - +b[1]); | ||
|
|
||
|
|
||
| const lastUpdate = +sorted[sorted.length - 1][1]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| if (!lastUpdate) { | ||
| return; | ||
| } | ||
|
|
||
| const lastUpdateOrCofirmDate = lastProfileConfirmationDate ? Math.max(lastUpdate, +lastProfileConfirmationDate) : lastUpdate; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [💡 |
||
| if (!lastUpdateOrCofirmDate || !isOlderThanTreshold(lastUpdateOrCofirmDate, PROFILE_UPDATE_REMINDER_PERIOD_DAYS)) { | ||
| setCookie(COOKIE_NAME, '', 0); | ||
| return; | ||
| } | ||
|
|
||
| let lastSeen = getLastSeen(); | ||
| if (!lastSeen) { | ||
| const oldFields = updatableProfileFields.filter(([,d]) => isOlderThanTreshold(d, PROFILE_UPDATE_REMINDER_PERIOD_DAYS)).map(([k]) => k); | ||
| const fieldKey = oldFields.length ? oldFields[0] : undefined; | ||
| const field = updatableProfileFields.find(f => f[0] === fieldKey) ?? []; | ||
| lastSeen = JSON.stringify({key: field[0], date: field[1]}); | ||
| if(lastSeen) { | ||
| setCookie(COOKIE_NAME, lastSeen, COOKIE_ACTIVE_PERIOD_DAYS); | ||
| } | ||
| } | ||
|
|
||
| return JSON.parse(lastSeen); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| } | ||
|
|
||
| export const hide = () => { | ||
| setCookie(COOKIE_NAME, 'hidden', COOKIE_ACTIVE_PERIOD_DAYS); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.