Skip to content

Instantly share code, notes, and snippets.

@lushiyun
Last active September 26, 2020 21:24
Show Gist options
  • Save lushiyun/239f65dd925546bc19a1b8715833a5f7 to your computer and use it in GitHub Desktop.
Save lushiyun/239f65dd925546bc19a1b8715833a5f7 to your computer and use it in GitHub Desktop.

Revisions

  1. lushiyun revised this gist Sep 26, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions action-cable-messagesSlice.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // src/features/messages/messagesSlice.js

    ... // omitted imports

    const messagesAdapter = createEntityAdapter()
  2. lushiyun revised this gist Sep 26, 2020. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions action-cable-messagesSlice.js
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,10 @@
    const messagesAdapter = createEntityAdapter()

    const initialState = messagesAdapter.getInitialState({
    status: 'idle',
    error: null,
    ...
    })

    ... // omitted thunks for fetching user's messages
    ... // omitted thunks

    const messagesSlice = createSlice({
    name: 'messages',
  3. lushiyun revised this gist Sep 26, 2020. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions action-cable-messagesSlice.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    ... // omitted imports

    import { selectTeamById } from '../teams/teamsSlice'

    const messagesAdapter = createEntityAdapter()

    const initialState = messagesAdapter.getInitialState({
  4. lushiyun created this gist Sep 26, 2020.
    38 changes: 38 additions & 0 deletions action-cable-messagesSlice.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    ... // omitted imports

    import { selectTeamById } from '../teams/teamsSlice'

    const messagesAdapter = createEntityAdapter()

    const initialState = messagesAdapter.getInitialState({
    status: 'idle',
    error: null,
    })

    ... // omitted thunks for fetching user's messages

    const messagesSlice = createSlice({
    name: 'messages',
    initialState,
    reducers: {
    messageReceived(state, action) {
    const data = action.payload.data
    const message = {
    id: data.id,
    ...data.attributes,
    teamId: data.relationships.team.data.id,
    userId: data.relationships.user.data.id,
    }
    messagesAdapter.addOne(state, message)
    },
    },
    extraReducers: {

    ...

    },
    })

    export const { messageReceived } = messagesSlice.actions

    export default messagesSlice.reducer