-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEAT] 댓글 번역 추가 및 번역 로직 수정
- Loading branch information
Showing
12 changed files
with
203 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { authApi } from '../axios-instance'; | ||
import { API_DOMAINS } from '@/constants/api'; | ||
export const translateText = async ({ content }) => { | ||
export const translateText = async (content) => { | ||
const response = await authApi.post(API_DOMAINS.TRANSLATE_TEXT, content); | ||
return response.data.data | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,27 @@ | ||
import TranslateImg from '@/assets/imgs/translate.svg'; | ||
|
||
import TranslateImg from '@/assets/imgs/translate.svg?react'; | ||
import { cn } from '@/utils/cn'; | ||
/** 번역 컴포넌트 | ||
* @param {Object} props - 컴포넌트의 props | ||
* @param {'post' | 'content' | 'text'} props.translateType - 번역 타입 (게시된 post, 작성 중인 content, 단순 text) | ||
* @param {number | string} props.value - post 번역 시 number, 작성 중인 글 본문이나 단순 text 번역 시 string | ||
* @param {function} props.setValue - 해당 함수로 번역된 텍스트 전달 | ||
* @param {function} props.handleTranslate | ||
* @param {boolean} props.state - 해당 함수로 번역된 텍스트 전달 | ||
* @param {function} props.setState - 해당 함수로 번역된 텍스트 전달 | ||
* @param {'small' | 'large'} props.size - 크기 | ||
* @param {'title' | 'base'} props.color - 색상 | ||
*/ | ||
export const TranslateButton = ({ translateType, value, setValue }) => { | ||
const translate = () => { | ||
if (value || value === 0) { | ||
if (translateType === 'post') { | ||
// postId 전달 시 게시글 번역 - value는 number | ||
console.log(value); | ||
} else if (translateType === 'content') { | ||
// 작성 중인 게시글 본문 번역 - value는 string | ||
// 모달 구현해야함 | ||
// 사용자가 선택한 언어 있으면 백에 알려주기 | ||
setValue(value); // 현재는 번역 안된 상태로 전달 | ||
} else if (translateType === 'text') { | ||
// 영어로 번역 - value는 string | ||
setValue(value); // 현재는 번역 안된 상태로 전달 | ||
} | ||
} | ||
}; | ||
|
||
export const TranslateButton = ({ handleTranslate, state, setState, size, color }) => { | ||
return ( | ||
<button type="button" onClick={translate}> | ||
<img src={TranslateImg} alt="" className="h-[1.375rem] w-[1.375rem]" /> | ||
<button type="button" | ||
onClick={(e) => { | ||
e.stopPropagation(); | ||
state ? setState(false) : handleTranslate(); | ||
}}> | ||
<TranslateImg alt="Translate" | ||
className={cn({ | ||
"w-[1.125rem] h-[1.125rem]": size === "small", | ||
"w-6 h-6": size === "large", | ||
"text-neutral-base": color === "base", | ||
"text-neutral-title": color === "title" | ||
})} /> | ||
</button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import { useState } from "react"; | ||
import { translateText } from "@/apis/translate/translateText.api"; | ||
import { QUERY_KEYS } from "@/constants/api"; | ||
|
||
export const useCommentTranslate = (commentId) => { | ||
const [translateState, setTranslateState] = useState(false); | ||
const [translatedContent, setTranslatedContent] = useState(null); | ||
const queryClient = useQueryClient(); | ||
const { mutate: commentTranslate, isPending: translatePending } = useMutation({ | ||
mutationFn: async (content) => { | ||
return await translateText({ content }); | ||
}, | ||
onSuccess: (translatedContent) => { | ||
queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET_TRANSLATE_TEXT, commentId] }); | ||
setTranslateState(true); | ||
setTranslatedContent(translatedContent.content); | ||
}, | ||
}); | ||
|
||
const handleTranslate = (content) => { | ||
if (translatedContent) { | ||
setTranslateState(true); | ||
} else { | ||
commentTranslate(content); | ||
} | ||
}; | ||
|
||
return { | ||
translateState, | ||
setTranslateState, | ||
translatedContent, | ||
setTranslatedContent, | ||
translatePending, | ||
handleTranslate, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { translatePost } from "@/apis/translate/translatePost.api"; | ||
import { QUERY_KEYS } from "@/constants/api"; | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import { useState } from "react"; | ||
|
||
export const usePostTranslate = (postId) => { | ||
const queryClient = useQueryClient(); | ||
const [translateState, setTranslateState] = useState(false); | ||
const [translatedPost, setTranslatedPost] = useState(null); | ||
const handleTranslate = () => { | ||
if (translatedPost) { | ||
setTranslateState(true); | ||
} | ||
else { | ||
postTranslate(postId); | ||
} | ||
} | ||
const { mutate: postTranslate, isPending: translatePending } = useMutation({ | ||
mutationFn: async () => { | ||
return await translatePost({ postId: postId }) | ||
}, | ||
onSuccess: (translatedPost) => { | ||
queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET_TRANSLATE_POST, postId] }); | ||
setTranslateState(true); | ||
setTranslatedPost(translatedPost) | ||
} | ||
}) | ||
return { | ||
translateState, | ||
setTranslateState, | ||
translatedPost, | ||
setTranslatedPost, | ||
translatePending, | ||
handleTranslate | ||
} | ||
} |
Oops, something went wrong.