-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.js
122 lines (108 loc) · 3.15 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const fs = require("fs");
const { app, BrowserWindow, ipcMain, Menu } = require("electron");
const { generateDocument } = require("./src/docx/generateDocument");
const { DocumentData } = require("./src/utils/utils");
const path = require("path");
// Specifies the enviroment variable
const inDevelopmentMode = process.env.MODE === "dev";
const { getSchoolTypeList, getSchoolList } = require("./src/db/api");
/***************/
/* Main Window */
/***************/
app.on("ready", () => {
// Configure the main window
let mainWindow = new BrowserWindow({
width: 1200,
height: 665,
icon: path.join(__dirname, "assets/Alinka-logo.png"),
webPreferences: {
backgroundThrottling: false
}
});
// Load the apropriate file depending on env mode
mainWindow.loadURL(
inDevelopmentMode
? `http://localhost:9000`
: `file://${__dirname}/build/index.html`
);
// Get list of school types from data base.
ipcMain.on("db:schoolType", async () => {
mainWindow.webContents.send("sendSchoolTypes", await getSchoolTypeList())
})
ipcMain.on("db:schoolList", async (event, schoolType) => {
mainWindow.webContents.send("sendSchoolList", await getSchoolList(schoolType))
})
// Build Menu from template and insert it
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
Menu.setApplicationMenu(mainMenu);
// Display develooper tools in dev mode
if (inDevelopmentMode) {
mainWindow.webContents.openDevTools();
const {
default: installExtension,
REACT_DEVELOPER_TOOLS
} = require("electron-devtools-installer");
installExtension(REACT_DEVELOPER_TOOLS)
.then(name => {
console.log(`Added Extension: ${name}`);
})
.catch(err => {
console.log("An error occurred: ", err);
});
}
// Quit app when closed
mainWindow.on("closed", () => {
app.quit();
});
});
/********/
/* Menu */
/********/
const mainMenuTemplate = [
{
label: "File",
submenu: [
{
label: "Quit",
accelerator: process.platform == "darwin" ? "Command+Q" : "Ctrl+Q",
click() {
app.quit();
}
}
]
}
];
// If mac add empty object to menu
if (process.platform === "darwin") {
mainMenuTemplate.unshift({});
}
// Add developer tools item in dev mode
if (inDevelopmentMode) {
mainMenuTemplate.push({
label: "Developer Tools",
submenu: [
{
label: "Toggle DevTools",
accelerator: process.platform == "darwin" ? "Command+I" : "Ctrl+I",
click(item, focusedWindow) {
focusedWindow.toggleDevTools();
}
},
{
role: "reload"
}
]
});
}
// Render documents
ipcMain.on("print:value", (event, values) => {
const documentTypes = [values.applicant.issue, "PROTOKOL", "ZAWIADOMIENIE", "ZARZADZENIE"];
for (const documentType of documentTypes ) {
const documentData = new DocumentData(values).templateData
generateDocument(documentType, documentData)
.generateNodeStream({ type: "nodebuffer", streamFiles: true })
.pipe(fs.createWriteStream(
`${values.child.firstName} ${values.child.lastName} - ${documentType} - ${values.date}.docx`
));
}
});