mirror of
https://github.com/darkwire/darkwire.io.git
synced 2025-07-19 19:14:53 +00:00
Cleanup reducers
This commit is contained in:
parent
3f028f06a4
commit
f73925df66
@ -1,7 +1,3 @@
|
|||||||
import {
|
|
||||||
process as processMessage,
|
|
||||||
} from 'utils/message'
|
|
||||||
|
|
||||||
export const openModal = payload => ({ type: 'OPEN_MODAL', payload })
|
export const openModal = payload => ({ type: 'OPEN_MODAL', payload })
|
||||||
export const closeModal = () => ({ type: 'CLOSE_MODAL' })
|
export const closeModal = () => ({ type: 'CLOSE_MODAL' })
|
||||||
|
|
||||||
@ -23,13 +19,6 @@ export const toggleSocketConnected = payload => async (dispatch) => {
|
|||||||
dispatch({ type: 'TOGGLE_SOCKET_CONNECTED', payload })
|
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) => {
|
export const createUser = payload => async (dispatch) => {
|
||||||
dispatch({ type: 'CREATE_USER', payload })
|
dispatch({ type: 'CREATE_USER', payload })
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { getSocket } from 'utils/socket'
|
import { getSocket } from 'utils/socket'
|
||||||
import {
|
import {
|
||||||
prepare as prepareMessage,
|
prepare as prepareMessage,
|
||||||
|
process as processMessage,
|
||||||
} from 'utils/message'
|
} from 'utils/message'
|
||||||
|
|
||||||
export const sendEncryptedMessage = payload => async (dispatch, getState) => {
|
export const sendEncryptedMessage = payload => async (dispatch, getState) => {
|
||||||
@ -8,4 +9,11 @@ export const sendEncryptedMessage = payload => async (dispatch, getState) => {
|
|||||||
const msg = await prepareMessage(payload, state)
|
const msg = await prepareMessage(payload, state)
|
||||||
dispatch({ type: `SEND_ENCRYPTED_MESSAGE_${msg.original.type}`, payload: msg.original.payload })
|
dispatch({ type: `SEND_ENCRYPTED_MESSAGE_${msg.original.type}`, payload: msg.original.payload })
|
||||||
getSocket().emit('ENCRYPTED_MESSAGE', msg.toSend)
|
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 } })
|
||||||
}
|
}
|
@ -1,5 +1,4 @@
|
|||||||
import { getSocket } from 'utils/socket'
|
import { getSocket } from 'utils/socket'
|
||||||
import isEqual from 'lodash/isEqual'
|
|
||||||
|
|
||||||
const receiveUserEnter = (payload, dispatch) => {
|
const receiveUserEnter = (payload, dispatch) => {
|
||||||
dispatch({ type: 'USER_ENTER', payload })
|
dispatch({ type: 'USER_ENTER', payload })
|
||||||
@ -8,7 +7,7 @@ const receiveUserEnter = (payload, dispatch) => {
|
|||||||
const receiveToggleLockRoom = (payload, dispatch, getState) => {
|
const receiveToggleLockRoom = (payload, dispatch, getState) => {
|
||||||
const state = 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 lockedByUsername = lockedByUser.username
|
||||||
const lockedByUserId = lockedByUser.id
|
const lockedByUserId = lockedByUser.id
|
||||||
|
|
||||||
|
@ -109,18 +109,10 @@ class Home extends Component {
|
|||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.bindEvents()
|
this.bindEvents()
|
||||||
|
|
||||||
if (this.props.joining) {
|
|
||||||
this.props.openModal('Connecting')
|
|
||||||
}
|
|
||||||
|
|
||||||
this.beep = window.Audio && new window.Audio(beepFile)
|
this.beep = window.Audio && new window.Audio(beepFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps) {
|
||||||
if (this.props.joining && !nextProps.joining) {
|
|
||||||
this.props.closeModal()
|
|
||||||
}
|
|
||||||
|
|
||||||
Tinycon.setBubble(nextProps.faviconCount)
|
Tinycon.setBubble(nextProps.faviconCount)
|
||||||
|
|
||||||
if (nextProps.faviconCount !== 0 && nextProps.faviconCount !== this.props.faviconCount && this.props.soundIsEnabled) {
|
if (nextProps.faviconCount !== 0 && nextProps.faviconCount !== this.props.faviconCount && this.props.soundIsEnabled) {
|
||||||
@ -427,7 +419,6 @@ Home.propTypes = {
|
|||||||
scrolledToBottom: PropTypes.bool.isRequired,
|
scrolledToBottom: PropTypes.bool.isRequired,
|
||||||
iAmOwner: PropTypes.bool.isRequired,
|
iAmOwner: PropTypes.bool.isRequired,
|
||||||
userId: PropTypes.string.isRequired,
|
userId: PropTypes.string.isRequired,
|
||||||
joining: PropTypes.bool.isRequired,
|
|
||||||
toggleWindowFocus: PropTypes.func.isRequired,
|
toggleWindowFocus: PropTypes.func.isRequired,
|
||||||
faviconCount: PropTypes.number.isRequired,
|
faviconCount: PropTypes.number.isRequired,
|
||||||
soundIsEnabled: PropTypes.bool.isRequired,
|
soundIsEnabled: PropTypes.bool.isRequired,
|
||||||
@ -453,7 +444,6 @@ const mapStateToProps = (state) => {
|
|||||||
modalComponent: state.app.modalComponent,
|
modalComponent: state.app.modalComponent,
|
||||||
scrolledToBottom: state.app.scrolledToBottom,
|
scrolledToBottom: state.app.scrolledToBottom,
|
||||||
iAmOwner: Boolean(me && me.isOwner),
|
iAmOwner: Boolean(me && me.isOwner),
|
||||||
joining: state.room.joining,
|
|
||||||
faviconCount: state.app.unreadMessageCount,
|
faviconCount: state.app.unreadMessageCount,
|
||||||
soundIsEnabled: state.app.soundIsEnabled,
|
soundIsEnabled: state.app.soundIsEnabled,
|
||||||
socketConnected: state.app.socketConnected,
|
socketConnected: state.app.socketConnected,
|
||||||
|
@ -1,12 +1,5 @@
|
|||||||
const initialState = {
|
const initialState = {
|
||||||
items: [
|
items: [],
|
||||||
// {
|
|
||||||
// type: 'message | file | isTyping | usernameChange | slashCommand',
|
|
||||||
// data,
|
|
||||||
// username,
|
|
||||||
// timestamp
|
|
||||||
// }
|
|
||||||
],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const activities = (state = initialState, action) => {
|
const activities = (state = initialState, action) => {
|
||||||
@ -90,10 +83,6 @@ const activities = (state = initialState, action) => {
|
|||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action.payload.state.room.joining) {
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
items: [
|
items: [
|
||||||
|
@ -9,33 +9,22 @@ const initialState = {
|
|||||||
],
|
],
|
||||||
id: '',
|
id: '',
|
||||||
isLocked: false,
|
isLocked: false,
|
||||||
joining: true,
|
|
||||||
size: 0,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const room = (state = initialState, action) => {
|
const room = (state = initialState, action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case 'USER_EXIT':
|
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 {
|
return {
|
||||||
...state,
|
...state,
|
||||||
members: state.members
|
members: state.members
|
||||||
.filter(m => memberPubKeys.includes(JSON.stringify(m.publicKey)))
|
.filter(member => memberPubKeys.includes(member.publicKey.n))
|
||||||
.map((m) => {
|
|
||||||
const payloadMember = action.payload.members.find(member => _.isEqual(m.publicKey, member.publicKey))
|
|
||||||
return {
|
|
||||||
...m,
|
|
||||||
...payloadMember,
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
case 'RECEIVE_ENCRYPTED_MESSAGE_ADD_USER':
|
case 'RECEIVE_ENCRYPTED_MESSAGE_ADD_USER':
|
||||||
const joining = false
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
members: state.members.map((member) => {
|
members: state.members.map((member) => {
|
||||||
if (_.isEqual(member.publicKey, action.payload.payload.publicKey)) {
|
if (member.publicKey.n === action.payload.payload.publicKey.n) {
|
||||||
return {
|
return {
|
||||||
...member,
|
...member,
|
||||||
username: action.payload.payload.username,
|
username: action.payload.payload.username,
|
||||||
@ -45,7 +34,6 @@ const room = (state = initialState, action) => {
|
|||||||
}
|
}
|
||||||
return member
|
return member
|
||||||
}),
|
}),
|
||||||
joining,
|
|
||||||
}
|
}
|
||||||
case 'CREATE_USER':
|
case 'CREATE_USER':
|
||||||
return {
|
return {
|
||||||
@ -61,14 +49,11 @@ const room = (state = initialState, action) => {
|
|||||||
}
|
}
|
||||||
case 'USER_ENTER':
|
case 'USER_ENTER':
|
||||||
const members = _.uniqBy(action.payload.users, member => member.publicKey.n);
|
const members = _.uniqBy(action.payload.users, member => member.publicKey.n);
|
||||||
const size = action.payload.users ? action.payload.users.length : 1;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
id: action.payload.id,
|
id: action.payload.id,
|
||||||
isLocked: Boolean(action.payload.isLocked),
|
isLocked: Boolean(action.payload.isLocked),
|
||||||
size,
|
|
||||||
joining: false,
|
|
||||||
members: members.reduce((acc, user) => {
|
members: members.reduce((acc, user) => {
|
||||||
const exists = state.members.find(m => m.publicKey.n === user.publicKey.n)
|
const exists = state.members.find(m => m.publicKey.n === user.publicKey.n)
|
||||||
if (exists) {
|
if (exists) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user