Skip to content

Commit

Permalink
Summary adjustments.
Browse files Browse the repository at this point in the history
  • Loading branch information
soup-bowl committed Oct 2, 2024
1 parent d031615 commit 03c7b56
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
25 changes: 21 additions & 4 deletions src/components/PostGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import {
IonAvatar,
IonCard,
IonCardContent,
IonCardHeader,
IonCardTitle,
IonChip,
IonCol,
IonGrid,
IonLabel,
IonRow,
IonSkeletonText,
} from "@ionic/react"
import { IPost } from "@/api"
import { degubbins } from "@/utils"
import { degubbins, isPostExtended } from "@/utils"
import { IPostExtended } from "@/interface"

const PostGrid: React.FC<{
posts?: IPost[]
posts?: IPost[] | IPostExtended[]
siteURL?: string
mockCount?: number
}> = ({ posts, siteURL, mockCount = 0 }) => {
Expand Down Expand Up @@ -42,7 +46,7 @@ const PostGrid: React.FC<{
<IonRow>
{posts.map((post, index) => (
<IonCol key={index} size="12" sizeMd="4">
<IonCard routerLink={`/${siteURL}/${post.type}/${post.id}`}>
<IonCard routerLink={`/${isPostExtended(post) ? post.url : siteURL}/${post.type}/${post.id}`}>
{post._embedded?.["wp:featuredmedia"]?.[0].media_details?.sizes?.full?.source_url && (
<img
alt=""
Expand All @@ -52,7 +56,20 @@ const PostGrid: React.FC<{
<IonCardHeader>
<IonCardTitle>{degubbins(post.title.rendered)}</IonCardTitle>
</IonCardHeader>
<IonCardContent>{degubbins(post.excerpt.rendered)}</IonCardContent>
<IonCardContent>
{isPostExtended(post) && (
<>
<IonChip>
<IonAvatar>
<img alt="" src={post.image} />
</IonAvatar>
<IonLabel>{post.name}</IonLabel>
</IonChip>
<br />
</>
)}
{degubbins(post.excerpt.rendered)}
</IonCardContent>
</IonCard>
</IonCol>
))}
Expand Down
21 changes: 17 additions & 4 deletions src/components/PostList.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { IonItem, IonLabel, IonList, IonSkeletonText, IonThumbnail } from "@ionic/react"
import { IonAvatar, IonChip, IonItem, IonLabel, IonList, IonSkeletonText, IonThumbnail } from "@ionic/react"
import { IPost } from "@/api"
import { degubbins } from "@/utils"
import { degubbins, isPostExtended } from "@/utils"
import { IPostExtended } from "@/interface"

const PostList: React.FC<{
posts?: IPost[]
posts?: IPost[] | IPostExtended[]
siteURL?: string
mockCount?: number
}> = ({ posts, siteURL, mockCount = 0 }) => {
Expand All @@ -27,7 +28,11 @@ const PostList: React.FC<{
return (
<IonList>
{posts.map((post, index) => (
<IonItem key={index} button routerLink={`/${siteURL}/${post.type}/${post.id}`}>
<IonItem
key={index}
button
routerLink={`/${isPostExtended(post) ? post.url : siteURL}/${post.type}/${post.id}`}
>
{post._embedded?.["wp:featuredmedia"]?.[0].media_details?.sizes?.full?.source_url && (
<IonThumbnail slot="start">
<img
Expand All @@ -40,6 +45,14 @@ const PostList: React.FC<{
<h2>{degubbins(post.title.rendered)}</h2>
<p>{degubbins(post.excerpt.rendered)}</p>
</IonLabel>
{isPostExtended(post) && (
<IonChip slot="end">
<IonAvatar>
<img alt="" src={post.image} />
</IonAvatar>
<IonLabel>{post.name}</IonLabel>
</IonChip>
)}
</IonItem>
))}
</IonList>
Expand Down
28 changes: 20 additions & 8 deletions src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {
} from "@ionic/react"
import { Placeholder, PostGrid, PostList } from "@/components"
import { useSettings } from "@/hooks"
import { ISite } from "@/interface"
import { IPostExtended, ISite } from "@/interface"
import { useQuery } from "@tanstack/react-query"
import { EPostType, fetchPosts, fetchSiteInfo, IPost, WordPressApi } from "@/api"
import { EPostType, fetchPosts, IPost, WordPressApi } from "@/api"
import { getLayoutIcon } from "@/utils"

const Home: React.FC = () => {
Expand Down Expand Up @@ -54,18 +54,30 @@ const Home: React.FC = () => {
)
}

const postData = useQuery<IPost[]>({
const postData = useQuery<IPostExtended[]>({
queryKey: ["summary", sites.length],
queryFn: async () => {
const extendPosts = (posts: IPost[], info: ISite): IPostExtended[] => {
return posts.map((post) => ({
...post,
name: info.name,
description: info.description,
url: info.url,
image: info.image,
}))
}

const postCollectionPromises = sites.map(async (site) => {
const wp = new WordPressApi({ endpoint: `https://${site.url}/wp-json` })
//const details = await fetchSiteInfo(wp)
const posts = await fetchPosts(wp, EPostType.Post, 1, 10)
const posts = extendPosts(await fetchPosts(wp, EPostType.Post, 1, 10), site)
return posts
})

const postCollections = await Promise.all(postCollectionPromises)
return postCollections.flat()
return postCollections
.flat()
.sort((a, b) => new Date(b.created).getTime() - new Date(a.created).getTime())
.slice(0, 15)
},
})

Expand All @@ -87,9 +99,9 @@ const Home: React.FC = () => {

<IonContent fullscreen>
{layout === "grid" ? (
<PostGrid posts={postData.data} siteURL={""} mockCount={10} />
<PostGrid posts={postData.data} mockCount={10} />
) : (
<PostList posts={postData.data} siteURL={""} mockCount={10} />
<PostList posts={postData.data} mockCount={10} />
)}
</IonContent>
</IonPage>
Expand Down
7 changes: 7 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
import { IPost } from "@/api"
import { IPostExtended } from "@/interface"

export { degubbins, getLayoutIcon } from "./stringUtils"

export const isPostExtended = (post: IPost | IPostExtended): post is IPostExtended => {
return (post as IPostExtended).image !== undefined
}

0 comments on commit 03c7b56

Please sign in to comment.