Skip to content

Commit

Permalink
[#9] feat: implement api integration
Browse files Browse the repository at this point in the history
- GET /survey/:type
  • Loading branch information
hee-suh committed Feb 23, 2023
1 parent fb730ca commit 5c2d7ab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
52 changes: 23 additions & 29 deletions bowwowcare/src/views/SolutionPage/Sections/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { solutions } from "../../../utils/Dictionary";

import "../../../components/YoutubeEmbed/youtube.css";

function Solution({ response, emotion }) {
function Solution({ solution, emotion }) {
const [popup, setPopup] = useState(false);

const handleTrigger = () => {
Expand All @@ -15,34 +15,28 @@ function Solution({ response, emotion }) {

return (
<div>
{solutions[emotion]?.map((x) => {
if (response === x.id) {
return (
<div
key={response}
className="w-64 h-full text-center shadow-lg rounded-2xl flex flex-col justify-between px-8 pt-16 pb-8 mx-3"
>
<div>
<div className="font-bold pb-4">{x.question}</div>
<div>{x.solution}</div>
</div>
<button
onClick={() => setPopup(true)}
className="h-12 mt-8 px-4 rounded-md text-center bg-white border border-main-color text-main-color hover:bg-main-color hover:text-white"
>
<div className="hover:text-white">실천하기</div>
</button>
<Modal
trigger={popup}
handleTrigger={handleTrigger}
solution={x.solution}
>
<YoutubeEmbed embedId="w7aE4ihj7Ao" />
</Modal>
</div>
);
}
})}
<div
key={solution}
className="w-64 h-full text-center shadow-lg rounded-2xl flex flex-col justify-between px-8 pt-16 pb-8 mx-3"
>
<div>
<div className="font-bold pb-4">{solution.situation}</div>
<div>{solution.solution}</div>
</div>
<button
onClick={() => setPopup(true)}
className="h-12 mt-8 px-4 rounded-md text-center bg-white border border-main-color text-main-color hover:bg-main-color hover:text-white"
>
<div className="hover:text-white">실천하기</div>
</button>
<Modal
trigger={popup}
handleTrigger={handleTrigger}
solution={solution.solution}
>
<YoutubeEmbed embedId="w7aE4ihj7Ao" />
</Modal>
</div>
</div>
);
}
Expand Down
29 changes: 22 additions & 7 deletions bowwowcare/src/views/SolutionPage/SolutionPage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { useState, useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import axios from "axios";
import { API_URL } from "../../Config";

import Header from "../../components/Header";
import Solution from "./Sections/Solution";
Expand All @@ -12,10 +14,25 @@ function SolutionPage() {
const navigate = useNavigate();
const [open, setOpen] = useState(false);
const [alertMessage, setAlertMessage] = useState("");
const [solutions, setSolutions] = useState([]);


useEffect(() => {
// TODO: POST location.state.responses and get { situation : solution }{}
if (location?.state?.responses) {
axios({
method: 'post',
url: `${API_URL}/survey/${location?.state?.type}`,
data: location.state.responses
})
.then(response => {
if (response.status === 200) {
setSolutions(response.data);
}
})
.catch(error => {
console.log(error?.response);
});
}
}, []);

const handleOpen = (e) => {
Expand All @@ -37,12 +54,10 @@ function SolutionPage() {
<Header />
<div className="h-5/6 flex flex-col justify-between">
<div className="h-2/3 overflow-x-auto flex mt-10 px-2 py-8">
{Object.entries(location?.state?.responses)?.map(([response, value]) => {
if (value === "예") {
return (
<Solution key={response} response={response} emotion={location?.state?.emotion} />
);
}
{solutions?.map(solution => {
return (
<Solution key={solution.id} solution={solution} emotion={location?.state?.emotion} />
);
})}
</div>
<div className="w-full">
Expand Down

0 comments on commit 5c2d7ab

Please sign in to comment.