Skip to content

Commit

Permalink
refactor: query 응답 관련 중복 코드를 함수화하여 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwhdtjr0457 committed Dec 13, 2023
1 parent 1d5ef53 commit 19fff2f
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 456 deletions.
2 changes: 1 addition & 1 deletion BE/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class AuthService {
): Promise<string> {
const accessTokenPayload = { userId };
const accessToken = await this.jwtService.sign(accessTokenPayload, {
expiresIn: "1h",
expiresIn: "5s",
});

const refreshTokenPayload = {
Expand Down
53 changes: 19 additions & 34 deletions FE/src/components/DiaryModal/DiaryAnalysisModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Tag from "../../styles/Modal/Tag";
import leftIcon from "../../assets/leftIcon.svg";
import rightIcon from "../../assets/rightIcon.svg";
import logoNoText from "../../assets/logo-notext.svg";
import handleResponse from "../../utils/handleResponse";

function DiaryAnalysisModal() {
const [userState, setUserState] = useRecoilState(userAtom);
Expand Down Expand Up @@ -40,40 +41,24 @@ function DiaryAnalysisModal() {
Authorization: `Bearer ${userState.accessToken}`,
},
},
).then((res) => {
if (res.status === 200) {
return res.json();
}
if (res.status === 403) {
setDiaryState((prev) => ({
...prev,
isRedirect: true,
}));
}
if (res.status === 401) {
return fetch(`${process.env.REACT_APP_BACKEND_URL}/auth/reissue`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${userState.accessToken}`,
},
})
.then((res) => res.json())
.then((data) => {
if (localStorage.getItem("accessToken")) {
localStorage.setItem("accessToken", data.accessToken);
}
if (sessionStorage.getItem("accessToken")) {
sessionStorage.setItem("accessToken", data.accessToken);
}
setUserState((prev) => ({
...prev,
accessToken: data.accessToken,
}));
});
}
throw new Error("error");
});
).then((res) =>
handleResponse(res, userState.accessToken, {
successStatus: 200,
onSuccessCallback: () => res.json(),
on403Callback: () => {
setDiaryState((prev) => ({
...prev,
isRedirect: true,
}));
},
on401Callback: (accessToken) => {
setUserState((prev) => ({
...prev,
accessToken,
}));
},
}),
);
}

const { data: tagsRankData, refetch: tagsRankRefetch } = useQuery(
Expand Down
54 changes: 19 additions & 35 deletions FE/src/components/DiaryModal/DiaryCreateModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Calendar from "./Calendar";
import close from "../../assets/close.svg";
import getFormattedDate from "../../utils/utils";
import ModalBackground from "../ModalBackground/ModalBackground";
import handleResponse from "../../utils/handleResponse";

function DiaryCreateModal(props) {
const { refetch } = props;
Expand Down Expand Up @@ -67,41 +68,24 @@ function DiaryCreateModal(props) {
},
body: JSON.stringify(formattedDiaryData),
})
.then((res) => {
if (res.status === 201) {
return res.json();
}
if (res.status === 403) {
setDiaryState((prev) => ({
...prev,
isRedirect: true,
}));
}
if (res.status === 401) {
return fetch(`${process.env.REACT_APP_BACKEND_URL}/auth/reissue`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${userState.accessToken}`,
},
})
.then((res) => res.json())
.then((data) => {
if (localStorage.getItem("accessToken")) {
localStorage.setItem("accessToken", data.accessToken);
}
if (sessionStorage.getItem("accessToken")) {
sessionStorage.setItem("accessToken", data.accessToken);
}
setUserState((prev) => ({
...prev,
accessToken: data.accessToken,
}));
createDiary({ diaryData, accessToken: data.accessToken });
});
}
throw new Error("error");
})
.then((res) =>
handleResponse(res, userState.accessToken, {
successStatus: 201,
onSuccessCallback: () => res.json(),
on403Callback: () =>
setDiaryState((prev) => ({
...prev,
isRedirect: true,
})),
on401Callback: (accessToken) => {
setUserState((prev) => ({
...prev,
accessToken,
}));
createDiary({ diaryData, accessToken });
},
}),
)
.then(() => {
refetch();
setDiaryState((prev) => ({
Expand Down
59 changes: 22 additions & 37 deletions FE/src/components/DiaryModal/DiaryDeleteModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import diaryAtom from "../../atoms/diaryAtom";
import userAtom from "../../atoms/userAtom";
import lastPageAtom from "../../atoms/lastPageAtom";
import ModalWrapper from "../../styles/Modal/ModalWrapper";
import handleResponse from "../../utils/handleResponse";

function DiaryDeleteModal(props) {
const { refetch, pointsRefetch } = props;
Expand All @@ -24,44 +25,28 @@ function DiaryDeleteModal(props) {
},
},
)
.then((res) => {
if (res.status === 204) {
return res;
}
if (res.status === 403) {
setDiaryState((prev) => ({
...prev,
isRedirect: true,
}));
}
if (res.status === 401) {
return fetch(`${process.env.REACT_APP_BACKEND_URL}/auth/reissue`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${data.accessToken}`,
},
})
.then((res) => res.json())
.then((data) => {
if (localStorage.getItem("accessToken")) {
localStorage.setItem("accessToken", data.accessToken);
}
if (sessionStorage.getItem("accessToken")) {
sessionStorage.setItem("accessToken", data.accessToken);
}
setUserState((prev) => ({
...prev,
accessToken: data.accessToken,
}));
deleteDiary({
diaryUuid: diaryState.diaryUuid,
accessToken: data.accessToken,
});
.then((res) =>
handleResponse(res, data.accessToken, {
successStatus: 204,
onSuccessCallback: () => {},
on403Callback: () => {
setDiaryState((prev) => ({
...prev,
isRedirect: true,
}));
},
on401Callback: (accessToken) => {
setUserState((prev) => ({
...prev,
accessToken,
}));
deleteDiary({
diaryUuid: diaryState.diaryUuid,
accessToken,
});
}
throw new Error("error");
})
},
}),
)
.then(() => {
refetch();
pointsRefetch();
Expand Down
53 changes: 19 additions & 34 deletions FE/src/components/DiaryModal/DiaryReadModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import editIcon from "../../assets/edit.svg";
import deleteIcon from "../../assets/delete.svg";
import close from "../../assets/close.svg";
import ModalBackground from "../ModalBackground/ModalBackground";
import handleResponse from "../../utils/handleResponse";

async function getDiary(accessToken, diaryUuid, setUserState, setDiaryState) {
return fetch(`${process.env.REACT_APP_BACKEND_URL}/diaries/${diaryUuid}`, {
Expand All @@ -22,40 +23,24 @@ async function getDiary(accessToken, diaryUuid, setUserState, setDiaryState) {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
}).then((res) => {
if (res.status === 200) {
return res.json();
}
if (res.status === 403) {
setDiaryState((prev) => ({
...prev,
isRedirect: true,
}));
}
if (res.status === 401) {
return fetch(`${process.env.REACT_APP_BACKEND_URL}/auth/reissue`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
})
.then((res) => res.json())
.then((data) => {
if (localStorage.getItem("accessToken")) {
localStorage.setItem("accessToken", data.accessToken);
}
if (sessionStorage.getItem("accessToken")) {
sessionStorage.setItem("accessToken", data.accessToken);
}
setUserState((prev) => ({
...prev,
accessToken: data.accessToken,
}));
});
}
throw new Error("error");
});
}).then((res) =>
handleResponse(res, accessToken, {
successStatus: 200,
onSuccessCallback: () => res.json(),
on403Callback: () => {
setDiaryState((prev) => ({
...prev,
isRedirect: true,
}));
},
on401Callback: (accessToken) => {
setUserState((prev) => ({
...prev,
accessToken,
}));
},
}),
);
}

function DiaryReadModal(props) {
Expand Down
70 changes: 28 additions & 42 deletions FE/src/components/DiaryModal/DiaryUpdateModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Calendar from "./Calendar";
import close from "../../assets/close.svg";
import getFormattedDate from "../../utils/utils";
import ModalBackground from "../ModalBackground/ModalBackground";
import handleResponse from "../../utils/handleResponse";

function DiaryUpdateModal(props) {
const { refetch } = props;
Expand Down Expand Up @@ -93,49 +94,34 @@ function DiaryUpdateModal(props) {
Authorization: `Bearer ${data.accessToken}`,
},
body: JSON.stringify(formattedDiaryData),
}).then((res) => {
if (res.status === 204) {
refetch();
setDiaryState((prev) => ({
...prev,
isRead: true,
}));
}
if (res.status === 403) {
console.log("권한 없음");
setDiaryState((prev) => ({
...prev,
isRedirect: true,
}));
}
if (res.status === 401) {
return fetch(`${process.env.REACT_APP_BACKEND_URL}/auth/reissue`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${data.accessToken}`,
},
})
.then((res) => res.json())
.then((data) => {
if (localStorage.getItem("accessToken")) {
localStorage.setItem("accessToken", data.accessToken);
}
if (sessionStorage.getItem("accessToken")) {
sessionStorage.setItem("accessToken", data.accessToken);
}
setUserState((prev) => ({
...prev,
accessToken: data.accessToken,
}));
updateDiary({
diaryData: diaryData,
accessToken: data.accessToken,
});
}).then((res) =>
handleResponse(res, userState.accessToken, {
successStatus: 204,
onSuccessCallback: () => {
refetch();
setDiaryState((prev) => ({
...prev,
isRead: true,
}));
},
on403Callback: () => {
setDiaryState((prev) => ({
...prev,
isRedirect: true,
}));
},
on401Callback: (accessToken) => {
setUserState((prev) => ({
...prev,
accessToken,
}));
updateDiary({
diaryData: diaryData,
accessToken,
});
}
throw new Error("error");
});
},
}),
);
}

async function getDiary(accessToken, diaryUuid) {
Expand Down
Loading

0 comments on commit 19fff2f

Please sign in to comment.