Skip to content

Commit

Permalink
feat: save as all qrs button implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sitarass committed Dec 24, 2023
1 parent d179b24 commit 06ba453
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
33 changes: 32 additions & 1 deletion public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function createWindow() {
const historyDirName = `${app.getPath("appData")}/QRsHistory`;

app.whenReady().then(() => {

createWindow();

if (!fs.existsSync(historyDirName)) {
Expand Down Expand Up @@ -110,3 +109,35 @@ ipcMain.on("saveQRfile", (event, fileData) => {
console.log(err);
});
});

ipcMain.on("saveQRfilesFolder", (event, files) => {
const options = {
title: "Save QR",
defaultPath: app.getPath("documents"),
};

dialog
.showSaveDialog(mainWindow, options)
.then(({ filePath: dir }) => {
if (!dir) return;

if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}

files?.forEach((file) => {
const fileName = `${dir}/${file?.fileName}.${file?.extension}`;
imageConverter(
fs.readFileSync(file?.file),
file?.extension,
file?.width,
file?.height
).then((data) => {
fs.writeFileSync(fileName, data);
});
});
})
.catch((err) => {
console.log(err);
});
});
8 changes: 6 additions & 2 deletions public/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("api", {
send: (channel, data) => {
// whitelist channels
const validChannels = ["convertUrlsToQRs", "saveQRfile"];
const validChannels = [
"convertUrlsToQRs",
"saveQRfile",
"saveQRfilesFolder",
];
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, data);
}
},
receive: (channel, func) => {
const validChannels = ["qrData", "saveQRfile"];
const validChannels = ["qrData", "saveQRfile", "saveQRfilesFolder"];
if (validChannels.includes(channel)) {
// Deliberately strip event as it includes `sender`
ipcRenderer.on(channel, (event, ...args) => func(...args));
Expand Down
9 changes: 8 additions & 1 deletion src/views/QRGenerator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const QRGenerator = () => {
window.api.send("convertUrlsToQRs", urlsArray);
};

const handleSaveAsAll = () => {
window.api.send("saveQRfilesFolder", qrData);
};

return (
<QRGeneratorLayout>
<TextInput
Expand All @@ -52,7 +56,10 @@ const QRGenerator = () => {
tableStyles={styles.tableInnerContainer}
/>
{displaySaveAsAllButton && (
<SecondaryButton className={styles.saveAsButton} onClick={() => {}}>
<SecondaryButton
className={styles.saveAsButton}
onClick={handleSaveAsAll}
>
Save as all
</SecondaryButton>
)}
Expand Down

0 comments on commit 06ba453

Please sign in to comment.