Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
bundle.js
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"JHCC",
"Wopu"
]
}
46 changes: 46 additions & 0 deletions chitterAPI.js
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;
51 changes: 51 additions & 0 deletions chitterAPI.test.js
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 () => {

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

let samplePosts = ['Peep', 'Peep2'];

fetch.mockResponseOnce(JSON.stringify({
samplePosts

Choose a reason for hiding this comment

The 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");
})
})

})

})
31 changes: 31 additions & 0 deletions index.html
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>

Choose a reason for hiding this comment

The 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

Copy link

@Curtis-Turk Curtis-Turk Sep 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<script type="text/javascript" src="bundle.js" defer ></script>

</body>

</html>
14 changes: 14 additions & 0 deletions index.js
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();
})
Loading