Skip to content
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

feat(preview): added preview images #1360

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/constants/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const DIFF_QUERY_KEY = "diff"
export const REVIEW_REQUEST_QUERY_KEY = "review-request"
export const COMMENTS_KEY = "comments-content"
export const ALL_SITES_KEY = "all-sites-content"
export const SITE_PREVIEW_KEY = "site-previews"
export const GET_HOMEPAGE_KEY = "get-homepage"
export const GET_NAV_KEY = "get-nav"
export const GET_ALL_COLLECTIONS_KEY = "get-all-collections"
Expand Down
1 change: 1 addition & 0 deletions src/hooks/allSitesHooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./useGetAllSites"
export * from "./useSitePreview"
24 changes: 24 additions & 0 deletions src/hooks/allSitesHooks/useSitePreview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useQuery, UseQueryResult } from "react-query"

import { SITE_PREVIEW_KEY } from "constants/queryKeys"

import * as AllSitesService from "services/AllSitesService"

import { SitePreviewRequest } from "types/sites"

export const useSitePreview = (
userEmail: string,
sites: string[]
): UseQueryResult<SitePreviewRequest[]> => {
return useQuery<SitePreviewRequest[]>(
[SITE_PREVIEW_KEY, userEmail],
() => AllSitesService.getSitePreview(sites),
{
retry: false,
refetchOnWindowFocus: false,
onError: (error) => {
console.log(error)
},
}
)
}
106 changes: 71 additions & 35 deletions src/layouts/Sites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Text,
VStack,
Flex,
Image,
} from "@chakra-ui/react"
import { InlineMessage } from "@opengovsg/design-system-react"
import { AllSitesHeader } from "components/Header/AllSitesHeader"
Expand All @@ -17,7 +18,7 @@ import { LOCAL_STORAGE_KEYS } from "constants/localStorage"

import { useLoginContext } from "contexts/LoginContext"

import { useGetAllSites } from "hooks/allSitesHooks"
import { useGetAllSites, useSitePreview } from "hooks/allSitesHooks"
import { useAnnouncements } from "hooks/useAnnouncement"

import elementStyles from "styles/isomer-cms/Elements.module.scss"
Expand All @@ -29,8 +30,54 @@ import { EmptySitesImage, IsomerLogoNoText } from "assets"
import { AnnouncementModal } from "features/AnnouncementModal/AnnouncementModal"
import { SiteData } from "types/sites"

const SitePreviewImage = ({ imageUrl }: { imageUrl?: string }) => {
return (
<Image
src={imageUrl}
height="50%"
fallback={<IsomerLogoNoText height="50%" />}
/>
)
}

const SitesCard = (
repoName: string,
lastUpdated: string,
urlLink: "dashboard" | "workspace",
imageUrl?: string
) => {
return (
<div className={siteStyles.siteContainer} key={repoName}>
<div className={siteStyles.site}>
<Link to={`/sites/${repoName}/${urlLink}`}>
<Flex w="full" h="70%" justifyContent="center" alignItems="center">
<SitePreviewImage imageUrl={imageUrl} />
</Flex>
<Divider />
<VStack
w="full"
h="30%"
justifyContent="center"
alignItems="start"
backgroundColor="interaction.mainLight.default"
pl="1rem"
borderRadius="0px 0px 4px 4px"
>
<Text fontSize="0.9em">{repoName}</Text>
{lastUpdated && (
<Text fontSize="0.6em" color="base.content.light">
{convertUtcToTimeDiff(lastUpdated)}
</Text>
)}
</VStack>
</Link>
</div>
</div>
)
}

const SitesContent = ({ siteNames }: { siteNames?: SiteData[] }) => {
const { userId } = useLoginContext()
const { userId, email } = useLoginContext()
const isEmailLoginUser = !userId
const urlLink = isEmailLoginUser ? "dashboard" : "workspace"

Expand Down Expand Up @@ -63,41 +110,30 @@ const SitesContent = ({ siteNames }: { siteNames?: SiteData[] }) => {
</VStack>
</VStack>
)
const { data: previews } = useSitePreview(
email,
siteNames.map((site) => site.repoName)
)
if (previews && previews.length === siteNames.length) {
caando marked this conversation as resolved.
Show resolved Hide resolved
const sitesWithPreview = siteNames.map((siteName, index) => {
return {
...siteName,
imageUrl: previews[index].imageUrl,
}
})
return (
<>
{sitesWithPreview.map(({ repoName, lastUpdated, imageUrl }) =>
SitesCard(repoName, lastUpdated, urlLink, imageUrl)
)}
</>
)
}
return (
<>
{siteNames.map((siteName) => (
<div className={siteStyles.siteContainer} key={siteName.repoName}>
<div className={siteStyles.site}>
<Link to={`/sites/${siteName.repoName}/${urlLink}`}>
<Flex
w="full"
h="70%"
justifyContent="center"
alignItems="center"
>
<IsomerLogoNoText height="30%" />
</Flex>
<Divider />
<VStack
w="full"
h="30%"
justifyContent="center"
alignItems="start"
backgroundColor="interaction.mainLight.default"
pl="1rem"
borderRadius="0px 0px 4px 4px"
>
<Text fontSize="0.9em">{siteName.repoName}</Text>
{siteName.lastUpdated && (
<Text fontSize="0.6em" color="base.content.light">
{convertUtcToTimeDiff(siteName.lastUpdated)}
</Text>
)}
</VStack>
</Link>
</div>
</div>
))}
{siteNames.map(({ repoName, lastUpdated }) =>
SitesCard(repoName, lastUpdated, urlLink)
)}
</>
)
}
Expand Down
14 changes: 13 additions & 1 deletion src/services/AllSitesService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { SiteDataRequest } from "types/sites"
import { SiteDataRequest, SitePreviewRequest } from "types/sites"

import { apiService } from "./ApiService"

export const getAllSites = async (): Promise<SiteDataRequest> => {
const endpoint = `/sites`
return apiService.get(endpoint).then((res) => res.data)
}

// This is a post request because the frontend needs to send
// a list of sites to request for which is not doable with GET
// request. However, in the future after migration of users to
// email login, the list of sites can be removed in favor of a
// db query. In that case the query method can be changed to GET.
export const getSitePreview = async (
sites: string[]
): Promise<SitePreviewRequest[]> => {
const endpoint = `/sites/preview`
return apiService.post(endpoint, { sites }).then((res) => res.data)
caando marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 4 additions & 0 deletions src/types/sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ export interface SiteData {
export interface SiteDataRequest {
siteNames: SiteData[]
}

export interface SitePreviewRequest {
imageUrl?: string
}