Skip to content

Commit

Permalink
🎨 fetchMyProfile 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
yunochi committed Dec 19, 2024
1 parent bd61f2f commit 9c8475f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
24 changes: 5 additions & 19 deletions src/app/main/_header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>>;
Expand All @@ -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) => {
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/app/main/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
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;
}
}

0 comments on commit 9c8475f

Please sign in to comment.