1- //node requirements
2- const { dialog, app, BrowserWindow, ipcMain } = require ( 'electron' ) ;
1+ // node requirements
2+ const {
3+ dialog, app, BrowserWindow, ipcMain,
4+ } = require ( 'electron' ) ;
35const fs = require ( 'fs' ) ;
46const path = require ( 'path' ) ;
57const connectSQL = require ( './model/sql-connect' ) ;
68const connectMongoose = require ( './model/mongoose-connect' ) ;
79const CommunicationSchema = require ( './model/mongoose-communicatonSchema' ) ;
810const HealthInfoSchema = require ( './model/mongoose-healthInfoSchema' ) ;
911
10- //declare a variable pool for SQL connection
12+ // declare a variable pool for SQL connection
1113let pool ;
1214
13- //declare win variable ---> Ousman
15+ // declare win variable ---> Ousman
1416let win ;
1517
16- //declaring a createWindow function ---> Ousman
18+ // declaring a createWindow function ---> Ousman
1719function createWindow ( ) {
18- //assign win to an instance of a new browser window.
20+ // assign win to an instance of a new browser window.
1921 win = new BrowserWindow ( {
20- //giving our window its width
22+ // giving our window its width
2123 width : 900 ,
22- //giving our window its hieght
24+ // giving our window its hieght
2325 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
2527 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
2729 webPreferences : {
2830 nodeIntegration : true ,
2931 } ,
3032 } ) ;
3133
3234 // 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
3436 win . loadURL ( 'http://localhost:8080/' ) ;
3537
3638 // Production
3739 // 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
4042 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 ;
4252 } ) ;
4353}
4454
45- //invoke createWindow function on Electron application load --> Ousman
55+ // invoke createWindow function on Electron application load --> Ousman
4656app . on ( 'ready' , createWindow ) ;
4757
4858// quits the application when all windows are closed --> Ousman
4959app . 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
5171 if ( process . platform !== 'darwin' ) {
52- //quits application
72+ // quits application
5373 app . quit ( ) ;
5474 }
5575} ) ;
5676
57- //event 'activate' emmitted upon application starting
77+ // event 'activate' emmitted upon application starting
5878app . 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
6080 if ( win === null ) {
6181 createWindow ( ) ;
6282 }
6383} ) ;
6484
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+ console . 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+
65116// Load settings JSON and returns current setup status back to the render process.
66- //ipc 'setup' route --> Ousman
117+ // ipc 'setup' route --> Ousman
67118ipcMain . 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
69121 const state = JSON . parse (
70- //read json from settings.json
122+ // read json from settings.json
71123 fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
72124 encoding : 'UTF-8' ,
73125 } ) ,
74126 ) ;
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
78130 message . returnValue = setupRequired ;
79131} ) ;
80132
81133// 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
83135ipcMain . on ( 'submit' , ( message , newService ) => {
84- //assigning state to the parsed return of setting
136+ // assigning state to the parsed return of setting
85137 const state = JSON . parse (
86138 fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
87139 encoding : 'UTF-8' ,
88140 } ) ,
89141 ) ;
90142 // 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.
91143
92- //*** What is happening here --> Ousman */
144+ //* ** What is happening here --> Ousman */
93145 if ( state . setupRequired ) {
94146 state . setupRequired = false ;
95147 state . services = [ JSON . parse ( newService ) ] ;
@@ -102,15 +154,15 @@ ipcMain.on('submit', (message, newService) => {
102154} ) ;
103155
104156// Load settings JSON and returns updated state back to the render process.
105- //on ipc 'dashboard' request --> Ousman
157+ // on ipc 'dashboard' request --> Ousman
106158ipcMain . on ( 'dashboard' , ( message ) => {
107- //assign state to the parsed return of setting --> Ousman
159+ // assign state to the parsed return of setting --> Ousman
108160 const state = JSON . parse (
109161 fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
110162 encoding : 'UTF-8' ,
111163 } ) ,
112164 ) ;
113- //destructure services from state... what is services? --> Ousman
165+ // destructure services from state... what is services? --> Ousman
114166 const { services } = state ;
115167 const dashboardList = services . reduce ( ( acc , curVal ) => {
116168 acc . push ( curVal [ 0 ] ) ;
@@ -119,35 +171,35 @@ ipcMain.on('dashboard', (message) => {
119171 message . returnValue = dashboardList ;
120172} ) ;
121173
122- //ipc 'deleteService' route
174+ // ipc 'deleteService' route
123175ipcMain . on ( 'deleteService' , ( message , index ) => {
124- //assigns state to the returned the object returned from settings.json
125- let state = JSON . parse (
126- // read json from settings.json
127- fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
128- encoding : 'UTF-8' ,
129- } ) ,
130- ) ;
131- // updating the services
132- if ( state . services . length > 1 ) {
133- state . services . splice ( index , 1 ) ;
134- } else {
135- state = { "setupRequired" : true , "services" : [ "hard" , "coded" , "in" ] }
136- }
137-
138- //write json from settings.json
139- fs . writeFileSync ( path . resolve ( __dirname , './user/ settings.json' ) , JSON . stringify ( state ) , {
140- encoding : 'UTF-8' } )
141- message . sender . send ( 'deleteResponse' , state . services ) ;
142- } ) ;
176+ // assigns state to the returned the object returned from settings.json
177+ let state = JSON . parse (
178+ // read json from settings.json
179+ fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
180+ encoding : 'UTF-8' ,
181+ } ) ,
182+ ) ;
183+ const { splash } = state ;
184+ // updating the services
185+ if ( state . services . length > 1 ) {
186+ state . services . splice ( index , 1 ) ;
187+ } else {
188+ state = { setupRequired : true , services : [ 'hard' , 'coded' , 'in' ] , splash } ;
189+ }
190+
191+ // write json from settings.json
192+ fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) , { encoding : 'UTF-8' } ) ;
193+ message . sender . send ( 'deleteResponse' , state . services ) ;
194+ } ) ;
143195
144196
145197// Queries the database for communications information and returns it back to the render process.
146198ipcMain . on ( 'overviewRequest' , ( message , index ) => {
147- const services = JSON . parse (
199+ const { services } = JSON . parse (
148200 fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , { encoding : 'UTF-8' } ) ,
149- ) . services ;
150-
201+ ) ;
202+
151203 const databaseType = services [ index ] [ 1 ] ;
152204 const URI = services [ index ] [ 2 ] ;
153205
@@ -157,36 +209,33 @@ ipcMain.on('overviewRequest', (message, index) => {
157209 if ( err ) {
158210 console . log ( `An error occured while querying the database: ${ err } ` ) ;
159211 message . sender . send ( 'overviewResponse' , JSON . stringify ( err ) ) ;
160- }
161-
162- const queryResults = JSON . stringify ( data ) ;
163- // Asynchronous event emitter used to transmit query results back to the render process.
164- message . sender . send ( 'overviewResponse' , queryResults ) ;
165-
212+ }
213+
214+ const queryResults = JSON . stringify ( data ) ;
215+ // Asynchronous event emitter used to transmit query results back to the render process.
216+ message . sender . send ( 'overviewResponse' , queryResults ) ;
166217 } ) ;
167218 }
168-
219+
169220 if ( databaseType === 'SQL' ) {
170221 pool = connectSQL ( index , URI ) ;
171222 const getCommunications = 'SELECT * FROM communications' ;
172223 pool . query ( getCommunications , ( err , result ) => {
173224 if ( err ) {
225+ // error object to log to Electron GUI ---> Ousman
226+ const errorAlert = {
227+ type : 'error' ,
228+ title : 'Error in Main process' ,
229+ message : 'Database information could not be retreived. Check that table exists.' ,
230+ } ;
174231
175- //error object to log to Electron GUI ---> Ousman
176- const errorAlert = {
177- type : "error" ,
178- title : "Error in Main process" ,
179- message : "Database information could not be retreived. Check that table exists."
180- } ;
181-
182- //after requiring dialog in the topmost section of main. We invoke the method showMessagebox passing the error object we created --> Ousman
183- dialog . showMessageBox ( errorAlert ) ;
184-
232+ // after requiring dialog in the topmost section of main. We invoke the method showMessagebox passing the error object we created --> Ousman
233+ dialog . showMessageBox ( errorAlert ) ;
185234
186- message . sender . send ( JSON . stringify ( 'Database info could not be retreived.' ) ) ;
187235
236+ message . sender . send ( JSON . stringify ( 'Database info could not be retreived.' ) ) ;
188237 } else {
189- console . log ( 'Connected to SQL Database' )
238+ console . log ( 'Connected to SQL Database' ) ;
190239 const queryResults = JSON . stringify ( result . rows ) ;
191240 // Asynchronous event emitter used to transmit query results back to the render process.
192241 message . sender . send ( 'overviewResponse' , queryResults ) ;
0 commit comments