Cleanup reducers

This commit is contained in:
Alan Friedman 2019-05-27 19:12:37 -04:00
parent 3f028f06a4
commit f73925df66
6 changed files with 13 additions and 53 deletions

View File

@ -1,7 +1,3 @@
import {
process as processMessage,
} from 'utils/message'
export const openModal = payload => ({ type: 'OPEN_MODAL', payload })
export const closeModal = () => ({ type: 'CLOSE_MODAL' })
@ -23,13 +19,6 @@ export const toggleSocketConnected = payload => async (dispatch) => {
dispatch({ type: 'TOGGLE_SOCKET_CONNECTED', payload })
}
export const receiveEncryptedMessage = payload => async (dispatch, getState) => {
const state = getState()
const message = await processMessage(payload, state)
// Pass current state to all RECEIVE_ENCRYPTED_MESSAGE reducers for convenience, since each may have different needs
dispatch({ type: `RECEIVE_ENCRYPTED_MESSAGE_${message.type}`, payload: { payload: message.payload, state } })
}
export const createUser = payload => async (dispatch) => {
dispatch({ type: 'CREATE_USER', payload })
}

View File

@ -1,6 +1,7 @@
import { getSocket } from 'utils/socket'
import {
prepare as prepareMessage,
process as processMessage,
} from 'utils/message'
export const sendEncryptedMessage = payload => async (dispatch, getState) => {
@ -9,3 +10,10 @@ export const sendEncryptedMessage = payload => async (dispatch, getState) => {
dispatch({ type: `SEND_ENCRYPTED_MESSAGE_${msg.original.type}`, payload: msg.original.payload })
getSocket().emit('ENCRYPTED_MESSAGE', msg.toSend)
}
export const receiveEncryptedMessage = payload => async (dispatch, getState) => {
const state = getState()
const message = await processMessage(payload, state)
// Pass current state to all RECEIVE_ENCRYPTED_MESSAGE reducers for convenience, since each may have different needs
dispatch({ type: `RECEIVE_ENCRYPTED_MESSAGE_${message.type}`, payload: { payload: message.payload, state } })
}

View File

@ -1,5 +1,4 @@
import { getSocket } from 'utils/socket'
import isEqual from 'lodash/isEqual'
const receiveUserEnter = (payload, dispatch) => {
dispatch({ type: 'USER_ENTER', payload })
@ -8,7 +7,7 @@ const receiveUserEnter = (payload, dispatch) => {
const receiveToggleLockRoom = (payload, dispatch, getState) => {
const state = getState()
const lockedByUser = state.room.members.find(m => isEqual(m.publicKey, payload.publicKey))
const lockedByUser = state.room.members.find(m => m.publicKey.n === payload.publicKey.n)
const lockedByUsername = lockedByUser.username
const lockedByUserId = lockedByUser.id

View File

@ -109,18 +109,10 @@ class Home extends Component {
componentDidMount() {
this.bindEvents()
if (this.props.joining) {
this.props.openModal('Connecting')
}
this.beep = window.Audio && new window.Audio(beepFile)
}
componentWillReceiveProps(nextProps) {
if (this.props.joining && !nextProps.joining) {
this.props.closeModal()
}
Tinycon.setBubble(nextProps.faviconCount)
if (nextProps.faviconCount !== 0 && nextProps.faviconCount !== this.props.faviconCount && this.props.soundIsEnabled) {
@ -427,7 +419,6 @@ Home.propTypes = {
scrolledToBottom: PropTypes.bool.isRequired,
iAmOwner: PropTypes.bool.isRequired,
userId: PropTypes.string.isRequired,
joining: PropTypes.bool.isRequired,
toggleWindowFocus: PropTypes.func.isRequired,
faviconCount: PropTypes.number.isRequired,
soundIsEnabled: PropTypes.bool.isRequired,
@ -453,7 +444,6 @@ const mapStateToProps = (state) => {
modalComponent: state.app.modalComponent,
scrolledToBottom: state.app.scrolledToBottom,
iAmOwner: Boolean(me && me.isOwner),
joining: state.room.joining,
faviconCount: state.app.unreadMessageCount,
soundIsEnabled: state.app.soundIsEnabled,
socketConnected: state.app.socketConnected,

View File

@ -1,12 +1,5 @@
const initialState = {
items: [
// {
// type: 'message | file | isTyping | usernameChange | slashCommand',
// data,
// username,
// timestamp
// }
],
items: [],
}
const activities = (state = initialState, action) => {
@ -90,10 +83,6 @@ const activities = (state = initialState, action) => {
return state
}
if (action.payload.state.room.joining) {
return state
}
return {
...state,
items: [

View File

@ -9,33 +9,22 @@ const initialState = {
],
id: '',
isLocked: false,
joining: true,
size: 0,
}
const room = (state = initialState, action) => {
switch (action.type) {
case 'USER_EXIT':
const memberPubKeys = action.payload.members.map(m => JSON.stringify(m.publicKey))
const memberPubKeys = action.payload.members.map(m => m.publicKey.n)
return {
...state,
members: state.members
.filter(m => memberPubKeys.includes(JSON.stringify(m.publicKey)))
.map((m) => {
const payloadMember = action.payload.members.find(member => _.isEqual(m.publicKey, member.publicKey))
return {
...m,
...payloadMember,
}
}),
.filter(member => memberPubKeys.includes(member.publicKey.n))
}
case 'RECEIVE_ENCRYPTED_MESSAGE_ADD_USER':
const joining = false
return {
...state,
members: state.members.map((member) => {
if (_.isEqual(member.publicKey, action.payload.payload.publicKey)) {
if (member.publicKey.n === action.payload.payload.publicKey.n) {
return {
...member,
username: action.payload.payload.username,
@ -45,7 +34,6 @@ const room = (state = initialState, action) => {
}
return member
}),
joining,
}
case 'CREATE_USER':
return {
@ -61,14 +49,11 @@ const room = (state = initialState, action) => {
}
case 'USER_ENTER':
const members = _.uniqBy(action.payload.users, member => member.publicKey.n);
const size = action.payload.users ? action.payload.users.length : 1;
return {
...state,
id: action.payload.id,
isLocked: Boolean(action.payload.isLocked),
size,
joining: false,
members: members.reduce((acc, user) => {
const exists = state.members.find(m => m.publicKey.n === user.publicKey.n)
if (exists) {