-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pulling admin banner from other apps
- Loading branch information
Showing
15 changed files
with
378 additions
and
3 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
services/ui-src/src/components/banners/AdminBannerProvider.tsx
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,129 @@ | ||
import { createContext, ReactNode, useMemo, useEffect } from "react"; | ||
// utils | ||
import { AdminBannerData, AdminBannerShape } from "types/banners"; | ||
import { bannerId } from "../../constants"; | ||
import { bannerErrors } from "verbiage/errors"; | ||
// api | ||
import { | ||
checkDateRangeStatus, | ||
deleteBanner, | ||
getBanner, | ||
useStore, | ||
writeBanner, | ||
} from "utils"; | ||
|
||
const ADMIN_BANNER_ID = bannerId; | ||
|
||
export const AdminBannerContext = createContext<AdminBannerShape>({ | ||
fetchAdminBanner: Function, | ||
writeAdminBanner: Function, | ||
deleteAdminBanner: Function, | ||
}); | ||
|
||
export const AdminBannerProvider = ({ children }: Props) => { | ||
// state management | ||
const { | ||
bannerData, | ||
setBannerData, | ||
bannerActive, | ||
setBannerActive, | ||
bannerLoading, | ||
setBannerLoading, | ||
bannerErrorMessage, | ||
setBannerErrorMessage, | ||
bannerDeleting, | ||
setBannerDeleting, | ||
} = useStore(); | ||
|
||
const fetchAdminBanner = async () => { | ||
setBannerLoading(true); | ||
try { | ||
const currentBanner = await getBanner(ADMIN_BANNER_ID); | ||
const newBannerData = currentBanner as AdminBannerData | undefined; | ||
setBannerData(newBannerData); | ||
setBannerErrorMessage(undefined); | ||
} catch (e: any) { | ||
// 404 expected when no current banner exists | ||
if (!e.toString().includes("404")) { | ||
setBannerErrorMessage(bannerErrors.GET_BANNER_FAILED); | ||
} | ||
} | ||
setBannerLoading(false); | ||
}; | ||
|
||
const deleteAdminBanner = async () => { | ||
setBannerDeleting(true); | ||
try { | ||
await deleteBanner(ADMIN_BANNER_ID); | ||
await fetchAdminBanner(); | ||
} catch { | ||
setBannerErrorMessage(bannerErrors.DELETE_BANNER_FAILED); | ||
} | ||
setBannerDeleting(false); | ||
}; | ||
|
||
const writeAdminBanner = async (newBannerData: AdminBannerData) => { | ||
try { | ||
await writeBanner(newBannerData); | ||
} catch { | ||
setBannerErrorMessage(bannerErrors.CREATE_BANNER_FAILED); | ||
} | ||
await fetchAdminBanner(); | ||
}; | ||
|
||
useEffect(() => { | ||
let bannerActivity = false; | ||
if (bannerData) { | ||
bannerActivity = checkDateRangeStatus( | ||
bannerData.startDate, | ||
bannerData.endDate | ||
); | ||
} | ||
setBannerActive(bannerActivity); | ||
}, [bannerData]); | ||
|
||
useEffect(() => { | ||
fetchAdminBanner(); | ||
}, []); | ||
|
||
const providerValue = useMemo( | ||
() => ({ | ||
// Banner Data | ||
bannerData, | ||
setBannerData, | ||
// Banner showing | ||
bannerActive, | ||
setBannerActive, | ||
// Banner Loading | ||
bannerLoading, | ||
setBannerLoading, | ||
// Banner Error State | ||
bannerErrorMessage, | ||
setBannerErrorMessage, | ||
// Banner Deleting State | ||
bannerDeleting, | ||
setBannerDeleting, | ||
// Banner API calls | ||
fetchAdminBanner, | ||
writeAdminBanner, | ||
deleteAdminBanner, | ||
}), | ||
[ | ||
bannerData, | ||
bannerActive, | ||
bannerLoading, | ||
bannerErrorMessage, | ||
bannerDeleting, | ||
] | ||
); | ||
|
||
return ( | ||
<AdminBannerContext.Provider value={providerValue}> | ||
{children} | ||
</AdminBannerContext.Provider> | ||
); | ||
}; | ||
|
||
interface Props { | ||
children?: ReactNode; | ||
} |
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,20 @@ | ||
// components | ||
import { Alert } from "components"; | ||
// types | ||
import { BannerData } from "types"; | ||
|
||
export const Banner = ({ bannerData, ...props }: Props) => { | ||
if (bannerData) { | ||
const { title, description, link } = bannerData; | ||
return ( | ||
bannerData && ( | ||
<Alert title={title} description={description} link={link} {...props} /> | ||
) | ||
); | ||
} else return <></>; | ||
}; | ||
|
||
interface Props { | ||
bannerData: BannerData | undefined; | ||
[key: string]: any; | ||
} |
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,22 @@ | ||
import { useFormContext } from "react-hook-form"; | ||
// components | ||
import { Banner } from "components"; | ||
|
||
export const PreviewBanner = ({ ...props }: Props) => { | ||
// get the form context | ||
const form = useFormContext(); | ||
|
||
// set banner preview data | ||
const formData = form.getValues(); | ||
const bannerData = { | ||
title: formData["bannerTitle"] || "New banner title", | ||
description: formData["bannerDescription"] || "New banner description", | ||
link: formData["bannerLink"] || "", | ||
}; | ||
|
||
return <Banner bannerData={bannerData} {...props} />; | ||
}; | ||
|
||
interface Props { | ||
[key: string]: any; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// BANNER | ||
|
||
export interface BannerData { | ||
title: string; | ||
description: string; | ||
link?: string; | ||
[key: string]: any; | ||
} | ||
|
||
export interface AdminBannerData extends BannerData { | ||
key: string; | ||
startDate: number; | ||
endDate: number; | ||
isActive?: boolean; | ||
} | ||
|
||
export interface AdminBannerMethods { | ||
fetchAdminBanner: Function; | ||
writeAdminBanner: Function; | ||
deleteAdminBanner: Function; | ||
} | ||
|
||
export interface AdminBannerShape extends AdminBannerMethods {} |
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,3 +1,4 @@ | ||
export * from "./banners"; | ||
export * from "./users"; | ||
export * from "./states"; | ||
export * from "./other"; | ||
|
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
31 changes: 31 additions & 0 deletions
31
services/ui-src/src/utils/api/requestMethods/banner.test.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,31 @@ | ||
import { getBanner, writeBanner, deleteBanner } from "./banner"; | ||
// utils | ||
import { bannerId } from "../../../constants"; | ||
import { mockBannerData } from "utils/testing/setupJest"; | ||
import { initAuthManager } from "utils/auth/authLifecycle"; | ||
|
||
describe("utils/banner", () => { | ||
beforeEach(async () => { | ||
jest.useFakeTimers(); | ||
initAuthManager(); | ||
jest.runAllTimers(); | ||
}); | ||
|
||
describe("getBanner()", () => { | ||
test("executes", () => { | ||
expect(getBanner(bannerId)).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe("writeBanner()", () => { | ||
test("executes", () => { | ||
expect(writeBanner(mockBannerData)).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe("deleteBanner()", () => { | ||
test("executes", () => { | ||
expect(deleteBanner(bannerId)).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
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 { API } from "aws-amplify"; | ||
import { getRequestHeaders } from "./getRequestHeaders"; | ||
import { AdminBannerData } from "types/banners"; | ||
import { updateTimeout } from "utils"; | ||
|
||
async function getBanner(bannerKey: string) { | ||
const requestHeaders = await getRequestHeaders(); | ||
const request = { | ||
headers: { ...requestHeaders }, | ||
}; | ||
|
||
updateTimeout(); | ||
const response = await API.get("mfp", `/banners/${bannerKey}`, request); | ||
return response; | ||
} | ||
|
||
async function writeBanner(bannerData: AdminBannerData) { | ||
const requestHeaders = await getRequestHeaders(); | ||
const request = { | ||
headers: { ...requestHeaders }, | ||
body: bannerData, | ||
}; | ||
|
||
updateTimeout(); | ||
const response = await API.post("mfp", `/banners/${bannerData.key}`, request); | ||
return response; | ||
} | ||
|
||
async function deleteBanner(bannerKey: string) { | ||
const requestHeaders = await getRequestHeaders(); | ||
const request = { | ||
headers: { ...requestHeaders }, | ||
}; | ||
|
||
updateTimeout(); | ||
const response = await API.del("mfp", `/banners/${bannerKey}`, request); | ||
return response; | ||
} | ||
|
||
export { getBanner, writeBanner, deleteBanner }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { bannerId } from "../../constants"; | ||
|
||
export const mockBannerData = { | ||
key: bannerId, | ||
title: "Yes here I am, a banner", | ||
description: "I have a description too thank you very much", | ||
startDate: 1640995200000, // 1/1/2022 00:00:00 UTC | ||
endDate: 1672531199000, // 12/31/2022 23:59:59 UTC | ||
}; |
Oops, something went wrong.