-
Notifications
You must be signed in to change notification settings - Fork 275
Chitter-challenge #124
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
base: master
Are you sure you want to change the base?
Chitter-challenge #124
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
bundle.js |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cSpell.words": [ | ||
"JHCC", | ||
"Wopu" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class ChitterAPI { | ||
loadPosts(callback) { | ||
fetch("https://chitter-backend-api-v2.herokuapp.com/peeps") | ||
.then(response => response.json()) | ||
.then(data => { | ||
callback(data); | ||
}) | ||
} | ||
|
||
createPost(userId, session_key, post, callback){ | ||
fetch('https://chitter-backend-api-v2.herokuapp.com/peeps', { | ||
method: 'POST', | ||
headers: { | ||
'Authorization': `Token token=${session_key}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
peep: { | ||
user_id: `${userId}`, | ||
body: `${post}` | ||
} | ||
}) | ||
}) | ||
.then(response => response.json()) | ||
.then(data => callback(data)) | ||
} | ||
|
||
newSession(username, password, callback){ | ||
fetch("https://chitter-backend-api-v2.herokuapp.com/sessions", { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
session: { | ||
handle: `${username}`, | ||
password: `${password}` | ||
} | ||
}) | ||
}) | ||
.then(response => response.json()) | ||
.then(data => callback(data)) | ||
} | ||
} | ||
|
||
module.exports = ChitterAPI; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require('jest-fetch-mock').enableMocks(); | ||
const ChitterAPI = require('./chitterAPI'); | ||
|
||
let api; | ||
|
||
describe('ChitterAPI', () => { | ||
beforeEach(() => { | ||
api = new ChitterAPI(); | ||
}) | ||
|
||
describe('loadPosts', () => { | ||
it('fetches notes from the server', async () => { | ||
let samplePosts = ['Peep', 'Peep2']; | ||
|
||
fetch.mockResponseOnce(JSON.stringify({ | ||
samplePosts | ||
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. Seems pretty straightforward to include the sample posts directly rather that setting them to a variable used only once |
||
})); | ||
|
||
api.loadPosts( (apiPosts) => { | ||
expect(apiPosts.samplePosts).toEqual(samplePosts); | ||
}) | ||
}) | ||
}) | ||
|
||
describe('createPost', () => { | ||
it('adds a post to the server', async () => { | ||
fetch.mockResponseOnce(JSON.stringify({ | ||
user_id: 1, | ||
body: "Test Peep" | ||
})); | ||
api.createPost(1, "Sample_Session_Key", "Peep", (post) => { | ||
expect(post.body).toEqual("Test Peep"); | ||
}) | ||
}) | ||
}) | ||
|
||
describe('newSession', () => { | ||
it('starts a session for a user', async () => { | ||
fetch.mockResponseOnce(JSON.stringify({ | ||
user_id: 0, | ||
session_key: "session_key" | ||
})) | ||
api.newSession("username", "password", (session) => { | ||
expect(session.user_id).toEqual(0); | ||
expect(session.session_key).toEqual("session_key"); | ||
}) | ||
}) | ||
|
||
}) | ||
|
||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<link rel="stylesheet" href="styles.css"> | ||
|
||
<title>Chitter Challenge</title> | ||
</head> | ||
|
||
<body> | ||
<div id="login-container"> | ||
<input type="text" id="input-username" placeholder="Username"/> | ||
<input type="password" id="input-password" placeholder="Password"/> | ||
<button id="submit-login">Login</button> | ||
<div id="logged-in"></div> | ||
</div> | ||
<h1>Chitter Challenge</h1> | ||
<input type="text" id="input-new-post"/> | ||
<button id="add-new-post">Peep!</button> | ||
<p>Welcome to Chitter</p> | ||
<div id="post-container"> | ||
|
||
</div> | ||
|
||
<script type="text/javascript" src="bundle.js"></script> | ||
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. Alex mentioned to us that it's good practice to include the script in the head with "Defer" in the tag to do the same thing 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.
|
||
</body> | ||
|
||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const ChitterAPI = require("./chitterAPI"); | ||
const Posts = require("./postsModel"); | ||
const PostsView = require("./postsView"); | ||
|
||
console.log('Chitter!'); | ||
|
||
const api = new ChitterAPI(); | ||
const posts = new Posts(); | ||
const view = new PostsView(posts, api); | ||
|
||
api.loadPosts((loaded_posts) => { | ||
posts.setPosts(loaded_posts); | ||
view.displayPosts(); | ||
}) |
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.
I don't think you need to use async functions in this case. I can understand why you would though