This repository has been archived by the owner on May 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create a socket * Connect to a channel on note selection * Leave old channel and connect to new one when selection new note
- Loading branch information
Showing
11 changed files
with
199 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,127 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
import _ from 'lodash'; | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
import _ from 'lodash'; | ||
import { Col, Input, Layout, Row } from 'antd'; | ||
import { updateBody, updateTitle } from './Actions'; | ||
import { | ||
connectToChannel, | ||
leaveChannel, | ||
updateBody, | ||
updateTitle | ||
} from './Actions'; | ||
|
||
const { Content } = Layout; | ||
const { Content } = Layout; | ||
const { TextArea } = Input; | ||
const Props = { | ||
const Props = { | ||
notes: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
id: PropTypes.number.isRequired, | ||
title: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired | ||
id: PropTypes.number.isRequired, | ||
title: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired | ||
}).isRequired | ||
).isRequired, | ||
noteId: PropTypes.number.isRequired, | ||
onUpdateBody: PropTypes.func.isRequired, | ||
noteId: PropTypes.number.isRequired, | ||
onUpdateBody: PropTypes.func.isRequired, | ||
onUpdateTitle: PropTypes.func.isRequired, | ||
onConnectToChannel: PropTypes.func.isRequired, | ||
}; | ||
|
||
let RenderNote = function Note({notes, noteId, onUpdateBody, onUpdateTitle}) { | ||
let currentNote; | ||
class Note extends Component { | ||
componentDidMount() { | ||
const {socket, noteId, onConnectToChannel} = this.props; | ||
|
||
if (noteId !== -1) { | ||
let noteIndex = _.findIndex(notes, (note) => {return (note.id === noteId)}); | ||
if (noteId !== -1) { | ||
onConnectToChannel(socket, noteId); | ||
} | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
const {socket, noteId, channel, onConnectToChannel, onLeaveChannel} = this.props; | ||
|
||
if (nextProps.noteId !== noteId) { | ||
onLeaveChannel(channel); | ||
onConnectToChannel(nextProps.socket, nextProps.noteId); | ||
} | ||
if (!socket && nextProps.socket) { | ||
onConnectToChannel(nextProps.socket, nextProps.noteId); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
const {channel, onLeaveChannel} = this.props; | ||
|
||
currentNote = notes[noteIndex] | ||
} else { | ||
currentNote = {title: 'No Note selected.', body: ''} | ||
if (channel) { | ||
onLeaveChannel(channel); | ||
} | ||
} | ||
|
||
return ( | ||
<Content style={{ margin: '24px', overflow: 'initial' }}> | ||
<div style={{ padding: 24, background: '#fff', textAlign: 'center' }}> | ||
<Row> | ||
<Col span={20} offset={2}> | ||
<h1> | ||
<TextArea | ||
autosize={{ minRows: 1 }} | ||
placeholder="Title" | ||
value={currentNote.title} | ||
onChange={({ target: { value: newTitle } }) => | ||
onUpdateTitle(currentNote.id, newTitle)} | ||
/> | ||
</h1> | ||
</Col> | ||
</Row> | ||
|
||
<Row style={{ marginTop: 35, textAlign: "justify" }}> | ||
<Col span={20} offset={2}> | ||
<p> | ||
<TextArea | ||
autosize={{ minRows: 6 }} | ||
placeholder="Note content" | ||
value={currentNote.body || ''} | ||
onChange={({ target: { value: newBody } }) => | ||
onUpdateBody(currentNote.id, newBody)} | ||
/> | ||
</p> | ||
</Col> | ||
</Row> | ||
</div> | ||
</Content> | ||
); | ||
render () { | ||
const {notes, noteId, onUpdateBody, onUpdateTitle} = this.props; | ||
let currentNote; | ||
|
||
if (noteId !== -1) { | ||
let noteIndex = _.findIndex(notes, (note) => {return (note.id === noteId)}); | ||
|
||
currentNote = notes[noteIndex] | ||
} else { | ||
currentNote = {title: 'No Note selected.', body: ''} | ||
} | ||
|
||
return ( | ||
<Content style={{ margin: '24px', overflow: 'initial' }}> | ||
<div style={{ padding: 24, background: '#fff', textAlign: 'center' }}> | ||
<Row> | ||
<Col span={20} offset={2}> | ||
<h1> | ||
<TextArea | ||
autosize={{ minRows: 1 }} | ||
placeholder="Title" | ||
value={currentNote.title} | ||
onChange={({ target: { value: newTitle } }) => | ||
onUpdateTitle(currentNote.id, newTitle)} | ||
/> | ||
</h1> | ||
</Col> | ||
</Row> | ||
|
||
<Row style={{ marginTop: 35, textAlign: "justify" }}> | ||
<Col span={20} offset={2}> | ||
<p> | ||
<TextArea | ||
autosize={{ minRows: 6 }} | ||
placeholder="Note content" | ||
value={currentNote.body || ''} | ||
onChange={({ target: { value: newBody } }) => | ||
onUpdateBody(currentNote.id, newBody)} | ||
/> | ||
</p> | ||
</Col> | ||
</Row> | ||
</div> | ||
</Content> | ||
); | ||
} | ||
} | ||
|
||
RenderNote.PropTypes = Props; | ||
Note.PropTypes = Props; | ||
|
||
const mapStateToProps = function mapStateToProps(state) { | ||
return { | ||
socket: state.SessionReducer.socket, | ||
notes: state.NotesListReducer.notes, | ||
noteId: state.NotesListReducer.noteId | ||
noteId: state.NotesListReducer.noteId, | ||
channel: state.NoteReducer.channel, | ||
}; | ||
}; | ||
|
||
const mapDispatchToProps = function mapDispatchToProps(dispatch) { | ||
return ({ | ||
onUpdateBody: (id, body) => {dispatch(updateBody(id, body))}, | ||
onUpdateTitle: (id, title) => {dispatch(updateTitle(id, title))}, | ||
onUpdateBody: (id, body) => {dispatch(updateBody(id, body))}, | ||
onUpdateTitle: (id, title) => {dispatch(updateTitle(id, title))}, | ||
onConnectToChannel: (socket, id) => {dispatch(connectToChannel(socket, id))}, | ||
onLeaveChannel: (channel) => {dispatch(leaveChannel(channel))}, | ||
}); | ||
} | ||
|
||
const Note = connect(mapStateToProps, mapDispatchToProps)(RenderNote); | ||
Note = connect(mapStateToProps, mapDispatchToProps)(Note); | ||
export default Note; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { combineReducers } from 'redux'; | ||
import NoteReducer from '../Note/Reducer'; | ||
import NotesListReducer from '../NotesList/Reducer'; | ||
import SessionReducer from '../Session/Reducer'; | ||
|
||
let NoteApp = combineReducers({NotesListReducer}); | ||
let NoteApp = combineReducers({ | ||
SessionReducer, | ||
NoteReducer, | ||
NotesListReducer, | ||
}); | ||
|
||
export default NoteApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Socket } from 'phoenix'; | ||
import { SOCKET_CONNECTED } from './Types'; | ||
|
||
const API_URL = process.env.REACT_APP_API_HOST_URL; | ||
const WEBSOCKET_URL = API_URL.replace(/(https|http)/, 'ws').replace('/api', ''); | ||
|
||
export const connectToSocket = () => { | ||
return function(dispatch) { | ||
const socket = new Socket(`${WEBSOCKET_URL}/socket`, {}); | ||
socket.connect(); | ||
|
||
return (dispatch({ | ||
type: SOCKET_CONNECTED, | ||
socket: socket, | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { SOCKET_CONNECTED } from './Types'; | ||
|
||
const initialState = { | ||
socket: null, | ||
}; | ||
|
||
let SessionReducer = function SessionReducer(state = initialState, action) { | ||
switch (action.type) { | ||
case SOCKET_CONNECTED: | ||
return (Object.assign( | ||
{}, | ||
state, | ||
{ | ||
...state, | ||
socket: action.socket, | ||
} | ||
)); | ||
|
||
default: | ||
return state; | ||
} | ||
} | ||
|
||
export default SessionReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const SOCKET_CONNECTED = 'SOCKET_CONNECTED'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters