Skip to content

Commit 52ca240

Browse files
committed
refactor(code-cleanup): post card view update
1 parent efe3bb3 commit 52ca240

File tree

9 files changed

+78
-59
lines changed

9 files changed

+78
-59
lines changed

.husky/pre-commit

100755100644
File mode changed.

cypress/integration/posts/posts.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ describe('Feature - Posts', () => {
77

88
cy.findByRole('button', { name: /add new post/i }).click()
99

10-
cy.findByRole('heading', { name: /hello from cypress!/i }).should('exist')
10+
cy.findByText(/hello from cypress!/i ).should('exist')
1111
})
1212
})

src/features/posts/api/__tests__/post.api.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getPosts } from 'features/posts/api/index'
2-
import fixture from 'test/msw/db.initial.data.json'
2+
import fixture from 'test/msw/fixtures/db.initial.data.json'
33

44
describe('Post API test - MSW example', () => {
55
it('should fetch all Posts', async () => {
Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
import { Button, Card, CardActions, CardContent, CardMedia } from '@mui/material'
2-
import Typography from '@mui/material/Typography'
3-
import React from 'react'
4-
import { useTranslation } from 'react-i18next'
1+
import DeleteForeverIcon from "@mui/icons-material/DeleteForever";
2+
import EditIcon from "@mui/icons-material/Edit";
3+
import { Grid, IconButton, Paper } from "@mui/material";
4+
import React from "react";
55

6-
import logoImage from 'features/posts/assets/imgs/logo192.png'
7-
import { Post } from 'features/posts/types'
6+
7+
import { Post } from "features/posts/types";
8+
9+
const styles = {
10+
Icon: {
11+
marginLeft: "auto"
12+
},
13+
Paper: {
14+
margin: "auto",
15+
padding: 10,
16+
display: "flex",
17+
alignItems: "center",
18+
marginTop: 10
19+
}
20+
};
821

922
export type PostCardViewProps = {
1023
post: Post
@@ -13,35 +26,43 @@ export type PostCardViewProps = {
1326
}
1427

1528
export const PostCardView = (props: PostCardViewProps) => {
16-
const { t } = useTranslation()
1729

18-
const { post, onDeleteClick, onUpdateClick } = props
30+
const { post, onDeleteClick, onUpdateClick } = props;
1931

2032
const handleDeleteClick = () => {
21-
onDeleteClick(post)
22-
}
33+
onDeleteClick(post);
34+
};
2335

24-
const handleUpdateClick = () => onUpdateClick(post)
36+
const handleUpdateClick = () => onUpdateClick(post);
2537

2638
return (
2739
<>
28-
<Card sx={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
29-
<CardMedia component="img" src={logoImage} alt="random" />
30-
<CardContent sx={{ flexGrow: 1 }}>
31-
<Typography gutterBottom variant="h5" component="h2">
32-
{post.title}
33-
</Typography>
34-
<Typography>{post.body}</Typography>
35-
</CardContent>
36-
<CardActions>
37-
<Button size="small" onClick={handleDeleteClick}>
38-
{t('home.buttons.delete')}
39-
</Button>
40-
<Button size="small" onClick={handleUpdateClick}>
41-
{t('home.buttons.update')}
42-
</Button>
43-
</CardActions>
44-
</Card>
40+
<Grid
41+
spacing={2}
42+
xs={12}
43+
item
44+
key={post.id}
45+
>
46+
<Paper elevation={2} style={styles.Paper}>
47+
<span><strong>{post.title}</strong> - {post.body}</span>
48+
<br />
49+
<IconButton
50+
color="primary"
51+
aria-label="Edit"
52+
style={styles.Icon}
53+
onClick={handleUpdateClick}
54+
>
55+
<EditIcon />
56+
</IconButton>
57+
<IconButton
58+
color="secondary"
59+
aria-label="Delete"
60+
onClick={handleDeleteClick}
61+
>
62+
<DeleteForeverIcon />
63+
</IconButton>
64+
</Paper>
65+
</Grid>
4566
</>
46-
)
47-
}
67+
);
68+
};

src/features/posts/components/PostForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const PostForm = (props: PostFormProps) => {
3939
const { handleSubmit, reset, control } = methods
4040

4141
return (
42-
<Stack sx={{ pt: 4 }} direction="column" spacing={1} justifyContent="center">
42+
<Stack sx={{ pt: 0 }} direction="column" spacing={1} justifyContent="center">
4343
<FormTextField name="title" label={t('home.form.title')} control={control} />
4444
<FormTextField name="body" label={t('home.form.body')} control={control} />
4545
<Button onClick={handleSubmit(onSubmitClick)} variant={'contained'}>
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Grid } from '@mui/material'
2-
import React from 'react'
1+
import { Grid } from "@mui/material";
2+
import React from "react";
33

4-
import { PostCardView } from 'features/posts/components/PostCardView'
4+
import { PostCardView } from "features/posts/components/PostCardView";
55

6-
import { Post } from '../types'
6+
import { Post } from "../types";
77

88
export type PostListProps = {
99
posts: Post[]
@@ -12,17 +12,15 @@ export type PostListProps = {
1212
}
1313

1414
export const PostList = (props: PostListProps) => {
15-
const { posts, onDeletePost, onUpdatePost } = props
15+
const { posts, onDeletePost, onUpdatePost } = props;
1616

1717
return (
1818
<>
19-
<Grid container spacing={4}>
19+
<Grid container>
2020
{posts.map(post => (
21-
<Grid item key={post.id} xs={12} sm={6} md={4}>
22-
<PostCardView post={post} onDeleteClick={onDeletePost} onUpdateClick={onUpdatePost} />
23-
</Grid>
21+
<PostCardView data-test-id={post.title} key={post.id} post={post} onDeleteClick={onDeletePost} onUpdateClick={onUpdatePost} />
2422
))}
2523
</Grid>
2624
</>
27-
)
28-
}
25+
);
26+
};

src/test/msw/db.initial.data.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/test/msw/db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { factory, primaryKey } from '@mswjs/data'
22
import { ModelDictionary } from '@mswjs/data/lib/glossary'
33

44
import Env from 'config/Env'
5-
import initialMockedDb from 'test/msw/db.initial.data.json'
5+
import initialMockedDb from 'test/msw/fixtures/db.initial.data.json'
66

77
const models: ModelDictionary = {
88
posts: {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"posts": [
3+
{
4+
"id": "v-UDRFY8Vqil3MXP0UPyE",
5+
"title": "Redux-Saga",
6+
"body": "An intuitive Redux side effect manager."
7+
},
8+
{
9+
"id": "fxhwAGn0MGcQzbccTTIs5",
10+
"title": "Redux Toolkit",
11+
"body": "The official, opinionated, batteries-included toolset for efficient Redux development."
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)