1- const { app, BrowserWindow, ipcMain } = require ( 'electron' ) ;
1+ // node requirements
2+ const {
3+ dialog, app, BrowserWindow, ipcMain,
4+ } = require ( 'electron' ) ;
25const fs = require ( 'fs' ) ;
36const path = require ( 'path' ) ;
47const connectSQL = require ( './model/sql-connect' ) ;
58const connectMongoose = require ( './model/mongoose-connect' ) ;
69const CommunicationSchema = require ( './model/mongoose-communicatonSchema' ) ;
710const HealthInfoSchema = require ( './model/mongoose-healthInfoSchema' ) ;
811
12+ // declare a variable pool for SQL connection
13+ let pool ;
14+
15+ // declare win variable ---> Ousman
916let win ;
17+
18+ // declaring a createWindow function ---> Ousman
1019function createWindow ( ) {
20+ // assign win to an instance of a new browser window.
1121 win = new BrowserWindow ( {
22+ // giving our window its width
1223 width : 900 ,
24+ // giving our window its hieght
1325 height : 800 ,
26+ // specify the path of the icon -- Which icon is this?.. note too tsure --> Ousman
1427 icon : path . join ( __dirname , 'app/assets/icons/icon.png' ) ,
28+ // enable node inegreation --> node intgeration, default is usally false --> Ousman
1529 webPreferences : {
1630 nodeIntegration : true ,
1731 } ,
1832 } ) ;
1933
2034 // Development
35+ // loads our application window to localHost 8080, application will not render without this loadUrl --> Ousman
2136 win . loadURL ( 'http://localhost:8080/' ) ;
2237
2338 // Production
2439 // win.loadURL(`file://${path.join(__dirname, './dist/index.html')}`);
2540
41+ // assign window to null on close and set splash property in settings.json back to true so splash page renders on restart
2642 win . on ( 'closed' , ( ) => {
27- 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 ;
2852 } ) ;
2953}
54+
55+ // invoke createWindow function on Electron application load --> Ousman
3056app . on ( 'ready' , createWindow ) ;
3157
58+ // quits the application when all windows are closed --> Ousman
3259app . on ( 'window-all-closed' , ( ) => {
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
3371 if ( process . platform !== 'darwin' ) {
72+ // quits application
3473 app . quit ( ) ;
3574 }
3675} ) ;
3776
77+ // event 'activate' emmitted upon application starting
3878app . on ( 'activate' , ( ) => {
79+ // if there is no window present invoke the create window function --> Ousman
3980 if ( win === null ) {
4081 createWindow ( ) ;
4182 }
4283} ) ;
4384
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+
44116// Load settings JSON and returns current setup status back to the render process.
117+ // ipc 'setup' route --> Ousman
45118ipcMain . on ( 'setup' , ( message ) => {
119+ //console.log('setup message received');
120+ // assigns state to the returned the object returned from settings.json --> Ousman
46121 const state = JSON . parse (
122+ // read json from settings.json
47123 fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
48124 encoding : 'UTF-8' ,
49125 } ) ,
50126 ) ;
127+ // destructure setupRequired from state constant ---> Ousman
51128 const { setupRequired } = state ;
129+ // assigning message object a property of return value and assigning it the setupRequired from state destructuring --> Ousman
52130 message . returnValue = setupRequired ;
53131} ) ;
54132
55133// Loads existing settings JSON and update settings to include new services entered by the user.
134+ // on ipc 'submit' request --> Ousman
56135ipcMain . on ( 'submit' , ( message , newService ) => {
136+ // Declares a variable state and initialize it to the returned parsed json object from the user/settings.json file
57137 const state = JSON . parse (
58138 fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
59139 encoding : 'UTF-8' ,
60140 } ) ,
61141 ) ;
62- // 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.
63- if ( state . michelleWasHere ) {
142+
143+ // Checks if setup is required by checking if the value for the state key 'setupRequired' is true
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
64146 state . setupRequired = false ;
65- state . michelleWasHere = false ;
66147 state . services = [ JSON . parse ( newService ) ] ;
67- fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) ) ;
68148 } else {
69- state . setupRequired = false ;
149+ // Else the newService is pushed into the services array
70150 state . services . push ( JSON . parse ( newService ) ) ;
71- fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) ) ;
72- }
151+ }
152+
153+ // Rewrites user/settings.json to show state
154+ fs . writeFileSync ( path . resolve ( __dirname , './user/settings.json' ) , JSON . stringify ( state ) ) ;
73155} ) ;
74156
75157// Load settings JSON and returns updated state back to the render process.
158+ // on ipc 'dashboard' request --> Ousman
76159ipcMain . on ( 'dashboard' , ( message ) => {
160+ // Declares a variable state and initialize it to the returned parse json object from the user/settings.json file
77161 const state = JSON . parse (
78162 fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , {
79163 encoding : 'UTF-8' ,
80164 } ) ,
81165 ) ;
166+ // destructure services from state... what is services? --> Ousman
82167 const { services } = state ;
83168 const dashboardList = services . reduce ( ( acc , curVal ) => {
84169 acc . push ( curVal [ 0 ] ) ;
@@ -87,48 +172,94 @@ ipcMain.on('dashboard', (message) => {
87172 message . returnValue = dashboardList ;
88173} ) ;
89174
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+
90203// Queries the database for communications information and returns it back to the render process.
91204ipcMain . on ( 'overviewRequest' , ( message , index ) => {
92- const databaseType = JSON . parse (
205+ console . log ( 'hello from overview request' ) ;
206+ const { services } = JSON . parse (
93207 fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , { encoding : 'UTF-8' } ) ,
94- ) . services [ index ] [ 1 ] ;
208+ ) ;
209+
210+ const databaseType = services [ index ] [ 1 ] ;
211+ const URI = services [ index ] [ 2 ] ;
95212
96213 if ( databaseType === 'MongoDB' ) {
97- connectMongoose ( index ) ;
214+ connectMongoose ( index , URI ) ;
98215 CommunicationSchema . find ( { } , ( err , data ) => {
99216 if ( err ) {
100217 console . log ( `An error occured while querying the database: ${ err } ` ) ;
101218 message . sender . send ( 'overviewResponse' , JSON . stringify ( err ) ) ;
102219 }
220+
103221 const queryResults = JSON . stringify ( data ) ;
104222 // Asynchronous event emitter used to transmit query results back to the render process.
105223 message . sender . send ( 'overviewResponse' , queryResults ) ;
106224 } ) ;
107225 }
108226
109227 if ( databaseType === 'SQL' ) {
110- const pool = connectSQL ( index ) ;
228+ pool = connectSQL ( index , URI ) ;
111229 const getCommunications = 'SELECT * FROM communications' ;
112230 pool . query ( getCommunications , ( err , result ) => {
113231 if ( err ) {
114- console . log ( 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+ } ;
238+
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 ) ;
241+
242+
115243 message . sender . send ( JSON . stringify ( 'Database info could not be retreived.' ) ) ;
244+ } else {
245+ console . log ( 'Connected to SQL Database' ) ;
246+ const queryResults = JSON . stringify ( result . rows ) ;
247+ // Asynchronous event emitter used to transmit query results back to the render process.
248+ console . log ( 'ipcMain about to send overviewResponse message' ) ;
249+ message . sender . send ( 'overviewResponse' , queryResults ) ;
116250 }
117- const queryResults = JSON . stringify ( result . rows ) ;
118- // Asynchronous event emitter used to transmit query results back to the render process.
119- message . sender . send ( 'overviewResponse' , queryResults ) ;
120251 } ) ;
121252 }
122253} ) ;
123254
124255// Queries the database for computer health information and returns it back to the render process.
125256ipcMain . on ( 'detailsRequest' , ( message , index ) => {
257+ console . log ( 'detailsRequest message received' ) ;
126258 const databaseType = JSON . parse (
127259 fs . readFileSync ( path . resolve ( __dirname , './user/settings.json' ) , { encoding : 'UTF-8' } ) ,
128260 ) . services [ index ] [ 1 ] ;
129261
130262 if ( databaseType === 'MongoDB' ) {
131- connectMongoose ( index ) ;
132263 HealthInfoSchema . find ( { } , ( err , data ) => {
133264 if ( err ) {
134265 message . sender . send ( 'detailsResponse' , JSON . stringify ( err ) ) ;
@@ -140,15 +271,16 @@ ipcMain.on('detailsRequest', (message, index) => {
140271 }
141272
142273 if ( databaseType === 'SQL' ) {
143- const pool = connectSQL ( index ) ;
144274 const getHealth = 'SELECT * FROM healthInfo' ;
145275 pool . query ( getHealth , ( err , result ) => {
146276 if ( err ) {
147277 message . sender . send ( 'detailsResponse' , JSON . stringify ( 'Database info could not be retreived.' ) ) ;
148278 }
149279 const queryResults = JSON . stringify ( result . rows ) ;
150280 // Asynchronous event emitter used to transmit query results back to the render process.
281+ // console.log('healthInfo data about to comeback');
151282 message . sender . send ( 'detailsResponse' , queryResults ) ;
152283 } ) ;
153284 }
154285} ) ;
286+
0 commit comments