Skip to content

Commit ee86a72

Browse files
markeriksontimdorr
authored andcommitted
Add a listener for "post added" toasts
# Conflicts: # src/features/posts/postsSlice.ts # Conflicts: # src/features/posts/postsSlice.ts # Conflicts: # src/features/posts/postsSlice.ts
1 parent 81f761b commit ee86a72

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/app/listenerMiddleware.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { createListenerMiddleware, addListener } from '@reduxjs/toolkit'
22
import type { RootState, AppDispatch } from './store'
33

4+
import { addPostsListeners } from '@/features/posts/postsSlice'
5+
46
export const listenerMiddleware = createListenerMiddleware()
57

68
export const startAppListening = listenerMiddleware.startListening.withTypes<RootState, AppDispatch>()
79
export type AppStartListening = typeof startAppListening
810

911
export const addAppListener = addListener.withTypes<RootState, AppDispatch>()
1012
export type AppAddListener = typeof addAppListener
13+
14+
addPostsListeners(startAppListening)

src/features/posts/postsSlice.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createEntityAdapter, createSelector, createSlice, EntityState, PayloadA
22
import { client } from '@/api/client'
33

44
import type { RootState } from '@/app/store'
5+
import { AppStartListening } from '@/app/listenerMiddleware'
56
import { createAppAsyncThunk } from '@/app/withTypes'
67

78
import { logout } from '@/features/auth/authSlice'
@@ -116,5 +117,24 @@ export const selectPostsByUser = createSelector(
116117
[selectAllPosts, (state: RootState, userId: string) => userId],
117118
(posts, userId) => posts.filter((post) => post.user === userId),
118119
)
120+
119121
export const selectPostsStatus = (state: RootState) => state.posts.status
120122
export const selectPostsError = (state: RootState) => state.posts.error
123+
124+
export const addPostsListeners = (startAppListening: AppStartListening) => {
125+
startAppListening({
126+
actionCreator: addNewPost.fulfilled,
127+
effect: async (action, listenerApi) => {
128+
const { toast } = await import('react-tiny-toast')
129+
130+
const toastId = toast.show('New post added!', {
131+
variant: 'success',
132+
position: 'bottom-right',
133+
pause: true,
134+
})
135+
136+
await listenerApi.delay(5000)
137+
toast.remove(toastId)
138+
},
139+
})
140+
}

0 commit comments

Comments
 (0)