Skip to content

Commit

Permalink
Merge pull request #87 from tempfiles-Team/62-파일별-다운로드-페이지-라우팅
Browse files Browse the repository at this point in the history
62 파일별 다운로드 페이지 라우팅
  • Loading branch information
ananjaemin committed Nov 17, 2022
2 parents 9d26d26 + 0b369e1 commit eb74bbb
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const App: React.FC = () => (
<Route path="/delete" element={<DeletePage />} />
<Route path="/filelist" element={<FileListPage />} />
<Route path="/api/*" element={<ApiPage />} />
<Route path="/checkpw" element={<CheckPasswordPage />} />
<Route path="/checkpw/:checkfileid" element={<CheckPasswordPage />} />
<Route path="*" element={<NotFoundPage />} />
</Route>
</Routes>
Expand Down
48 changes: 35 additions & 13 deletions src/pages/checkpw/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import axios from 'axios';
import React, { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { useDispatch } from 'react-redux';
import { useNavigate } from 'react-router-dom';
import { useParams } from 'react-router-dom';
import { toast } from 'react-toastify';
import { bindActionCreators } from 'redux';

import { FileBox, Button } from '../../components';
import { actionCreators } from '../../state';
import { RootState } from '../../state/reducers';
import { getDate } from '../../utils';
import * as S from './styled';

export const CheckPasswordPage: React.FC = () => {
const checkPasswordFileProps = useSelector((state: RootState) => state.CheckPasswordFileProps);
const [password, setPassword] = useState('');
const { year, month, day } = getDate(checkPasswordFileProps.uploadDate);
const { checkfileid } = useParams<{ checkfileid: string }>();
const [fileProps, setFileProps] = useState({
filename: '',
size: '',
uploadDate: { year: 0, month: 0, day: 0 },
});
// const { year, month, day } = getDate(checkPasswordFileProps.uploadDate);
const dispatch = useDispatch();
const { SetDownloadFileProps } = bindActionCreators(actionCreators, dispatch);

Expand All @@ -30,15 +34,14 @@ export const CheckPasswordPage: React.FC = () => {
} else {
await axios({
method: 'get',
url: `${process.env.REACT_APP_BACKEND_BASEURL}/checkpw/${checkPasswordFileProps.fileId}?pw=${password}`,
url: `${process.env.REACT_APP_BACKEND_BASEURL}/checkpw/${checkfileid}?pw=${password}`,
})
.then((res) => {
SetDownloadFileProps({
fileId: checkPasswordFileProps.fileId,
isEncrypted: checkPasswordFileProps.isEncrypted,
isEncrypted: true,
token: res.data.token,
});
navigate('/download');
navigate(`/download/${checkfileid}`);
toast.success('통과!', {
autoClose: 1000,
position: toast.POSITION.BOTTOM_RIGHT,
Expand All @@ -54,15 +57,34 @@ export const CheckPasswordPage: React.FC = () => {
}
};
useEffect(() => {
if (checkPasswordFileProps.filename === null || checkPasswordFileProps.fileId === null) {
navigate('/');
}
const getFileProps = async () => {
await axios({
method: 'get',
url: `${process.env.REACT_APP_BACKEND_BASEURL}/file/${checkfileid}`,
})
.then((res) => {
setFileProps({
filename: res.data.filename,
size: res.data.size,
uploadDate: getDate(res.data.uploadDate),
});
})
.catch((err) => {
console.log(err);
navigate('/');
toast.error('error 문의해주세요...', {
autoClose: 1000,
position: toast.POSITION.BOTTOM_RIGHT,
});
});
};
getFileProps();
});
return (
<S.CheckPasswordPageContainer>
<FileBox>
파일이름:{checkPasswordFileProps.filename} / 크기:{checkPasswordFileProps.size} / 업로드된
날짜: {year}-{month}-{day}
파일이름:{fileProps.filename} / 크기:{fileProps.size} / 업로드된 날짜:{' '}
{fileProps.uploadDate.year}-{fileProps.uploadDate.month}-{fileProps.uploadDate.day}
</FileBox>
<S.PasswordInputSection>
<S.CheckPasswordInput
Expand Down
30 changes: 15 additions & 15 deletions src/pages/download/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,24 @@ export const DownloadPage: React.FC = () => {
await axios({
method: 'get',
url: `${process.env.REACT_APP_BACKEND_BASEURL}/file/${fileid}${
downloadFileProps.isEncrypted ? `?token=${downloadFileProps.token}` : ''
downloadFileProps.isEncrypted ? `?token=${downloadFileProps.token}` : ``
}`,
})
.then((res) => {
setLoading(false);
setFileProps({
filename: res.data.filename,
// fileId: res.data.fileId,
size: getFileSize(res.data.size),
uploadDate: getDate(res.data.uploadDate),
download_url: res.data.download_url,
delete_url: res.data.delete_url,
isEncrypted: res.data.isEncrypted,
});
if (res.data.isEncrypted && !res.data.provide_token) {
navigate(`/checkpw/${fileid}`);
} else {
setFileProps({
filename: res.data.filename,
// fileId: res.data.fileId,
size: getFileSize(res.data.size),
uploadDate: getDate(res.data.uploadDate),
download_url: res.data.download_url,
delete_url: res.data.delete_url,
isEncrypted: res.data.isEncrypted,
});
}
})
.catch((err) => {
navigate('/');
Expand Down Expand Up @@ -87,11 +91,7 @@ export const DownloadPage: React.FC = () => {
</a>
<Button
click={() => {
navigator.clipboard.writeText(
`${fileProps.download_url}${
fileProps.isEncrypted ? `?token=${downloadFileProps.token}` : ''
}`,
);
navigator.clipboard.writeText(window.location.href);
toast.success('복사 완료', {
autoClose: 1000,
position: toast.POSITION.BOTTOM_RIGHT,
Expand Down
14 changes: 2 additions & 12 deletions src/pages/filelist/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ export const FileListPage: React.FC = () => {
const [loading, setLoading] = useState(false);
const SkeletonUIRandomWidth = ['50', '55', '60', '65', '70', '75', '80'];
const dispatch = useDispatch();
const { SetDownloadFileProps, SetCheckPasswordFileProps } = bindActionCreators(
actionCreators,
dispatch,
);
const { SetDownloadFileProps } = bindActionCreators(actionCreators, dispatch);
const [fileList, setFileList] = useState<any[]>();
const getFileList = async () => {
await axios({
Expand Down Expand Up @@ -52,14 +49,7 @@ export const FileListPage: React.FC = () => {
isEncrypted={item.isEncrypted}
click={() => {
if (item.isEncrypted) {
SetCheckPasswordFileProps({
filename: item.filename,
fileId: item.fileId,
size: getFileSize(item.size),
uploadDate: item.uploadDate,
isEncrypted: item.isEncrypted,
});
navigate('/checkpw');
navigate(`/checkpw/${item.fileId}`);
} else {
SetDownloadFileProps({
fileId: item.fileId,
Expand Down
1 change: 0 additions & 1 deletion src/state/action-types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export enum ActionType {
DeleteFileProps = 'DeleteFileProps',
DownloadFileProps = 'DownloadFileProps',
CheckPasswordFileProps = 'CheckPasswordFileProps',
}
7 changes: 0 additions & 7 deletions src/state/actions-creators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@ export const SetDownloadFileProps = (props: any) => (dispatch: Dispatch<Action>)
props: props,
});
};

export const SetCheckPasswordFileProps = (props: any) => (dispatch: Dispatch<Action>) => {
dispatch({
type: ActionType.CheckPasswordFileProps,
props: props,
});
};
6 changes: 1 addition & 5 deletions src/state/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@ interface DownloadFileProps {
props: any;
}

interface CheckPasswordFileProps {
type: ActionType.CheckPasswordFileProps;
props: any;
}
export type Action = DeleteFileProps | DownloadFileProps | CheckPasswordFileProps;
export type Action = DeleteFileProps | DownloadFileProps;
16 changes: 0 additions & 16 deletions src/state/reducers/CheckPasswordFileProps.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/state/reducers/DownloadFileProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Action } from '../actions';

const reducer = (
state: any = {
fileId: null,
token: null,
isEncrypted: null,
},
Expand Down
2 changes: 0 additions & 2 deletions src/state/reducers/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { combineReducers } from 'redux';

import CheckPasswordFileProps from './CheckPasswordFileProps';
import DeleteFileName from './DeleteFileProps';
import DownloadFileProps from './DownloadFileProps';

const reducers = combineReducers({
DeleteFileName: DeleteFileName,
DownloadFileProps: DownloadFileProps,
CheckPasswordFileProps: CheckPasswordFileProps,
});

export default reducers;
Expand Down

0 comments on commit eb74bbb

Please sign in to comment.