Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Trevor Austin
/
mpcs-52553-austin-winter-2021
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
af1a3877
authored
Mar 03, 2021
by
Trevor Austin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move posts state up to root element
parent
ab343228
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
22 deletions
week_8/examples/login_and_post/static/script.js
week_8/examples/login_and_post/static/script.js
View file @
af1a3877
...
...
@@ -67,28 +67,9 @@ class Compose extends React.Component {
}
class
Posts
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
);
this
.
state
=
{
posts
:
[],
}
}
refresh
(){
const
session_token
=
window
.
localStorage
.
getItem
(
"journal_session_token"
);
fetch
(
"http://127.0.0.1:5000/api/post"
,
{
method
:
'POST'
,
headers
:
{
'Content-Type'
:
'application/json'
},
body
:
JSON
.
stringify
({
session_token
:
session_token
})
})
.
then
((
response
)
=>
response
.
json
())
.
then
((
data
)
=>
{
this
.
setState
({
posts
:
data
});
});
}
render
()
{
const
posts
=
this
.
state
.
posts
.
map
((
post
)
=>
const
posts
=
this
.
props
.
posts
.
map
((
post
)
=>
<
div
key
=
{
post
.
id
}
id
=
{
"post_"
+
post
.
id
}
>
<
h3
>
{
post
.
title
}
<
/h3
>
<
div
>
{
post
.
body
}
<
/div
>
...
...
@@ -98,7 +79,7 @@ class Posts extends React.Component {
return
(
<
div
className
=
"posts"
id
=
"posts"
>
<
h2
>
Posts
<
/h2
>
<
button
onClick
=
{
()
=>
this
.
refresh
()
}
>
Refresh
<
/button
>
<
button
onClick
=
{
this
.
props
.
getPosts
}
>
Refresh
<
/button
>
{
posts
}
<
/div
>
);
...
...
@@ -114,6 +95,7 @@ class Journal extends React.Component {
this
.
passwordHandler
=
this
.
passwordHandler
.
bind
(
this
);
this
.
loginHandler
=
this
.
loginHandler
.
bind
(
this
);
this
.
logoutHandler
=
this
.
logoutHandler
.
bind
(
this
);
this
.
getPosts
=
this
.
getPosts
.
bind
(
this
);
this
.
state
=
{
posts
:
[],
username
:
'trevor'
,
...
...
@@ -161,6 +143,19 @@ class Journal extends React.Component {
this
.
setState
({
isLoggedIn
:
false
});
}
getPosts
()
{
const
session_token
=
window
.
localStorage
.
getItem
(
"journal_session_token"
);
fetch
(
"http://127.0.0.1:5000/api/post"
,
{
method
:
'POST'
,
headers
:
{
'Content-Type'
:
'application/json'
},
body
:
JSON
.
stringify
({
session_token
:
session_token
})
})
.
then
((
response
)
=>
response
.
json
())
.
then
((
data
)
=>
{
this
.
setState
({
posts
:
data
});
});
}
render
()
{
const
isLoggedIn
=
this
.
state
.
isLoggedIn
;
...
...
@@ -177,7 +172,7 @@ class Journal extends React.Component {
logoutHandler
=
{
this
.
logoutHandler
}
/
>
{
isLoggedIn
&&
<
Compose
/>
}
{
isLoggedIn
&&
<
Posts
/>
}
{
isLoggedIn
&&
<
Posts
posts
=
{
this
.
state
.
posts
}
getPosts
=
{
this
.
getPosts
}
/>
}
<
/div
>
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment