Skip to content

Commit 5b2937f

Browse files
author
github username
committed
adding posts heading and new button
1 parent c9f312a commit 5b2937f

File tree

9 files changed

+76
-35
lines changed

9 files changed

+76
-35
lines changed

new-work-ny-frontend/src/App.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function App() {
1414
return (
1515
<div className="app">
1616
<Provider store={store}>
17+
{console.log("in app", store.getState())}
1718
<Router>
1819
<Switch>
1920
<Route exact path="/" render={(props) => <Home {...props} />} />

new-work-ny-frontend/src/actions/index.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
fetchLogIn,
77
fetchNewPost,
88
fetchPosts,
9+
fetchUser,
910
} from "../adapters";
1011

1112
//USERS############################################################################
@@ -29,11 +30,30 @@ export const loggingIn = (dispatch, userData) => {
2930
return (dispatch) => {
3031
dispatch({ type: "ASYNC_START" });
3132
fetchLogIn(userData).then((data) => {
32-
if (data.user) {
33+
if (data !== "false") {
34+
window.localStorage.setItem("userId", JSON.stringify(data.user.id));
35+
dispatch({ type: LOG_IN, data });
36+
history.push("/dashboard");
37+
} else {
38+
history.push("/");
39+
alert("Please login again.");
40+
}
41+
});
42+
};
43+
};
44+
45+
export const setCurrentUser = (dispatch) => {
46+
return (dispatch) => {
47+
dispatch({ type: "ASYNC_START" });
48+
fetchUser().then((data) => {
49+
if (data.logged_in !== false) {
50+
debugger;
51+
window.localStorage.setItem("userId", `${data.user.id}`);
3352
dispatch({ type: LOG_IN, data });
3453
history.push("/dashboard");
3554
} else {
36-
alert("Login failed, please try again.");
55+
history.push("/");
56+
alert("Please login again.");
3757
}
3858
});
3959
};
@@ -44,17 +64,19 @@ export const loggingIn = (dispatch, userData) => {
4464
export const newPost = (dispatch, postData) => {
4565
return (dispatch) => {
4666
dispatch({ type: "ASYNC_START" });
47-
fetchNewPost(postData).then((data) => {
48-
dispatch({ type: NEW_POST, data });
49-
});
67+
fetchNewPost(postData);
5068
};
5169
};
5270

5371
export const getAllPosts = (dispatch) => {
5472
return (dispatch) => {
5573
dispatch({ type: "ASYNC_START" });
5674
fetchPosts().then((data) => {
57-
dispatch({ type: GET_POSTS, data });
75+
if (data !== null) {
76+
dispatch({ type: GET_POSTS, data });
77+
} else {
78+
return null;
79+
}
5880
});
5981
};
6082
};

new-work-ny-frontend/src/adapters/index.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ export const fetchLogIn = async (data) => {
2929
}
3030
);
3131
const json = await resp.json();
32+
if (json.status !== 404) {
33+
return json;
34+
} else {
35+
return "false";
36+
}
37+
};
38+
39+
export const fetchUser = async () => {
40+
const resp = await fetch(
41+
"https://new-work-ny-backend.herokuapp.com/logged_in",
42+
{
43+
headers: headers,
44+
method: "GET",
45+
}
46+
);
47+
const json = await resp.json();
48+
console.log("in fetch user", json);
3249
return json;
3350
};
3451

@@ -50,9 +67,11 @@ export const fetchPosts = async () => {
5067
method: "GET",
5168
});
5269
const response = await resp;
53-
if (response.ok) {
54-
return response.json();
55-
} else {
70+
71+
if (response.statusText === "No Content" || response.posts === undefined) {
5672
return null;
73+
} else {
74+
console.log("in fetch posts", response);
75+
return response;
5776
}
5877
};

new-work-ny-frontend/src/components/posts/NewPostModal.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const NewPostModal = ({ currentUser, newPost }) => {
1919
};
2020

2121
const handleSubmit = () => {
22-
let author_id = currentUser.id;
22+
let author_id = parseInt(window.localStorage.getItem("userId"));
2323
let data = {
2424
title,
2525
description,

new-work-ny-frontend/src/components/posts/postsContainer/Posts.jsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ import React, { Component } from "react";
22
import { ParallaxLayer } from "react-spring/renderprops-addons";
33
import { Layout, Button, Space, Col, Row, Divider } from "antd";
44
import PostCard from "../postcard/PostCard";
5-
import { useEffect } from "react";
5+
import { useEffect, useState } from "react";
66

77
import "./Posts.css";
88

99
const { Header, Sider } = Layout;
1010

1111
const Posts = ({ getAllPosts, posts }) => {
12+
const [postList, editPostList] = useState([]);
13+
1214
useEffect(() => {
1315
getAllPosts();
14-
}, []);
1516

16-
const postCards = posts.map((post, i) => (
17-
<PostCard id="flex-item" post={post} key={post.id} />
18-
));
17+
if (posts.length) {
18+
let postCards = posts.map((post, i) => (
19+
<PostCard id="flex-item" post={post} key={post.id} />
20+
));
21+
editPostList(postCards);
22+
}
23+
});
1924

2025
return (
2126
<Layout
@@ -44,12 +49,11 @@ const Posts = ({ getAllPosts, posts }) => {
4449
<br />
4550
<br />
4651
<Divider orientation="center" />
47-
<div id="container" align="middle" style={{ background: "none" }}>
48-
<Space size="large">{posts.length ? postCards : null}</Space>
49-
<Space size="large">{posts.length ? postCards : null}</Space>
50-
<Space size="large">{posts.length ? postCards : null}</Space>
51-
<Space size="large">{posts.length ? postCards : null}</Space>
52-
</div>
52+
{postList !== [] ? (
53+
<div id="container" align="middle" style={{ background: "none" }}>
54+
<Space size="large">{postList.length ? postList : null}</Space>
55+
</div>
56+
) : null}
5357
</Layout>
5458
);
5559
};

new-work-ny-frontend/src/components/singUpLogIn/LogIn.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useHistory } from "react-router-dom";
77
import "./LogIn.css";
88
const { Header } = Layout;
99

10-
const LogIn = ({ handleSubmit, handleFormView, formView }) => {
10+
const LogIn = ({ handleSubmit }) => {
1111
const [email, setEmail] = useState("");
1212
const [password, setPassword] = useState("");
1313

@@ -23,7 +23,6 @@ const LogIn = ({ handleSubmit, handleFormView, formView }) => {
2323
password,
2424
};
2525
handleSubmit("login", data);
26-
handleFormView("login");
2726
setEmail("");
2827
setPassword("");
2928
};

new-work-ny-frontend/src/containers/dashboard/Dashboard.jsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { connect } from "react-redux";
77
import * as actions from "../../actions";
88
import NewPostModal from "../../components/posts/NewPostModal";
99
import Posts from "../../components/posts/postsContainer/Posts";
10+
import { useEffect, useState } from "react";
1011

1112
const { Header } = Layout;
1213

@@ -38,15 +39,9 @@ class Dashboard extends Component {
3839
>
3940
NEW WORK NEW YORK
4041
</Header>
41-
<Header
42-
id="coming-soon-header"
43-
style={{
44-
background: "none",
45-
padding: "1%",
46-
}}
47-
>
48-
More coming soon!
49-
</Header>
42+
43+
<NewPostModal currentUser={currentUser} newPost={newPost} />
44+
<Posts getAllPosts={getAllPosts} posts={posts} />
5045
</div>
5146
</div>
5247
);

new-work-ny-frontend/src/reducers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const userReducer = (state = null, action) => {
1515
const postsReducer = (state = [], action) => {
1616
switch (action.type) {
1717
case NEW_POST:
18-
return state.posts.concat(action.data.post);
18+
return [...state.posts, action.data.post];
1919
case GET_POSTS:
2020
return action.data.posts;
2121
default:
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { createStore, applyMiddleware } from "redux";
1+
import { createStore, applyMiddleware, compose } from "redux";
22
import reduxThunk from "redux-thunk";
33
import rootReducer from "../reducers/index";
44

55
const configureStore = () => {
6-
return createStore(rootReducer, applyMiddleware(reduxThunk));
6+
let store = createStore(rootReducer, compose(applyMiddleware(reduxThunk)));
7+
return store;
78
};
89

910
export default configureStore;

0 commit comments

Comments
 (0)