Skip to content

Commit b28f6cc

Browse files
committed
working on C & D for conversations
1 parent d881fdb commit b28f6cc

File tree

5 files changed

+72
-17
lines changed

5 files changed

+72
-17
lines changed

src/components/newConversationForm.js

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,75 @@
11
import React, {Component} from 'react';
2+
import {Link} from 'react-router-dom';
23

34
class newConversationForm extends Component {
5+
constructor(props){
6+
super(props)
7+
this.state = {
8+
title: "",
9+
topic: "",
10+
// conversationIsPrivate: false
11+
}
12+
}
13+
14+
// ADD PARTICIPANTS AS WELL
15+
16+
handleCreateConversation = () => {
17+
console.log(this.state)
18+
const conversation = this.state
19+
console.log(JSON.stringify({conversation:conversation}))
20+
fetch('http://localhost:3000/conversations', {
21+
method: 'POST',
22+
headers: {
23+
Authorization: `Bearer ${localStorage.getItem("token")}`,
24+
"Content-type": "application/json"
25+
},
26+
body: JSON.stringify(conversation)
27+
}).then(res => res.json())
28+
.then(json => console.log(json))
29+
// grab json and set active conversation
30+
// push history to messageContainer view
31+
this.props.history.push('/home')
32+
}
33+
34+
onAddConversation = (conversation) => {
35+
console.log("ONADDCONVERSATION BEING CALLED")
36+
console.log(conversation)
37+
this.conversationChannel.send({
38+
title: conversation.title,
39+
topic: conversation.topic,
40+
user_id: localStorage.getItem("token")
41+
})
42+
}
43+
44+
handleTitle = e => {
45+
this.setState({title: e.target.value})
46+
}
47+
48+
handleTopic = e => {
49+
this.setState({topic: e.target.value})
50+
}
51+
52+
// handleIsPrivate = e => {
53+
// this.setState(prevState => ({conversationIsPrivate: !prevState.conversationIsPrivate}))
54+
// }
55+
56+
handleSubmit = e => {
57+
e.preventDefault()
58+
this.handleCreateConversation(this.state)
59+
}
460

5-
661
render() {
762
return (
863
<div>
964
<h3>Create a conversation</h3>
10-
<form onSubmit={this.props.handleNewConversation}>
65+
<Link to='/home'>Back</Link>
66+
<form onSubmit={this.handleSubmit}>
1167
<label>Title</label>
12-
<input></input><br></br>
68+
<input value={this.state.title} onChange={this.handleTitle}></input><br></br>
1369
<label>Topic</label>
14-
<input></input><br></br>
15-
<label>Private?</label>
16-
<input type="radio"></input><br></br>
70+
<input value={this.state.topic} onChange={this.handleTopic}></input><br></br>
71+
{/* <label>Private?</label>
72+
<input type="radio" value={this.state.conversationIsPrivate} onChange={this.handleIsPrivate}></input><br></br> */}
1773
<button type="submit">Submit</button>
1874
</form>
1975
</div>

src/containers/Home.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Home extends Component {
3333
})
3434

3535
this.cable = actioncable.createConsumer('ws://localhost:3000/cable')
36+
this.cable.subscriptions.create({channel: "ConversationsChannel"})
3637
this.conversationChannels = []
3738
json.forEach(conversation => {
3839
this.conversationChannels[`${conversation.id}`] = this.cable.subscriptions.create({
@@ -55,8 +56,8 @@ class Home extends Component {
5556
this.setState({activeConversation: activeConversation})
5657
}
5758

58-
handleCreateConversation = conversation => {
59-
console.log(conversation)
59+
handleCreateConversation = (title, topic, isPrivate) => {
60+
const conversation = {title: title, topic: topic, private: isPrivate}
6061
fetch('http://localhost:3000/conversations', {
6162
method: 'POST',
6263
headers: {
@@ -67,6 +68,7 @@ class Home extends Component {
6768
.then(json => console.log(json))
6869
// grab json and set active conversation
6970
// push history to messageContainer view
71+
this.props.history.push('/home')
7072
}
7173

7274
handleDelete = conversation => {
@@ -120,7 +122,7 @@ class Home extends Component {
120122
/>
121123
{error ? this.props.history.push('/login') : null}
122124
{activeConversation ?
123-
<MessageContainer activeConversation={activeConversation} onAddMessage={this.onAddMessage} />
125+
<MessageContainer activeConversation={activeConversation} onAddMessage={this.onAddMessage} handleDelete={this.handleDelete} />
124126
: <Greeting />}
125127

126128
</Fragment>

src/containers/conversationContainer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import React from 'react';
22
import Conversation from '../components/Conversation.js'
33

4-
const populateConversations = (conversations, handleClick) => {
4+
const populateConversations = (conversations, handleClick, handleDelete) => {
55
return conversations.map( conversation => {
6-
return <Conversation key={conversation.id} handleClick={handleClick} conversation={conversation} />
6+
return <Conversation key={conversation.id} handleClick={handleClick} conversation={conversation} handleDelete={handleDelete} />
77
})
88
}
99

1010
const ConversationContainer = (props) => {
1111

1212
const conversations = props.conversations
1313
const handleClick = props.handleClick
14-
14+
const handleDelete = props.handleDelete
1515
return (
1616
<div>
1717
{conversations ?
1818
<div>
1919
<h4 className="conversation_header">All Conversations</h4>
2020
<ul>
21-
{populateConversations(conversations, handleClick)}
21+
{populateConversations(conversations, handleClick, handleDelete)}
2222
</ul>
2323
</div>
2424
:

src/containers/navBar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class NavBar extends Component {
2323

2424
<ul>
2525
<li onClick={(() => {localStorage.setItem("token", "")})}><Link to="login">Log Out</Link></li>
26-
<li><Link to='/new' handleCreateConversation={handleCreateConversation}>Add Conversation</Link></li>
26+
<li><Link to='/new' state={handleCreateConversation}>Add Conversation</Link></li>
2727
<ConversationContainer conversations={conversations} handleClick={handleActiveConversation}/>
2828
</ul>
2929
</div>

src/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ import ReactDOM from 'react-dom';
33
import './index.css';
44
import App from './App';
55
import * as serviceWorker from './serviceWorker';
6-
// import { ActionCableProvider } from 'react-actioncable-provider';
76

87
ReactDOM.render(
98
<React.StrictMode>
10-
{/* <ActionCableProvider url="ws://localhost:3000/cable"> */}
119
<App />
12-
{/* </ ActionCableProvider> */}
1310
</React.StrictMode>,
1411
document.getElementById('root')
1512
);

0 commit comments

Comments
 (0)