-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Newsfeed app react #69
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
39ca738
Intial React app setup and libraries installed
vardhanapoorv 2314ec6
Ably publish and subscribe added with input giving by user
vardhanapoorv b0090d0
Displaying the updates in the newsfeed
vardhanapoorv a05828b
Getting online users using Ably Channel Occupancy API
vardhanapoorv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,24 @@ | ||
.App { | ||
text-align: center; | ||
.App-header { | ||
margin-top: 10px | ||
} | ||
|
||
.App-logo { | ||
animation: App-logo-spin infinite 20s linear; | ||
height: 80px; | ||
.App-button { | ||
margin-top: 5px; | ||
float: right; | ||
} | ||
|
||
.App-header { | ||
background-color: #222; | ||
height: 150px; | ||
padding: 20px; | ||
color: white; | ||
.App-users { | ||
margin-top: 10px; | ||
text-align: left | ||
} | ||
|
||
.App-intro { | ||
font-size: large; | ||
.App-update { | ||
margin-bottom: 10px; | ||
cursor: pointer; | ||
text-align: center | ||
} | ||
|
||
@keyframes App-logo-spin { | ||
from { | ||
transform: rotate(0deg); | ||
} | ||
to { | ||
transform: rotate(360deg); | ||
} | ||
} | ||
.App-cards { | ||
overflow-y: scroll; | ||
max-height: 500px | ||
} |
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,21 +1,127 @@ | ||
import React, { Component } from "react"; | ||
import logo from "./logo.svg"; | ||
import "./App.css"; | ||
import React, { useState, useEffect } from "react"; | ||
import * as Ably from 'ably'; | ||
import { Container, Header, Divider, Button, Form, TextArea } from 'semantic-ui-react' | ||
|
||
import './App.css'; | ||
|
||
const avatarsInAssets = [ | ||
'https://cdn.glitch.com/0bff6817-d500-425d-953c-6424d752d171%2Favatar_8.png?1536042504672', | ||
'https://cdn.glitch.com/0bff6817-d500-425d-953c-6424d752d171%2Favatar_3.png?1536042507202', | ||
'https://cdn.glitch.com/0bff6817-d500-425d-953c-6424d752d171%2Favatar_6.png?1536042508902', | ||
'https://cdn.glitch.com/0bff6817-d500-425d-953c-6424d752d171%2Favatar_10.png?1536042509036', | ||
'https://cdn.glitch.com/0bff6817-d500-425d-953c-6424d752d171%2Favatar_7.png?1536042509659', | ||
'https://cdn.glitch.com/0bff6817-d500-425d-953c-6424d752d171%2Favatar_9.png?1536042513205', | ||
'https://cdn.glitch.com/0bff6817-d500-425d-953c-6424d752d171%2Favatar_2.png?1536042514285', | ||
'https://cdn.glitch.com/0bff6817-d500-425d-953c-6424d752d171%2Favatar_1.png?1536042516362', | ||
'https://cdn.glitch.com/0bff6817-d500-425d-953c-6424d752d171%2Favatar_4.png?1536042516573', | ||
'https://cdn.glitch.com/0bff6817-d500-425d-953c-6424d752d171%2Favatar_5.png?1536042517889' | ||
] | ||
|
||
const names = ["Ross", "Monica", "Rachel", "Joey", "Chandler", "Steve", "Bill", "Elon", "Tom", "Shaun" ] | ||
function getRandomArbitrary(min, max) { | ||
return Math.floor(Math.random() * (max - min) + min); | ||
} | ||
|
||
const myId = "id-" + Math.random().toString(36).substr(2, 16); | ||
const apiKey = 'JAofug.wynVXg:YU-Tf_OcO7Jz_UIK'; | ||
const ably = new Ably.Realtime({ | ||
key: apiKey, | ||
clientId: myId, | ||
echoMessages: false | ||
}); | ||
const chatChannel = ably.channels.get("chat"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add some helpful comments throughout your code? If anyone were to look at the source code directly, they should still be able to infer what's happening. As you'll be making a new PR anyway, adding comments in all (commit) steps shouldn't be an issue. |
||
const presenceChannel = ably.channels.get("presence"); | ||
let my = {}; | ||
my.avatar = avatarsInAssets[getRandomArbitrary(0, 9)]; | ||
my.name = names[getRandomArbitrary(0, 9)] | ||
|
||
const App = () => { | ||
const [todoInput, setTodoInput] = useState(""); | ||
const [state, setState] = useState({ | ||
msgs: [], | ||
newMsgs: [] | ||
}) | ||
|
||
let you = {}; | ||
|
||
useEffect(() => { | ||
|
||
chatChannel.subscribe("userInfo", (data) => { | ||
var dataObj = JSON.parse(JSON.stringify(data)); | ||
if (dataObj.clientId !== myId) { | ||
you.avatar = dataObj.data.avatar; | ||
you.name = dataObj.data.name; | ||
} | ||
}); | ||
|
||
presenceChannel.presence.subscribe('enter', function (member) { | ||
if (member.clientId !== myId) { | ||
chatChannel.publish("userInfo", { | ||
"avatar": my.avatar, | ||
"name": my.name | ||
}); | ||
} | ||
}); | ||
presenceChannel.presence.enter(); | ||
presenceChannel.presence.get(function (err, members) { | ||
for (var i in members) { | ||
if (members[i].clientId !== myId) { | ||
chatChannel.publish("userInfo", { | ||
"avatar": my.avatar, | ||
"name": my.name | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
chatChannel.subscribe("chatMessage", (data) => { | ||
var dataObj = JSON.parse(JSON.stringify(data)); | ||
var message = dataObj.data.message; | ||
var date = new Date(dataObj.data.date); | ||
setState(prevState => { | ||
return { | ||
...prevState, | ||
newMsgs: [...prevState.newMsgs, {"summary": message, "image": you.avatar, "date": date, "name": you.name}] | ||
}; | ||
}); | ||
}) | ||
|
||
}, []); | ||
|
||
function sendMyMessage() { | ||
const newMsgId = "msg-id-" + Math.random().toString(36).substr(2, 6); | ||
setTodoInput(""); | ||
if (todoInput !== "") { | ||
chatChannel.publish("chatMessage", { | ||
message: todoInput, | ||
localMsgId: newMsgId, | ||
date: new Date() | ||
}); | ||
setState(prevState => { | ||
return { | ||
...prevState, | ||
msgs: [...prevState.msgs, {"summary": todoInput, "image": my.avatar, "date": new Date(), "name": my.name}].sort((a,b)=>b.date-a.date) | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
class App extends Component { | ||
render() { | ||
return ( | ||
<div className="App"> | ||
<div className="App-header"> | ||
<img src={logo} className="App-logo" alt="logo" /> | ||
<h2>Welcome to React</h2> | ||
</div> | ||
<p className="App-intro"> | ||
To get started, edit <code>src/App.js</code> and save to reload. | ||
</p> | ||
</div> | ||
); | ||
} | ||
<Container textAlign='left' className='App-header'> | ||
<Header as='h2'>Ably React Tutorial - RealTime</Header> | ||
<Form> | ||
<TextArea | ||
value={todoInput} | ||
placeholder="Post an update" | ||
onChange={e => setTodoInput(e.target.value)} | ||
/> | ||
<div className="App-button"><Button content='Send' primary onClick={sendMyMessage} /></div> | ||
</Form> | ||
|
||
<Divider /> | ||
|
||
</Container> | ||
) | ||
} | ||
|
||
export default App; | ||
export default App; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is never recommended to include the API key in GitHub commits. This will need to go, I perhaps should explicitly mention it in the guide. Please see other tutorials, they have a placeholder like '' in place of the actual API key that they are expected to replace with their own key. I'm afraid it'll mean you'll need to raise a new PR with the step-wise commits again.