Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 스트리밍기능 추가, 알림함 비우기 기능 + Refactor #116

Merged
merged 12 commits into from
Dec 19, 2024
Prev Previous commit
Next Next commit
🎨 fetchMyProfile 분리
  • Loading branch information
yunochi committed Dec 19, 2024
commit 9c8475fb6a61aa1e6b478651ba6c41829e62aca9
24 changes: 5 additions & 19 deletions src/app/main/_header.tsx
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import { FaXmark } from 'react-icons/fa6';
import WebSocketState from '../_components/webSocketState';
import { NotificationContext } from './layout';
import { webSocketManager } from '@/app/main/_websocketManager';
import { fetchMyProfile } from '@/utils/profile/fetchMyProfile';

type headerProps = {
setUserProfile: Dispatch<SetStateAction<userProfileMeDto | undefined>>;
@@ -33,24 +34,6 @@ export default function MainHeader({ setUserProfile }: headerProps) {

const notificationContext = useContext(NotificationContext);

const fetchMyProfile = async (): Promise<userProfileMeDto | undefined> => {
const user_handle = localStorage.getItem('user_handle');

if (user_handle) {
const res = await fetch('/api/db/fetch-my-profile', {
method: 'GET',
});
if (!res.ok) {
if (res.status === 401) {
forcedLogoutModalRef.current?.showModal();
}
return;
}
const data = await res.json();
return data;
}
};

const onProfileUpdateEvent = (ev: CustomEvent<Partial<userProfileMeDto>>) => {
const logger = new Logger('onProfileUpdateEvent', { noColor: true });
setUserProfile((prev) => {
@@ -116,8 +99,11 @@ export default function MainHeader({ setUserProfile }: headerProps) {
}, [notificationContext]);

useEffect(() => {
const onResNotOk = () => {
forcedLogoutModalRef.current?.showModal();
};
if (setUserProfile) {
fetchMyProfile().then((r) => {
fetchMyProfile(onResNotOk).then((r) => {
setUserProfile(r);
setQuestions_num(r?.questions ?? 0);
setLoginChecked(true);
8 changes: 4 additions & 4 deletions src/app/main/layout.tsx
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ export default function MainLayout({ modal, children }: { children: React.ReactN

const onNotiEv = (ev: CustomEvent<NotificationPayloadTypes>) => {
const notiData = ev.detail;
switch (ev.detail.notification_name) {
switch (notiData.notification_name) {
case 'answer_on_my_question': {
setNoti((prev) => {
return {
@@ -78,7 +78,7 @@ export default function MainLayout({ modal, children }: { children: React.ReactN
});
fetchNoti();

const onEv = (ev: CustomEvent<string | undefined>) => {
const onFetchMoreEv = (ev: CustomEvent<string | undefined>) => {
const id = ev.detail;
fetchAllAnswers({ sort: 'DESC', limit: 25, untilId: id }).then((r) => {
if (r.length === 0) {
@@ -113,12 +113,12 @@ export default function MainLayout({ modal, children }: { children: React.ReactN
});
};

AnswerEv.addFetchMoreRequestEventListener(onEv);
AnswerEv.addFetchMoreRequestEventListener(onFetchMoreEv);
AnswerEv.addAnswerCreatedEventListener(onAnswerCreated);
AnswerEv.addAnswerDeletedEventListener(onAnswerDeleted);
NotificationEv.addNotificationEventListener(onNotiEv);
return () => {
AnswerEv.removeFetchMoreRequestEventListener(onEv);
AnswerEv.removeFetchMoreRequestEventListener(onFetchMoreEv);
AnswerEv.removeAnswerCreatedEventListener(onAnswerCreated);
AnswerEv.removeAnswerDeletedEventListener(onAnswerDeleted);
NotificationEv.removeNotificationEventListener(onNotiEv);
19 changes: 19 additions & 0 deletions src/utils/profile/fetchMyProfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { userProfileMeDto } from '@/app/_dto/fetch-profile/Profile.dto';

export async function fetchMyProfile(onResNotOk?: (code: number) => void): Promise<userProfileMeDto | undefined> {
const user_handle = localStorage.getItem('user_handle');

if (user_handle) {
const res = await fetch('/api/db/fetch-my-profile', {
method: 'GET',
});
if (!res.ok) {
if (onResNotOk) {
onResNotOk(res.status);
}
return;
}
const data = await res.json();
return data;
}
}