From 560ae5be74861cc3fc8da8fea439813759377378 Mon Sep 17 00:00:00 2001
From: cv <34649812+cpvalente@users.noreply.github.com>
Date: Sun, 6 Jun 2021 22:25:23 +0200
Subject: [PATCH] feat: upload file
---
client/src/app/api/ontimeApi.js | 5 +-
client/src/features/menu/MenuBar.jsx | 46 ++++++++++++++++++-
.../features/menu/buttons/UploadIconBtn.jsx | 19 ++++++++
server/main.js | 3 +-
server/src/controllers/ontimeController.js | 44 ++++++++++++------
server/src/routes/ontimeRouter.js | 9 +++-
6 files changed, 107 insertions(+), 19 deletions(-)
create mode 100644 client/src/features/menu/buttons/UploadIconBtn.jsx
diff --git a/client/src/app/api/ontimeApi.js b/client/src/app/api/ontimeApi.js
index 4ebc5f73b..71b4678b7 100644
--- a/client/src/app/api/ontimeApi.js
+++ b/client/src/app/api/ontimeApi.js
@@ -28,7 +28,6 @@ export const downloadEvents = async () => {
};
export const uploadEvents = async (file) => {
- console.log('uploading', file);
const formData = new FormData();
formData.append('jsondb', file); // appending file
await axios
@@ -40,3 +39,7 @@ export const uploadEvents = async (file) => {
.then((res) => console.log(res.data))
.catch((err) => console.error(err));
};
+
+export const uploadEventsWithPath = async (filepath) => {
+ await axios.post(ontimeURL + '/dbpath', { path: filepath });
+};
diff --git a/client/src/features/menu/MenuBar.jsx b/client/src/features/menu/MenuBar.jsx
index cc400a978..3d32ea0bf 100644
--- a/client/src/features/menu/MenuBar.jsx
+++ b/client/src/features/menu/MenuBar.jsx
@@ -1,4 +1,6 @@
-import { downloadEvents } from 'app/api/ontimeApi';
+import { useMutation, useQueryClient } from 'react-query';
+import { downloadEvents, uploadEventsWithPath } from 'app/api/ontimeApi';
+import { EVENTS_TABLE } from 'app/api/apiConstants';
import DownloadIconBtn from './buttons/DownloadIconBtn';
import SettingsIconBtn from './buttons/SettingsIconBtn';
import InfoIconBtn from './buttons/InfoIconBtn';
@@ -7,15 +9,49 @@ import MinIconBtn from './buttons/MinIconBtn';
import QuitIconBtn from './buttons/QuitIconBtn';
import style from './MenuBar.module.css';
import HelpIconBtn from './buttons/HelpIconBtn';
-const { ipcRenderer } = window.require('electron');
+import UploadIconBtn from './buttons/UploadIconBtn';
+
+const { ipcRenderer, remote } = window.require('electron');
export default function MenuBar(props) {
const { onOpen, onClose } = props;
+ const queryClient = useQueryClient();
+ const uploaddbPath = useMutation(uploadEventsWithPath, {
+ onSettled: () => {
+ queryClient.invalidateQueries(EVENTS_TABLE);
+ },
+ });
const handleDownload = () => {
downloadEvents();
};
+ const handleUpload = () => {
+ remote.dialog
+ .showOpenDialog({
+ title: 'Select the File to be uploaded',
+ buttonLabel: 'Upload',
+ filters: [
+ {
+ name: 'Text Files',
+ extensions: ['json'],
+ },
+ ],
+ // Specifying the File Selector Property
+ properties: ['openFile'],
+ })
+ .then((file) => {
+ // Stating whether dialog operation was
+ // cancelled or not.
+ if (!file.canceled) {
+ uploaddbPath.mutate(file.filePaths[0].toString());
+ }
+ })
+ .catch((err) => {
+ console.log(err);
+ });
+ };
+
const handleIPC = (action) => {
switch (action) {
case 'min':
@@ -55,11 +91,17 @@ export default function MenuBar(props) {
clickhandler={() => handleIPC('help')}
/>