Skip to content

Commit 54529a2

Browse files
authored
feat: notion api 연동 구현 완료
2 parents 39459d8 + a48d817 commit 54529a2

File tree

7 files changed

+237
-134
lines changed

7 files changed

+237
-134
lines changed

src/api/notionAPI.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ import axios from 'axios';
22

33
const API_BASE_URL = 'https://www.branchify.site/api';
44

5-
/**
6-
* @param {string} userEmail - 사용자 이메일
7-
* @param {string} assistantName - 어시스턴트 이름
8-
* @returns {Promise<Object>} Notion OAuth 연결 응답 데이터
9-
*/
105
export const connectNotionOAuth = async ({ userEmail, assistantName }) => {
116
try {
127
const response = await axios.get(
@@ -33,10 +28,6 @@ export const connectNotionOAuth = async ({ userEmail, assistantName }) => {
3328
}
3429
};
3530

36-
/**
37-
* @param {string} assistantName - 어시스턴트 이름
38-
* @returns {Promise<Object[]>} Notion 페이지 데이터 배열
39-
*/
4031
export const fetchNotionPages = async (assistantName) => {
4132
const token = localStorage.getItem('access_token');
4233

@@ -65,3 +56,29 @@ export const fetchNotionPages = async (assistantName) => {
6556
throw error;
6657
}
6758
};
59+
60+
export const saveNotionPages = async (assistantName, selectedPages) => {
61+
const token = localStorage.getItem('access_token');
62+
if (!token) throw new Error('인증 토큰이 없습니다.');
63+
if (!assistantName) throw new Error('assistantName이 필요합니다.');
64+
65+
const updatedPages = [...selectedPages].map((page) => ({
66+
id: page.id,
67+
isChecked: true,
68+
}));
69+
70+
try {
71+
const response = await axios.post(
72+
`${API_BASE_URL}/assistantlist/notionPages`,
73+
updatedPages,
74+
{
75+
headers: { Authorization: `Bearer ${token}` },
76+
params: { assistantName },
77+
}
78+
);
79+
return response.data;
80+
} catch (error) {
81+
console.error(error.response?.data || error.message);
82+
throw error;
83+
}
84+
};

src/components/BotAi/BotStep3.jsx

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,18 @@ const BotStep3 = ({ onPrev, assistantData, setAssistantData }) => {
3939
const [selectedActions, setSelectedActions] = useState([]);
4040
const navigate = useNavigate();
4141

42-
const { data: updatedAssistant, refetch } = useQuery({
43-
queryKey: ['assistant', assistantData.assistantName],
42+
const {
43+
data: notionPages,
44+
refetch,
45+
isLoading,
46+
error,
47+
} = useQuery({
48+
queryKey: ['notionPages', assistantData.assistantName],
4449
queryFn: () => fetchNotionPages(assistantData.assistantName),
45-
enabled: !!assistantData.assistantName,
50+
enabled: !!assistantData.assistantName && assistantData.isConnect === 1,
4651
});
4752

48-
useEffect(() => {
49-
if (updatedAssistant && updatedAssistant.isConnect === 1) {
50-
console.log('Notion 연결됨', updatedAssistant);
51-
setAssistantData((prev) => ({
52-
...prev,
53-
isConnect: 1,
54-
}));
55-
setSelectedKnowledge('Notion');
56-
}
57-
}, [updatedAssistant]);
53+
useEffect(() => {}, [notionPages]);
5854

5955
const handlePDFClick = () => {
6056
setSelectedKnowledge('PDF');
@@ -78,14 +74,39 @@ const BotStep3 = ({ onPrev, assistantData, setAssistantData }) => {
7874
const notionWindow = window.open(
7975
notionAuthURL,
8076
'_blank',
81-
'width=600,height=700'
77+
'width=600,height=700,top=100,left=500'
8278
);
8379
if (!notionWindow) {
8480
alert('팝업을 허용해주세요.');
8581
}
82+
83+
// 팝업창이 닫히면 OAuth 연결 상태 갱신
84+
const checkPopupClosed = setInterval(() => {
85+
if (notionWindow.closed) {
86+
clearInterval(checkPopupClosed);
87+
setAssistantData((prev) => ({ ...prev, isConnect: 1 }));
88+
refetch(); // Notion 페이지 다시 불러오기
89+
}
90+
}, 1000);
8691
}
8792
};
8893

94+
useEffect(() => {
95+
const handleNotionAuthComplete = (event) => {
96+
if (event.data === 'notion_auth_success') {
97+
if (typeof setAssistantData === 'function') {
98+
setAssistantData((prev) => ({ ...prev, isConnect: 1 }));
99+
}
100+
}
101+
};
102+
103+
window.addEventListener('message', handleNotionAuthComplete);
104+
105+
return () => {
106+
window.removeEventListener('message', handleNotionAuthComplete);
107+
};
108+
}, [setAssistantData]);
109+
89110
const toggleAction = (action) => {
90111
setSelectedActions((prev) =>
91112
prev.includes(action)
@@ -221,7 +242,10 @@ const BotStep3 = ({ onPrev, assistantData, setAssistantData }) => {
221242
)}
222243

223244
{selectedKnowledge === 'Notion' && (
224-
<BotStepNotion onClose={() => setSelectedKnowledge(null)} />
245+
<BotStepNotion
246+
onClose={() => setSelectedKnowledge(null)}
247+
assistantName={assistantData.assistantName}
248+
/>
225249
)}
226250

227251
{selectedKnowledge === 'Drive' && (

src/components/BotAi/modal/BotStepDrive.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const buttonStyle = css`
5858
height: 38px;
5959
width: 120px;
6060
border-radius: 30px;
61-
background: #F1502F;
61+
background: #f1502f;
6262
display: flex;
6363
align-items: center;
6464
justify-content: center;
@@ -86,10 +86,14 @@ const BotStepDrive = ({ onClose }) => {
8686

8787
return (
8888
<div css={modalStyle}>
89-
<span css={closeButton} onClick={onClose}></span>
89+
<span css={closeButton} onClick={onClose}>
90+
91+
</span>
9092
<div>
91-
<h3 css={titleText}>추가하고 싶은 <b>Drive</b>를 넣어주세요.</h3>
92-
{/* 생성하기 버튼 */}
93+
<h3 css={titleText}>
94+
추가하고 싶은 <b>Drive</b>를 넣어주세요.
95+
</h3>
96+
{/* 생성하기 버튼 */}
9397
</div>
9498
<div css={buttonContainer}>
9599
<button css={buttonStyle} onClick={() => navigate('/bot/list')}>

0 commit comments

Comments
 (0)