Skip to content

Commit ea97d07

Browse files
authored
Merge pull request #29 from Chronos2-0/jenae/comment_clean_up
Commented DeleteService Component
2 parents dac3530 + 8919d28 commit ea97d07

File tree

6 files changed

+2601
-236
lines changed

6 files changed

+2601
-236
lines changed

Main.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ app.on('activate', () => {
8585
// Fired by the useEffect hook inside of the Splash.jsx component, this message route will toggle
8686
// splash property inside of settings.json to false once the Splash page renders itself just once
8787
ipcMain.on('toggleSplash', (message) => {
88-
console.log('toggleSplash message received');
88+
//console.log('toggleSplash message received');
8989
const state = JSON.parse(
9090
// read json from settings.json
9191
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
@@ -102,7 +102,7 @@ ipcMain.on('toggleSplash', (message) => {
102102
});
103103

104104
ipcMain.on('checkSplash', (message) => {
105-
console.log('checkSplash message received');
105+
//sconsole.log('checkSplash message received');
106106
const state = JSON.parse(
107107
// read json from settings.json
108108
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
@@ -116,7 +116,7 @@ ipcMain.on('checkSplash', (message) => {
116116
// Load settings JSON and returns current setup status back to the render process.
117117
// ipc 'setup' route --> Ousman
118118
ipcMain.on('setup', (message) => {
119-
console.log('setup message received');
119+
//console.log('setup message received');
120120
// assigns state to the returned the object returned from settings.json --> Ousman
121121
const state = JSON.parse(
122122
// read json from settings.json
@@ -133,30 +133,31 @@ ipcMain.on('setup', (message) => {
133133
// Loads existing settings JSON and update settings to include new services entered by the user.
134134
// on ipc 'submit' request --> Ousman
135135
ipcMain.on('submit', (message, newService) => {
136-
// 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
137137
const state = JSON.parse(
138138
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
139139
encoding: 'UTF-8',
140140
}),
141141
);
142-
// 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.
143142

144-
//* ** What is happening here --> Ousman */
143+
// Checks if setup is required by checking if the value for the state key 'setupRequired' is true
145144
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
146146
state.setupRequired = false;
147147
state.services = [JSON.parse(newService)];
148-
fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state));
149148
} else {
150-
state.setupRequired = false;
149+
// Else the newService is pushed into the services array
151150
state.services.push(JSON.parse(newService));
152-
fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state));
153-
}
151+
}
152+
153+
// Rewrites user/settings.json to show state
154+
fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state));
154155
});
155156

156157
// Load settings JSON and returns updated state back to the render process.
157158
// on ipc 'dashboard' request --> Ousman
158159
ipcMain.on('dashboard', (message) => {
159-
// 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
160161
const state = JSON.parse(
161162
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
162163
encoding: 'UTF-8',
@@ -171,24 +172,29 @@ ipcMain.on('dashboard', (message) => {
171172
message.returnValue = dashboardList;
172173
});
173174

174-
// ipc 'deleteService' route
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
175178
ipcMain.on('deleteService', (message, index) => {
176-
// assigns state to the returned the object returned from settings.json
179+
// Declares a variable state and initialize it to the returned parse json object from the user/settings.json file
177180
let state = JSON.parse(
178-
// read json from settings.json
179181
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
180182
encoding: 'UTF-8',
181183
}),
182184
);
185+
186+
// Send a response back with the updated services
183187
const { splash } = state;
184-
// updating the services
188+
// Checks if there is more than one services in the services array
185189
if (state.services.length > 1) {
190+
// If true, removes the service at position 'index'
186191
state.services.splice(index, 1);
187192
} else {
193+
// Else reassign state to what the user/setting.json file was originally before any database was save
188194
state = { setupRequired: true, services: ['hard', 'coded', 'in'], splash };
189195
}
190196

191-
// write json from settings.json
197+
// Rewrites json from settings.json
192198
fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state), { encoding: 'UTF-8' });
193199
message.sender.send('deleteResponse', state.services);
194200
});

app/components/DeleteService.jsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,48 @@ import React, { useContext } from 'react';
22
import DashboardContext from '../context/DashboardContext';
33
import SetupContext from '../context/SetupContext';
44

5-
5+
// Declare a constant ipcRenderer by deconstructing window.require('electron') so that the onDelete function can initialize an IPC Communication
66
const { ipcRenderer } = window.require('electron');
77

88

9+
// Deletes a Service
910
const DeleteService = (props) => {
10-
console.log('in the DeleteService')
11-
12-
// Used to toggle setup required if user wants delete all of the databases and the user needs to be sent to the front page.
11+
// Declares a constant setup and initialize it to the SetupContext.
12+
// SetupContext indicates whether or not an initial setup is necessary.
13+
// An initial setup is necessary when the user has not saved any database to chronos frontend application.
1314
const setup = useContext(SetupContext);
1415

15-
// List of the databases saved by users to track microservices.
16+
// Declares a variable serviceList and initialize it to the DashboardContext.
17+
// DashboardContext lists the databases saved by users.
1618
let serviceList = useContext(DashboardContext);
1719

18-
// Only happens when a DB button is clicked
20+
/*
21+
Sends a deleteService request with an index to an ipcMain.on function within the Main.js
22+
On return, the function reassigns the serviceList variable to the updated services provided within the deleteResponse. If the serviceList equals ["hard","coded","in"], then the user has no database saved to the chronos frontend application. If this is true, the function reassigns setup.setupRequired to true by invoking setup.toggleSetup with the argument 'false'. Then the function reloads the application to show the changes made to the services.
23+
*/
1924
const onDelete= (index) => {
20-
// IPC communication used to delete the button pressed.
2125
ipcRenderer.send('deleteService', index);
2226
ipcRenderer.on('deleteResponse', (event, services) => {
2327
serviceList = services;
2428
if(serviceList === ["hard","coded","in"]){
25-
setup.setupRequired =setup.toggleSetup(false);
29+
setup.setupRequired = setup.toggleSetup(false);
2630
}
2731
document.location.reload();
28-
2932
});
3033
}
3134

35+
// Declares a constant databaseButtons and initialize it an empty array
3236
const databaseButtons =[];
37+
38+
/* Iterates over the serviceList to create a button for each service. Each button is pushed into the databaseButtons array as the button is created. Each button has an onclick function that invokes the window confirm function with a warning message (ex:'Are you sure you want to delete this service?') and stores the result of invoking confirm into a constant moveForward. If the moveForward is true, then onDelete function is invoked with the index of where the service is stored within the serviceList*/
3339
for(let i = 0; i<serviceList.length; i++){
3440
databaseButtons.push(<button className="microserviceBtn deleteMicroservice" key ={"delete"+i} onClick={()=>{
35-
const moveForward = confirm(`Are you sure you want to delete ${serviceList[i]}? \n If "YES" press the "OK" button, \n else press the "Cancel" button`);
36-
// if yes run the function
41+
const moveForward = confirm(`Are you sure you want to delete ${serviceList[i]}? \n If "YES" press the "OK" button, else press the "Cancel" button`);
3742
if (moveForward){onDelete(i)}
3843

3944
}}>{serviceList[i]}</button>)
4045
}
41-
46+
// returns the title of the page with all of the services that can be deleted as buttons
4247
return (
4348
<div className="mainContainer">
4449
<h1 className='overviewTitle'>Press on the Database You Want to Delete</h1>
@@ -47,4 +52,6 @@ const DeleteService = (props) => {
4752
);
4853
};
4954

55+
// export the DeleteService function so that it can imported anywhere
5056
export default DeleteService;
57+

app/index.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,5 @@ form {
414414
}
415415
.deleteMicroservice{
416416
padding: 10px;
417+
margin: 10px;
417418
}

0 commit comments

Comments
 (0)