1
- //node requirements
2
- const { dialog, app, BrowserWindow, ipcMain } = require ( 'electron' ) ;
1
+ // node requirements
2
+ const {
3
+ dialog, app, BrowserWindow, ipcMain,
4
+ } = require ( 'electron' ) ;
3
5
const fs = require ( 'fs' ) ;
4
6
const path = require ( 'path' ) ;
5
7
const connectSQL = require ( './model/sql-connect' ) ;
6
8
const connectMongoose = require ( './model/mongoose-connect' ) ;
7
9
const CommunicationSchema = require ( './model/mongoose-communicatonSchema' ) ;
8
10
const HealthInfoSchema = require ( './model/mongoose-healthInfoSchema' ) ;
9
11
10
- //declare a variable pool for SQL connection
12
+ // declare a variable pool for SQL connection
11
13
let pool ;
12
14
13
- //declare win variable ---> Ousman
15
+ // declare win variable ---> Ousman
14
16
let win ;
15
17
16
- //declaring a createWindow function ---> Ousman
18
+ // declaring a createWindow function ---> Ousman
17
19
function createWindow ( ) {
18
- //assign win to an instance of a new browser window.
20
+ // assign win to an instance of a new browser window.
19
21
win = new BrowserWindow ( {
20
- //giving our window its width
22
+ // giving our window its width
21
23
width : 900 ,
22
- //giving our window its hieght
24
+ // giving our window its hieght
23
25
height : 800 ,
24
- //specify the path of the icon -- Which icon is this?.. note too tsure --> Ousman
26
+ // specify the path of the icon -- Which icon is this?.. note too tsure --> Ousman
25
27
icon : path . join ( __dirname , 'app/assets/icons/icon.png' ) ,
26
- //enable node inegreation --> node intgeration, default is usally false --> Ousman
28
+ // enable node inegreation --> node intgeration, default is usally false --> Ousman
27
29
webPreferences : {
28
30
nodeIntegration : true ,
29
31
} ,
30
32
} ) ;
31
33
32
34
// Development
33
- //loads our application window to localHost 8080, application will not render without this loadUrl --> Ousman
35
+ // loads our application window to localHost 8080, application will not render without this loadUrl --> Ousman
34
36
win . loadURL ( 'http://localhost:8080/' ) ;
35
37
36
38
// Production
37
39
// win.loadURL(`file://${path.join(__dirname, './dist/index.html')}`);
38
-
39
- //assign window to null on close
40
+
41
+ // assign window to null on close and set splash property in settings.json back to true so splash page renders on restart
40
42
win . on ( 'closed' , ( ) => {
41
- win = null ;
43
+ const state = JSON . parse (
44
+ // read json from settings.json
45
+ fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
46
+ encoding : 'UTF-8' ,
47
+ } ) ,
48
+ ) ;
49
+ // reassign state.splash
50
+ state . splash = true ;
51
+ fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) , { encoding : 'UTF-8' } ) ; win = null ;
42
52
} ) ;
43
53
}
44
54
45
- //invoke createWindow function on Electron application load --> Ousman
55
+ // invoke createWindow function on Electron application load --> Ousman
46
56
app . on ( 'ready' , createWindow ) ;
47
57
48
58
// quits the application when all windows are closed --> Ousman
49
59
app . on ( 'window-all-closed' , ( ) => {
50
- //process platform is a property that return a string identifying the OS platform on which NodeJs process is running --> Ousman
60
+ console . log ( 'window-all-closed message received' ) ;
61
+ const state = JSON . parse (
62
+ // read json from settings.json
63
+ fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
64
+ encoding : 'UTF-8' ,
65
+ } ) ,
66
+ ) ;
67
+ // reassign state.splash
68
+ state . splash = true ;
69
+ fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) , { encoding : 'UTF-8' } ) ;
70
+ // process platform is a property that return a string identifying the OS platform on which NodeJs process is running --> Ousman
51
71
if ( process . platform !== 'darwin' ) {
52
- //quits application
72
+ // quits application
53
73
app . quit ( ) ;
54
74
}
55
75
} ) ;
56
76
57
- //event 'activate' emmitted upon application starting
77
+ // event 'activate' emmitted upon application starting
58
78
app . on ( 'activate' , ( ) => {
59
- //if there is no window present invoke the create window function --> Ousman
79
+ // if there is no window present invoke the create window function --> Ousman
60
80
if ( win === null ) {
61
81
createWindow ( ) ;
62
82
}
63
83
} ) ;
64
84
85
+ // Fired by the useEffect hook inside of the Splash.jsx component, this message route will toggle
86
+ // splash property inside of settings.json to false once the Splash page renders itself just once
87
+ ipcMain . on ( 'toggleSplash' , ( message ) => {
88
+ //console.log('toggleSplash message received');
89
+ const state = JSON . parse (
90
+ // read json from settings.json
91
+ fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
92
+ encoding : 'UTF-8' ,
93
+ } ) ,
94
+ ) ;
95
+ // reassign state.splash to false
96
+ state . splash = false ;
97
+
98
+ // overwrite settings.json with false splash property
99
+ fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) , { encoding : 'UTF-8' } ) ;
100
+
101
+ message . returnValue = state . splash ;
102
+ } ) ;
103
+
104
+ ipcMain . on ( 'checkSplash' , ( message ) => {
105
+ //sconsole.log('checkSplash message received');
106
+ const state = JSON . parse (
107
+ // read json from settings.json
108
+ fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
109
+ encoding : 'UTF-8' ,
110
+ } ) ,
111
+ ) ;
112
+
113
+ message . returnValue = state . splash ;
114
+ } ) ;
115
+
65
116
// Load settings JSON and returns current setup status back to the render process.
66
- //ipc 'setup' route --> Ousman
117
+ // ipc 'setup' route --> Ousman
67
118
ipcMain . on ( 'setup' , ( message ) => {
68
- //assigns state to the returned the object returned from settings.json --> Ousman
119
+ //console.log('setup message received');
120
+ // assigns state to the returned the object returned from settings.json --> Ousman
69
121
const state = JSON . parse (
70
- //read json from settings.json
122
+ // read json from settings.json
71
123
fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
72
124
encoding : 'UTF-8' ,
73
125
} ) ,
74
126
) ;
75
- //destructure setupRequired from state constant ---> Ousman
76
- const { setupRequired } = state ;
77
- //assigning message object a property of return value and assigning it the setupRequired from state destructuring --> Ousman
127
+ // destructure setupRequired from state constant ---> Ousman
128
+ const { setupRequired } = state ;
129
+ // assigning message object a property of return value and assigning it the setupRequired from state destructuring --> Ousman
78
130
message . returnValue = setupRequired ;
79
131
} ) ;
80
132
81
133
// Loads existing settings JSON and update settings to include new services entered by the user.
82
- //on ipc 'submit' request --> Ousman
134
+ // on ipc 'submit' request --> Ousman
83
135
ipcMain . on ( 'submit' , ( message , newService ) => {
84
- //assigning state to the parsed return of setting
136
+ // Declares a variable state and initialize it to the returned parsed json object from the user/settings.json file
85
137
const state = JSON . parse (
86
138
fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
87
139
encoding : 'UTF-8' ,
88
140
} ) ,
89
141
) ;
90
- // if statement is used to replace hard coded data. Hard coded data and the michelleWasHere key is needed to avoid a load error caused by Electron querying the database before a user has added or selected a database.
91
142
92
- //*** What is happening here --> Ousman */
143
+ // Checks if setup is required by checking if the value for the state key 'setupRequired' is true
93
144
if ( state . setupRequired ) {
145
+ // If setup is required, the value for key 'setupRequired' is reassign to false and the value for key 'services' is reassign to an array with newService as its only element
94
146
state . setupRequired = false ;
95
147
state . services = [ JSON . parse ( newService ) ] ;
96
- fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) ) ;
97
148
} else {
98
- state . setupRequired = false ;
149
+ // Else the newService is pushed into the services array
99
150
state . services . push ( JSON . parse ( newService ) ) ;
100
- fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) ) ;
101
- }
151
+ }
152
+
153
+ // Rewrites user/settings.json to show state
154
+ fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) ) ;
102
155
} ) ;
103
156
104
157
// Load settings JSON and returns updated state back to the render process.
105
- //on ipc 'dashboard' request --> Ousman
158
+ // on ipc 'dashboard' request --> Ousman
106
159
ipcMain . on ( 'dashboard' , ( message ) => {
107
- //assign state to the parsed return of setting --> Ousman
160
+ // Declares a variable state and initialize it to the returned parse json object from the user/settings.json file
108
161
const state = JSON . parse (
109
162
fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
110
163
encoding : 'UTF-8' ,
111
164
} ) ,
112
165
) ;
113
- //destructure services from state... what is services? --> Ousman
166
+ // destructure services from state... what is services? --> Ousman
114
167
const { services } = state ;
115
168
const dashboardList = services . reduce ( ( acc , curVal ) => {
116
169
acc . push ( curVal [ 0 ] ) ;
@@ -119,12 +172,41 @@ ipcMain.on('dashboard', (message) => {
119
172
message . returnValue = dashboardList ;
120
173
} ) ;
121
174
175
+ // Deletes the service at position 'index' from the services array within the user/setting.json file,
176
+ // resets the user/setting.json file to what it was originally if all of the services are deleted,
177
+ // and sends the remaining services back to onDelete function within DeleteService as a response
178
+ ipcMain . on ( 'deleteService' , ( message , index ) => {
179
+ // Declares a variable state and initialize it to the returned parse json object from the user/settings.json file
180
+ let state = JSON . parse (
181
+ fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
182
+ encoding : 'UTF-8' ,
183
+ } ) ,
184
+ ) ;
185
+
186
+ // Send a response back with the updated services
187
+ const { splash } = state ;
188
+ // Checks if there is more than one services in the services array
189
+ if ( state . services . length > 1 ) {
190
+ // If true, removes the service at position 'index'
191
+ state . services . splice ( index , 1 ) ;
192
+ } else {
193
+ // Else reassign state to what the user/setting.json file was originally before any database was save
194
+ state = { setupRequired : true , services : [ 'hard' , 'coded' , 'in' ] , splash } ;
195
+ }
196
+
197
+ // Rewrites json from settings.json
198
+ fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) , { encoding : 'UTF-8' } ) ;
199
+ message . sender . send ( 'deleteResponse' , state . services ) ;
200
+ } ) ;
201
+
202
+
122
203
// Queries the database for communications information and returns it back to the render process.
123
204
ipcMain . on ( 'overviewRequest' , ( message , index ) => {
124
- const services = JSON . parse (
205
+ console . log ( 'hello from overview request' ) ;
206
+ const { services } = JSON . parse (
125
207
fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , { encoding : 'UTF-8' } ) ,
126
- ) . services ;
127
-
208
+ ) ;
209
+
128
210
const databaseType = services [ index ] [ 1 ] ;
129
211
const URI = services [ index ] [ 2 ] ;
130
212
@@ -134,38 +216,36 @@ ipcMain.on('overviewRequest', (message, index) => {
134
216
if ( err ) {
135
217
console . log ( `An error occured while querying the database: ${ err } ` ) ;
136
218
message . sender . send ( 'overviewResponse' , JSON . stringify ( err ) ) ;
137
- }
138
-
139
- const queryResults = JSON . stringify ( data ) ;
140
- // Asynchronous event emitter used to transmit query results back to the render process.
141
- message . sender . send ( 'overviewResponse' , queryResults ) ;
142
-
219
+ }
220
+
221
+ const queryResults = JSON . stringify ( data ) ;
222
+ // Asynchronous event emitter used to transmit query results back to the render process.
223
+ message . sender . send ( 'overviewResponse' , queryResults ) ;
143
224
} ) ;
144
225
}
145
-
226
+
146
227
if ( databaseType === 'SQL' ) {
147
228
pool = connectSQL ( index , URI ) ;
148
229
const getCommunications = 'SELECT * FROM communications' ;
149
230
pool . query ( getCommunications , ( err , result ) => {
150
231
if ( err ) {
232
+ // error object to log to Electron GUI ---> Ousman
233
+ const errorAlert = {
234
+ type : 'error' ,
235
+ title : 'Error in Main process' ,
236
+ message : 'Database information could not be retreived. Check that table exists.' ,
237
+ } ;
151
238
152
- //error object to log to Electron GUI ---> Ousman
153
- const errorAlert = {
154
- type : "error" ,
155
- title : "Error in Main process" ,
156
- message : "Database information could not be retreived. Check that table exists."
157
- } ;
158
-
159
- //after requiring dialog in the topmost section of main. We invoke the method showMessagebox passing the error object we created --> Ousman
160
- dialog . showMessageBox ( errorAlert ) ;
161
-
239
+ // after requiring dialog in the topmost section of main. We invoke the method showMessagebox passing the error object we created --> Ousman
240
+ dialog . showMessageBox ( errorAlert ) ;
162
241
163
- message . sender . send ( JSON . stringify ( 'Database info could not be retreived.' ) ) ;
164
242
243
+ message . sender . send ( JSON . stringify ( 'Database info could not be retreived.' ) ) ;
165
244
} else {
166
- console . log ( 'Connected to SQL Database' )
245
+ console . log ( 'Connected to SQL Database' ) ;
167
246
const queryResults = JSON . stringify ( result . rows ) ;
168
247
// Asynchronous event emitter used to transmit query results back to the render process.
248
+ console . log ( 'ipcMain about to send overviewResponse message' ) ;
169
249
message . sender . send ( 'overviewResponse' , queryResults ) ;
170
250
}
171
251
} ) ;
@@ -174,6 +254,7 @@ ipcMain.on('overviewRequest', (message, index) => {
174
254
175
255
// Queries the database for computer health information and returns it back to the render process.
176
256
ipcMain . on ( 'detailsRequest' , ( message , index ) => {
257
+ console . log ( 'detailsRequest message received' ) ;
177
258
const databaseType = JSON . parse (
178
259
fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , { encoding : 'UTF-8' } ) ,
179
260
) . services [ index ] [ 1 ] ;
@@ -197,6 +278,7 @@ ipcMain.on('detailsRequest', (message, index) => {
197
278
}
198
279
const queryResults = JSON . stringify ( result . rows ) ;
199
280
// Asynchronous event emitter used to transmit query results back to the render process.
281
+ // console.log('healthInfo data about to comeback');
200
282
message . sender . send ( 'detailsResponse' , queryResults ) ;
201
283
} ) ;
202
284
}
0 commit comments