Skip to content

Commit

Permalink
Position PostDetail properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Matushl committed Dec 13, 2023
1 parent a761be4 commit 2dbb890
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 37 deletions.
27 changes: 4 additions & 23 deletions src/components/Posts/Post.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Stack, Typography} from '@mui/material'
import {Grid, Stack, Typography} from '@mui/material'

Check warning on line 1 in src/components/Posts/Post.tsx

View workflow job for this annotation

GitHub Actions / branch-test

'Grid' is defined but never used
import {FC} from 'react'

import {formatDate} from '@/utils/formatDate'
import {useSeminarInfo} from '@/utils/useSeminarInfo'

import {Button} from '../Clickable/Button'
import {Link} from '../Clickable/Link'
import {PostMarkdown} from './PostMarkdown'
import {PostDetail} from './PostDetail'

Check warning on line 9 in src/components/Posts/Post.tsx

View workflow job for this annotation

GitHub Actions / branch-test

'PostDetail' is defined but never used

export interface IPost {
id: number
Expand All @@ -18,17 +18,16 @@ export interface IPost {
visible_after: string
visible_until: string
sites: number[]
isDetailOpen: boolean
openDetail: () => void
}

export const Post: FC<IPost> = ({caption, short_text, links, details, sites, added_at, isDetailOpen, openDetail}) => {
export const Post: FC<IPost> = ({caption, short_text, links, details, sites, added_at, openDetail}) => {
const {seminarId} = useSeminarInfo()

if (!sites.includes(seminarId)) return null

return (
<Stack sx={{position: 'relative'}}>
<Stack>
<Typography variant="postTitle">{caption}</Typography>

<Typography variant="postBody">{short_text}</Typography>
Expand All @@ -51,24 +50,6 @@ export const Post: FC<IPost> = ({caption, short_text, links, details, sites, add
{formatDate(added_at)}
</Typography>
</Stack>

{isDetailOpen && (
<Stack
p={2}
sx={{
position: 'absolute',
left: '25vw',
width: '30vw',
border: '1rem solid black',
backgroundColor: 'white',
}}
>
<Typography variant="postTitle" textTransform="uppercase" fontStyle="italic" mb={4}>
{caption}
</Typography>
<PostMarkdown content={details} />
</Stack>
)}
</Stack>
)
}
28 changes: 28 additions & 0 deletions src/components/Posts/PostDetail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {Stack, Typography} from '@mui/material'
import {FC} from 'react'

import {PostMarkdown} from './PostMarkdown'

export interface IPost {
caption: string
details: string
}

export const PostDetail: FC<IPost> = ({caption, details}) => {
return (
<Stack
p={2}
sx={{
border: '1rem solid black',
backgroundColor: 'white',
maxHeight: '60vh',
overflow: 'scroll',
}}
>
<Typography variant="postTitle" textTransform="uppercase" fontStyle="italic" mb={4}>
{caption}
</Typography>
<PostMarkdown content={details} />
</Stack>
)
}
44 changes: 31 additions & 13 deletions src/components/Posts/Posts.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {Stack, Typography} from '@mui/material'
import {Grid, Stack, Typography} from '@mui/material'
import {useQuery} from '@tanstack/react-query'
import axios from 'axios'
import {FC, useState} from 'react'
import {FC, Fragment, useRef, useState} from 'react'

Check warning on line 4 in src/components/Posts/Posts.tsx

View workflow job for this annotation

GitHub Actions / branch-test

'Fragment' is defined but never used

Check warning on line 4 in src/components/Posts/Posts.tsx

View workflow job for this annotation

GitHub Actions / branch-test

'useRef' is defined but never used

import {Loading} from '../Loading/Loading'
import {IPost, Post} from './Post'
import {PostDetail} from './PostDetail'

export const Posts: FC = () => {
const {
Expand All @@ -17,22 +18,39 @@ export const Posts: FC = () => {
})
const posts = postsData?.data ?? []

const [activePostDetailId, setActivePostDetailId] = useState<number | null>(null)
const [activePostDetailIndex, setActivePostDetailIndex] = useState<number | undefined>(undefined)

if (postsIsLoading) return <Loading />

if (postsError) return <Typography>{postsError.message}</Typography>

return (
<Stack gap={5} mb={10}>
{posts.map((post) => (
<Post
key={post.id}
{...post}
isDetailOpen={post.id === activePostDetailId}
openDetail={() => setActivePostDetailId(post.id)}
/>
))}
</Stack>
<>
{activePostDetailIndex !== 0 && (
<Grid display="grid" gridTemplateColumns="repeat(12, 1fr)" gap={5} mb={5}>
<Stack gap={5} gridColumn="span 4">
{posts.slice(0, activePostDetailIndex).map((post, index) => (
<Post key={post.id} {...post} openDetail={() => setActivePostDetailIndex(index)} />
))}
</Stack>
</Grid>
)}
{activePostDetailIndex !== undefined && (
<Grid display="grid" gridTemplateColumns="repeat(12, 1fr)" gap={5}>
<Stack gap={5} gridColumn="span 4">
{posts.slice(activePostDetailIndex).map((post, index) => (
<Post
key={post.id}
{...post}
openDetail={() => setActivePostDetailIndex(activePostDetailIndex + index)}
/>
))}
</Stack>
<Stack gridColumn="span 5">
<PostDetail caption={posts[activePostDetailIndex].caption} details={posts[activePostDetailIndex].details} />
</Stack>
</Grid>
)}
</>
)
}
2 changes: 1 addition & 1 deletion src/pages/strom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {PageLayout} from '@/components/PageLayout/PageLayout'
import {Posts} from '@/components/Posts/Posts'

const Strom: NextPage = () => (
<PageLayout title="Novinky" contentWidth={1}>
<PageLayout title="Novinky" contentWidth={3}>
<Posts />
</PageLayout>
)
Expand Down

0 comments on commit 2dbb890

Please sign in to comment.