Skip to content
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
87 changes: 87 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.4.0",
"react": "^18.2.0",
"react-cookie": "^4.1.1",
"react-dom": "^18.2.0",
"react-router-dom": "^6.11.1",
"react-scripts": "5.0.1",
Expand Down
3 changes: 3 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import PostEditPage from "./routes/PostEditPage";
import SignUpPage from "./routes/SignUpPage";
import PostDetailPage from "./routes/PostDetailPage";
import SignInPage from "./routes/SignInPage";
import MyInfoPage from "./routes/MyInfoPage"


function App() {
Expand All @@ -29,6 +30,8 @@ function App() {
<Route path="/signup" element={<SignUpPage />} />
{/* sign up */}
<Route path="/signin" element={<SignInPage />} />
{/* my info */}
<Route path="/myinfo" element={<MyInfoPage />} />
</Routes>
<Footer />
</BrowserRouter>
Expand Down
188 changes: 188 additions & 0 deletions src/apis/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { instance, instanceWithToken } from "./axios";
import { removeCookie } from "../utils/cookie";

// src/api/api.js
// Account 관련 API들
export const signIn = async (data) => {
const response = await instance.post("/account/signin/", data);
if (response.status === 200) {
window.location.href = "/";
} else {
console.log("Error");
}
};

export const signUp = async (data) => {
const response = await instance.post("/account/signup/", data);
if (response.status === 200) {
window.location.href = "/";
}
return response;
};

export const getUser = async () => {
const response = await instanceWithToken.get("/account/info/");
if (response.status == 200) {
console.log("success");
} else {
console.log("fail");
}
return response;
};

export const getUserProfile = async () => {
const response = await instanceWithToken.get("/account/profile/");
if (response.status == 200) {
console.log("success");
} else {
console.log("fail");
}
return response;
};

export const updateUserProfile = async () => {
const response = await instanceWithToken.patch("/account/profile/");
if (response.status == 200) {
console.log("success");
} else {
console.log("fail");
}
return response;
};

export const getPosts = async () => {
const response = await instance.get("/post/");
return response.data;
};

export const getPost = async (id) => {
const response = await instance.get(`/post/${id}/`);
return response.data;
};

export const createPost = async (data, navigate) => {
const response = await instanceWithToken.post("/post/", data);
if (response.status === 201) {
console.log("POST SUCCESS");
navigate(-1); // 새로고침 X
navigate("/post");
} else {
console.log("[ERROR] error while creating post");
}
};

export const updatePost = async (id, data, navigate) => {
const response = await instanceWithToken.patch(`/post/${id}/`, data);
if (response.status === 200) {
console.log("POST UPDATE SUCCESS");
navigate(-1);
} else {
console.log("[ERROR] error while updating post");
}
};

// 과제!!
export const deletePost = async (id, navigate) => {
const response = await instanceWithToken.delete(`/post/${id}/`);
window.confirm("정말 삭제하실건가요...? U._.U");
if (response.status === 204) {
console.log("POST DELETE SUCCESS");
window.location.reload();
navigate(-1);
} else {
console.log("[ERROR] error while deleting post");
}
};

// 과제!!
export const likePost = async (postId) => {
const response = await instanceWithToken.post(`/post/${postId}/like/`);
if (response.status == 200) {
console.log("I like it!");
window.location.reload();
} else {
console.log("[Error] error while liking post");
}
};

// src/apis/api.js
// ...
// 추가

// Tag 관련 API들
export const getTags = async () => {
const response = await instance.get("/tag/");
return response.data;
};

export const createTag = async (data) => {
const response = await instanceWithToken.post("/tag/", data);
if (response.status === 201) {
console.log("TAG SUCCESS");
} else {
console.log("[ERROR] error while creating tag");
}
return response; // response 받아서 그 다음 처리
};

// Comment 관련 API들
export const getComments = async (postId) => {
const response = await instance.get(`/comment/?post=${postId}`);
return response.data;
};

export const createComment = async (data) => {
const response = await instanceWithToken.post("/comment/", data);
if (response.status === 201) {
console.log("COMMENT SUCCESS");
window.location.reload(); // 새로운 코멘트 생성시 새로고침으로 반영
} else {
console.log("[ERROR] error while creating comment");
}
};

export const updateComment = async (id, data) => {
const response = await instanceWithToken.patch(`/comment/${id}/`, data);
if (response.status === 200) {
console.log("COMMENT UPDATE SUCCESS");
window.location.reload();
} else {
console.log("[ERROR] error while updating comment");
}
};

// 과제 !!
export const deleteComment = async (id) => {
const response = await instanceWithToken.delete(`/comment/${id}/`);
if (response.status === 204) {
console.log("COMMENT DELETE SUCCESS");
window.location.reload();
} else {
console.log("[ERROR] error while deleting comment");
}
};

export const refreshToken = async (token) => {
const response = await instance.post("/account/refresh/", { refresh: token });
if (response.status === 200) {
console.log("REFRESH TOKEN SUCCESS");
} else {
console.log("[ERROR] error while refreshing token");
}
};

export const logOut = async (token) => {
const response = await instanceWithToken.post("/account/logout/", {
refresh: token,
});
if (response.status === 204) {
console.log("REFRESH TOKEN SUCCESS");

removeCookie("refresh_token");
removeCookie("access_token");

window.location.reload();
} else {
console.log("[ERROR] error while refreshing token");
}
};
Loading