Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit e45d636

Browse files
committed
Add posts table to overview
1 parent 39a7149 commit e45d636

File tree

1 file changed

+147
-5
lines changed

1 file changed

+147
-5
lines changed

resources/assets/js/pages/Overview/Overview.jsx

Lines changed: 147 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,147 @@
1-
import React from 'react'
1+
import { compose } from 'recompose'
2+
import { reduxForm, Field } from 'redux-form'
3+
import { connect } from 'react-redux'
4+
import React, { useEffect } from 'react'
25

3-
import { NeutralButton } from 'components'
46
import { ModalConsumer } from 'contexts'
7+
import { NeutralButton, TextInput, TextArea } from 'components'
8+
import { selectAllPosts } from 'store/selectors/posts'
9+
import {
10+
getPosts as getPostsAction,
11+
updatePost as updatePostAction,
12+
createPost as createPostAction,
13+
deletePost as deletePostAction
14+
} from 'store/action-creators/posts'
515

6-
const OverviewComponent = props => {
16+
const PostModalComponent = ({ onSubmit, handleSubmit, initialValues }) => {
17+
return (
18+
<div>
19+
<h2 className="mb-2">{initialValues ? 'Edit' : 'Add'} post</h2>
20+
<form onSubmit={handleSubmit(onSubmit)}>
21+
<Field
22+
className="mb-2"
23+
name="title"
24+
placeholder="title"
25+
component={TextInput}
26+
/>
27+
<Field
28+
className="mb-2"
29+
name="body"
30+
placeholder="body"
31+
component={TextArea}
32+
/>
33+
<NeutralButton type="submit">
34+
{initialValues ? 'Edit' : 'Add'} Post
35+
</NeutralButton>
36+
</form>
37+
</div>
38+
)
39+
}
40+
41+
const CreatePostModal = compose(
42+
connect(
43+
null,
44+
(dispatch, { hideModal }) => ({
45+
onSubmit: values => {
46+
dispatch(createPostAction(values))
47+
hideModal()
48+
}
49+
})
50+
),
51+
reduxForm({
52+
form: 'add-post'
53+
})
54+
)(PostModalComponent)
55+
56+
const UpdatePostModal = compose(
57+
connect(
58+
(state, ownProps) => ({
59+
initialValues: ownProps
60+
}),
61+
(dispatch, { hideModal }) => ({
62+
onSubmit: values => {
63+
dispatch(updatePostAction(values))
64+
hideModal()
65+
}
66+
})
67+
),
68+
reduxForm({
69+
form: 'update-post'
70+
})
71+
)(PostModalComponent)
72+
73+
const OverviewComponent = ({ getPosts, deletePost, posts }) => {
774
const ModalExample = props => <div>{props.message}</div>
75+
76+
const populatePosts = async () => {
77+
await getPosts()
78+
}
79+
80+
useEffect(() => {
81+
populatePosts()
82+
}, [])
83+
884
return (
985
<div>
10-
Put your initial dashboard page here
86+
Put your initial dashboard page here. This branch contains a CRUD example
87+
using a "Post" as a dummy example model. You can play around with that
88+
below and read through the code on{' '}
89+
<a
90+
target="_blank"
91+
href="https://github.com/huwcarwyn/react-laravel-boilerplate/tree/crud-example"
92+
>
93+
This branch
94+
</a>
95+
<div className="py-6">
96+
{posts.length > 0 && (
97+
<table className="w-full text-left mb-4">
98+
<thead>
99+
<tr>
100+
<th>id</th>
101+
<th>title</th>
102+
<th>body</th>
103+
</tr>
104+
</thead>
105+
<tbody>
106+
{posts.map(({ id, title, body }) => (
107+
<tr key={id}>
108+
<td>{id}</td>
109+
<td>{title}</td>
110+
<td>{body}</td>
111+
<td>
112+
<span
113+
className="text-red inline-block mr-6"
114+
onClick={() => deletePost(id)}
115+
>
116+
delete
117+
</span>
118+
<ModalConsumer>
119+
{({ showModal }) => (
120+
<span
121+
onClick={() =>
122+
showModal(UpdatePostModal, { id, title, body })
123+
}
124+
className="text-green"
125+
>
126+
edit
127+
</span>
128+
)}
129+
</ModalConsumer>
130+
</td>
131+
</tr>
132+
))}
133+
</tbody>
134+
</table>
135+
)}
136+
137+
<ModalConsumer>
138+
{({ showModal }) => (
139+
<NeutralButton onClick={() => showModal(CreatePostModal)}>
140+
Create Post
141+
</NeutralButton>
142+
)}
143+
</ModalConsumer>
144+
</div>
11145
<div className="mt-4">
12146
<ModalConsumer>
13147
{({ showModal }) => (
@@ -27,4 +161,12 @@ const OverviewComponent = props => {
27161
)
28162
}
29163

30-
export default OverviewComponent
164+
export default connect(
165+
state => ({
166+
posts: selectAllPosts(state)
167+
}),
168+
dispatch => ({
169+
getPosts: () => dispatch(getPostsAction()),
170+
deletePost: id => dispatch(deletePostAction(id))
171+
})
172+
)(OverviewComponent)

0 commit comments

Comments
 (0)