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

Commit 39a7149

Browse files
committed
Add the setup for the posts Redux store
1 parent 4301863 commit 39a7149

File tree

7 files changed

+103
-2
lines changed

7 files changed

+103
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { getPosts, createPost, updatePost, deletePost } from './posts'
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import axios from 'axios'
2+
3+
import { postActions as actions } from 'store/actions'
4+
import { makeRequest } from 'store/action-creators/requests'
5+
6+
export const getPosts = () => async dispatch => {
7+
const response = await dispatch(
8+
makeRequest('get-posts', () => axios.get(`/api/posts`))
9+
)
10+
11+
dispatch({
12+
type: actions.ADD_POSTS,
13+
posts: response.data.data
14+
})
15+
}
16+
17+
export const createPost = data => async dispatch => {
18+
const response = await dispatch(
19+
makeRequest('create-post', () => axios.post(`/api/posts`, data))
20+
)
21+
22+
dispatch({
23+
type: actions.ADD_POST,
24+
posts: response.data.data
25+
})
26+
}
27+
28+
export const updatePost = data => async dispatch => {
29+
const response = await dispatch(
30+
makeRequest(`update-post-${data.id}`, () =>
31+
axios.put(`/api/posts/${data.id}`, data)
32+
)
33+
)
34+
35+
dispatch({
36+
type: actions.UPDATE_POST,
37+
posts: response.data.data
38+
})
39+
}
40+
41+
export const deletePost = slug => async dispatch => {
42+
await dispatch(
43+
makeRequest(`update-post-${slug}`, () => axios.delete(`/api/posts/${slug}`))
44+
)
45+
46+
dispatch({
47+
type: actions.DELETE_POST,
48+
slug
49+
})
50+
}

resources/assets/js/store/actions.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ export const userActions = {
1717
SET_CURRENT_USER_INFO: 'USER/SET_CURRENT_USER_INFO',
1818
SET_AVATAR: 'USER/SET_AVATAR'
1919
}
20+
21+
export const postActions = {
22+
ADD_POST: 'ADD_POST',
23+
UPDATE_POST: 'UPDATE_POST',
24+
DELETE_POST: 'DELETE_POST',
25+
ADD_POSTS: 'ADD_POSTS'
26+
}

resources/assets/js/store/initialState.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const initialState = {
22
entities: {
3-
users: {}
3+
users: {},
4+
posts: {}
45
},
56
session: {
67
currentUser: null
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { postActions } from 'store/actions'
2+
import { initialState } from 'store/initialState'
3+
import { post as postSchema } from 'store/schemas'
4+
import { createReducer, normalizeAndMerge } from 'store/reducers/utilities'
5+
6+
const {
7+
entities: { posts: postsState }
8+
} = initialState
9+
10+
const deletePost = (state, { slug }) => {
11+
const newState = { ...state }
12+
13+
delete newState[slug]
14+
15+
return newState
16+
}
17+
18+
export const postsReducer = createReducer(postsState, {
19+
[postActions.ADD_POST]: normalizeAndMerge('posts', postSchema, {
20+
singular: true
21+
}),
22+
[postActions.ADD_POSTS]: normalizeAndMerge('posts', postSchema),
23+
[postActions.UPDATE_POST]: normalizeAndMerge('posts', postSchema, {
24+
singular: true
25+
}),
26+
[postActions.DELETE_POST]: deletePost
27+
})

resources/assets/js/store/schemas.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { schema } from 'normalizr'
22

33
export const user = new schema.Entity('users')
4+
export const post = new schema.Entity('posts')
45

56
export const entities = {
6-
users: [user]
7+
users: [user],
8+
posts: [post]
79
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { denormalize } from 'normalizr'
2+
3+
import { entities as entitiesSchema } from 'store/schemas'
4+
5+
export const selectAllPosts = state => {
6+
const dnEntities = denormalize(
7+
{ posts: Object.keys(state.entities.posts) },
8+
entitiesSchema,
9+
state.entities
10+
)
11+
12+
return dnEntities.posts
13+
}

0 commit comments

Comments
 (0)