Skip to content

Commit 9e9bc71

Browse files
committed
* Added file drag and drop support
1 parent b66a248 commit 9e9bc71

File tree

8 files changed

+135
-55
lines changed

8 files changed

+135
-55
lines changed

src/components/App/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import File from "../../routes/File";
1111
import Text from "../../routes/Text";
1212
import Drawerbar from "../Drawerbar";
1313
import {CssBaseline} from "@material-ui/core";
14+
import DropZone from "../DropZone";
1415

1516
function App() {
1617

1718
let themeIndex = useSelector(state => state.MainReducer.themeIndex);
19+
1820
let themeType = "light";
1921
if (themeIndex === 8) {
2022
themeType = "dark";
@@ -32,7 +34,7 @@ function App() {
3234
return (
3335
<ThemeProvider theme={theme}>
3436
<BrowserRouter>
35-
<div className="App">
37+
<DropZone>
3638
<Topbar/>
3739
<Drawerbar/>
3840
<CssBaseline/>
@@ -53,7 +55,7 @@ function App() {
5355
<Home/>
5456
</Route>
5557
</Switch>
56-
</div>
58+
</DropZone>
5759
</BrowserRouter>
5860
</ThemeProvider>
5961
);

src/components/DropZone/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from "react";
2+
import {useHistory} from "react-router-dom";
3+
import {useDispatch, useSelector} from "react-redux";
4+
5+
const DropZone = ({children}) => {
6+
7+
const history = useHistory();
8+
const enabled = useSelector(state => state.MainReducer.canDragDrop);
9+
const dispatch = useDispatch();
10+
11+
/**
12+
* Event that is fired when one or more files are dropped
13+
* @param event The event that contains the drop details
14+
*/
15+
const onDrop = (event) => {
16+
event.preventDefault();
17+
if (!enabled) return;
18+
19+
dispatch({type: 'SET_CURRENT_FILE', payload: event.dataTransfer.files[0]});
20+
history.push("/file");
21+
};
22+
23+
/**
24+
* Event that is fired when a drag over event is detected
25+
* @param event The event that contains the drag over details
26+
*/
27+
const onDragOver = (event) => {
28+
event.preventDefault();
29+
};
30+
31+
return (
32+
<div
33+
onDragOver={onDragOver}
34+
onDrop={onDrop}>
35+
{children}
36+
</div>
37+
);
38+
39+
};
40+
41+
export default DropZone;

src/reducers/CryptoReducer/Actions/CryptoActions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export const resetCryptoReducer = createAction("RESET_CRYPTO_REDUCER");
1212
export const setFileHashes = createAction("SET_FILE_HASHES");
1313
export const setTextHashes = createAction("SET_TEXT_HASHES");
1414
export const setTextInput = createAction("SET_TEXT_INPUT");
15+
export const setCurrentFile = createAction("SET_CURRENT_FILE");

src/reducers/CryptoReducer/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
setSha384State,
88
setSha512State,
99
setSha3State,
10-
setSha224State, resetCryptoReducer, setTextHashes, setFileHashes, setTextInput
10+
setSha224State, resetCryptoReducer, setTextHashes, setFileHashes, setTextInput, setCurrentFile
1111
} from "./Actions/CryptoActions";
1212

1313
let md5 = localStorage['md5'];
@@ -47,6 +47,7 @@ const initState = {
4747
fileHashes: null,
4848
textHashes: null,
4949
textInput: "",
50+
currentFile: null
5051
};
5152

5253
const CryptoReducer = handleActions({
@@ -143,6 +144,13 @@ const CryptoReducer = handleActions({
143144
...state,
144145
textInput: action.payload
145146
}
147+
},
148+
[setCurrentFile](state, action) {
149+
return {
150+
...state,
151+
currentFile: action.payload,
152+
fileHashes: null
153+
}
146154
}
147155
}, initState);
148156

src/reducers/MainReducer/Actions/MainActions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export const resetMainReducer = createAction("RESET_MAIN_REDUCER");
1010
export const setMinimizeStatus = createAction("SET_MINIMIZE_STATUS");
1111
export const setMaximizeStatus = createAction("SET_MAXIMIZE_STATUS");
1212
export const setLanguageButtonStatus = createAction("SET_LANGUAGE_STATUS");
13+
export const setCanDragDrop = createAction("SET_CAN_DRAG_DROP");

src/reducers/MainReducer/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
setThemeIndex,
88
setAutoUpdate,
99
setUpdateChecked,
10-
resetMainReducer, setMinimizeStatus, setMaximizeStatus, setLanguageButtonStatus
10+
resetMainReducer, setMinimizeStatus, setMaximizeStatus, setLanguageButtonStatus, setCanDragDrop
1111
} from './Actions/MainActions';
1212
import de_DE from "../../languages/de_DE";
1313
import nl_NL from "../../languages/nl_NL";
@@ -24,6 +24,7 @@ let autoUpdate = localStorage['autoUpdate'];
2424
let minimizeEnabled = localStorage['minimizeEnabled'];
2525
let maximizeEnabled = localStorage['maximizeEnabled'];
2626
let languageEnabled = localStorage['languageEnabled'];
27+
let canDragDrop = localStorage['canDragDrop'];
2728

2829
if (!languageIndex) {
2930
languageIndex = 1;
@@ -41,6 +42,7 @@ autoUpdate = !autoUpdate || autoUpdate === "true";
4142
minimizeEnabled = !minimizeEnabled || minimizeEnabled === "true";
4243
maximizeEnabled = !maximizeEnabled || maximizeEnabled === "true";
4344
languageEnabled = !languageEnabled || languageEnabled === "true";
45+
canDragDrop = !canDragDrop || canDragDrop === "true";
4446

4547
const initState = {
4648
languageIndex: languageIndex,
@@ -68,7 +70,8 @@ const initState = {
6870
checkedForUpdates: false,
6971
minimizeEnabled: minimizeEnabled,
7072
maximizeEnabled: maximizeEnabled,
71-
languageEnabled: languageEnabled
73+
languageEnabled: languageEnabled,
74+
canDragDrop: canDragDrop
7275
};
7376

7477
const MainReducer = handleActions({
@@ -118,6 +121,7 @@ const MainReducer = handleActions({
118121
localStorage['minimizeEnabled'] = true;
119122
localStorage['maximizeEnabled'] = true;
120123
localStorage['languageEnabled'] = true;
124+
localStorage['canDragDrop'] = true;
121125

122126
return {
123127
...state,
@@ -126,7 +130,8 @@ const MainReducer = handleActions({
126130
autoUpdate: true,
127131
minimizeEnabled: true,
128132
maximizeEnabled: true,
129-
languageEnabled: true
133+
languageEnabled: true,
134+
canDragDrop: true
130135
}
131136
},
132137
[setMinimizeStatus](state, action) {
@@ -149,6 +154,13 @@ const MainReducer = handleActions({
149154
...state,
150155
languageEnabled: action.payload
151156
}
157+
},
158+
[setCanDragDrop](state,action) {
159+
localStorage['canDragDrop'] = action.payload;
160+
return {
161+
...state,
162+
canDragDrop : action.payload
163+
}
152164
}
153165
}, initState);
154166

src/routes/File/index.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useEffect, useState} from "react";
1+
import React, {useEffect, useState, useRef} from "react";
22
import {useDispatch, useSelector} from "react-redux";
33
import {Checkbox, FormControlLabel, makeStyles, Paper} from "@material-ui/core";
44
import Container from "@material-ui/core/Container";
@@ -13,7 +13,6 @@ import CryptoJs from "crypto-js/crypto-js";
1313
import {FileDataReader} from "../../utils/FileDataReader";
1414
import {CryptoCalculator} from "../../utils/CryptoCalculator";
1515
import BackButton from "../../components/BackButton";
16-
import Input from '@material-ui/core/Input';
1716

1817
const useStyles = makeStyles(theme => ({
1918
heroContent: {
@@ -43,14 +42,16 @@ const useStyles = makeStyles(theme => ({
4342
const File = () => {
4443

4544
const hashes = useSelector(state => state.CryptoReducer.fileHashes);
45+
const file = useSelector(state => state.CryptoReducer.currentFile);
4646
const language = useSelector(state => state.MainReducer.languages[state.MainReducer.languageIndex]);
4747
const dispatch = useDispatch();
4848
const classes = useStyles();
4949

50-
const [file, setFile] = useState(null);
5150
const [compare, setCompare] = useState(false);
5251
const [compareHash, setCompareHash] = useState("");
5352

53+
const fileRef = useRef(null);
54+
5455
const md5 = useSelector(state => state.CryptoReducer.md5);
5556
const sha1 = useSelector(state => state.CryptoReducer.sha1);
5657
const sha224 = useSelector(state => state.CryptoReducer.sha224);
@@ -80,7 +81,6 @@ const File = () => {
8081
<CopyPasteMenu id={1} copyData={() => navigator.clipboard.writeText(compareHash)}
8182
pasteData={() => pasteData(setCompareHash)}>
8283
<TextField
83-
id="outlined-basic"
8484
style={{width: '100%'}}
8585
margin="normal"
8686
value={compareHash}
@@ -127,10 +127,18 @@ const File = () => {
127127
* Clear the user interface
128128
*/
129129
const clearData = () => {
130-
setFile(null);
131130
setCompare(false);
132131
setCompareHash("");
133-
dispatch({type: "SET_FILE_HASHES", payload: null});
132+
dispatch({type: 'SET_CURRENT_FILE', payload: null});
133+
};
134+
135+
/**
136+
* Change the currently selected file
137+
* @param event The event that called this function
138+
*/
139+
const onFileChange = function(event) {
140+
dispatch({type: 'SET_CURRENT_FILE', payload: event.target.files[0]});
141+
fileRef.current.value = "";
134142
};
135143

136144
return (
@@ -154,7 +162,19 @@ const File = () => {
154162
{language.input}
155163
</Typography>
156164
<Paper className={classes.paper}>
157-
<Input type={"file"} onChange={e => setFile(e.target.files[0])} disableUnderline={true} />
165+
<TextField
166+
margin="normal"
167+
onClick={() => fileRef.current.click()}
168+
disabled
169+
id="filled-disabled"
170+
value={file && file.path ? file.path : ""}
171+
variant="outlined"
172+
/>
173+
174+
<input ref={fileRef} type="file" onChange={onFileChange}
175+
style={{display: 'none'}}/>
176+
177+
<Button color={"primary"} variant={"contained"} onClick={() => fileRef.current.click()}>{language.select}</Button>
158178

159179
<FormControlLabel
160180
control={

0 commit comments

Comments
 (0)