Skip to content

Commit

Permalink
Merge pull request #65 from CNU-OOHub/#59/save-file
Browse files Browse the repository at this point in the history
feat: 파일 저장
  • Loading branch information
leecr1215 authored Nov 5, 2022
2 parents 8dd3665 + 2d16a2c commit 963bf54
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 24 deletions.
48 changes: 31 additions & 17 deletions src/api.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,21 +341,33 @@ export const addSharedFileInOrganization = async (
};

// 공유파일 내용 조회
export const useGetSharedFileContent = (organizationName, fileName,filePath) => {
return useQuery(["file"], () => getSharedFileContent(organizationName, fileName,filePath), {
staleTime: 5000,
cacheTime: Infinity,
enabled: filePath.length > 0,
});
export const useGetSharedFileContent = (
organizationName,
fileName,
filePath
) => {
return useQuery(
["file"],
() => getSharedFileContent(organizationName, fileName, filePath),
{
staleTime: 5000,
cacheTime: Infinity,
enabled: filePath.length > 0,
}
);
};

export const getSharedFileContent = async (organizationName, fileName,filePath) => {
export const getSharedFileContent = async (
organizationName,
fileName,
filePath
) => {
try {
const response = await axios.post(
`${SERVER}/api/v1/${organizationName}/sharedFile/${fileName}/info`,
filePath
);
console.log("api test:"+ JSON.stringify(response.data));
console.log("api test:" + JSON.stringify(response.data));
return response.data;
} catch (err) {
throw new Error("fetch shared file content error");
Expand Down Expand Up @@ -457,17 +469,19 @@ export const getAllFile = async () => {
};

// 파일 저장
export const addFile = async (fileInfo) => {
export const addFile = async (contents, originalPath, updatePath) => {
try {
const response = await axiosInstance.post(
`${SERVER}/api/v1/files`,
fileInfo
);
if (response.status === 200) {
alert("파일이 저장되었습니다.");
}
console.log(contents);
console.log(originalPath);
console.log(updatePath);
const response = await axiosInstance.post(`${SERVER}/api/v1/files`, {
contents: contents,
originalPath: originalPath,
updatePath: updatePath,
});
return response;
} catch (error) {
throw new Error("sign up user error");
throw new Error("file save error");
}
};

Expand Down
1 change: 0 additions & 1 deletion src/axiosInterceptor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ function addSubscriber(requestCallback) {

// subscriber 에 담긴 요청들 실행
function executeSubscriber(accessToken) {
console.log(subscribers.length);
subscribers.map((callback) => callback(accessToken));
subscribers = [];
}
Expand Down
49 changes: 43 additions & 6 deletions src/components/organisms/fileView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
AiOutlineFile,
} from "react-icons/ai";
import { IoIosClose } from "react-icons/io";
import { BiSave } from "react-icons/bi";
import Button from "../atoms/button";
import { CONSOLE, TERMINAL } from "../../constants";
import styled from "styled-components";
Expand All @@ -28,8 +29,9 @@ import {
addSharedFileInOrganization,
deleteSharedFile,
useGetSharedFileContent,
addFile,
} from "../../api";
import { useMutation } from "@tanstack/react-query";
import { QueryClient, useMutation } from "@tanstack/react-query";
import { BsFolderPlus } from "react-icons/bs";
import { FiFilePlus } from "react-icons/fi";
import { BiGroup } from "react-icons/bi";
Expand Down Expand Up @@ -172,10 +174,23 @@ const FileView = () => {
data: sharedFileData,
} = useGetSharedFileContent(opendGroupName, openedFileName, filePathInfo);

if (!myFilesIsLoading) {
// console.log(myFiles);
// TODO : 내 파일들의 path 저장
}
// 파일 저장
const queryClient = new QueryClient();

const fileSaveMutation = useMutation(
() =>
addFile(
fileContents.contents,
filePathInfo.filePath,
filePathInfo.filePath
),
{
onSuccess: () => {
alert("파일이 저장되었습니다");
queryClient.invalidateQueries();
},
}
);

useEffect(() => {
if (!getOrganizationIsLoading) {
Expand Down Expand Up @@ -210,6 +225,9 @@ const FileView = () => {
if (localStorage.getItem("isAdmin") === "true") {
setAdmin(true);
}
}, []);

useEffect(() => {
if (isFileClicked) {
refetch().then(() => {
setSharedFileOrganizationName(fileData.organizationName);
Expand Down Expand Up @@ -543,7 +561,7 @@ const FileView = () => {
{openedFileName}
</Text>
<IoIosClose size={20} color={theme.textGreyColor} />
<div style={{ marginLeft: "3vh" }}>
<div style={{ marginLeft: "3vh", display: "flex" }}>
<VscRunAll
size="20"
color="green"
Expand All @@ -552,6 +570,25 @@ const FileView = () => {
executeFile();
}}
/>
{/* 저장 */}
<BiSave
size={25}
style={{ marginLeft: "1rem" }}
color="orange"
onClick={() => {
console.log(fileContents.contents);
console.log(filePathInfo.filePath);
const originalPath = filePathInfo.filePath;
// console.log(fileContents.contents)
if (originalPath !== undefined) {
fileSaveMutation.mutate(
fileContents.contents,
originalPath,
originalPath
);
}
}}
/>
</div>
</FlexRow>
<div style={{ width: "6rem" }}>
Expand Down

0 comments on commit 963bf54

Please sign in to comment.