Skip to content

Commit ed9175e

Browse files
committed
comment delete service
1 parent b4a6ff5 commit ed9175e

File tree

5 files changed

+2169
-234
lines changed

5 files changed

+2169
-234
lines changed

Main.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,33 @@ ipcMain.on('setup', (message) => {
7878
message.returnValue = setupRequired;
7979
});
8080

81-
// Loads existing settings JSON and update settings to include new services entered by the user.
82-
//on ipc 'submit' request --> Ousman
81+
// Adds a new service to the services array within the user/setting.json file
8382
ipcMain.on('submit', (message, newService) => {
84-
//assigning state to the parsed return of setting
83+
// Declares a variable state and initialize it to the returned parsed json object from the user/settings.json file
8584
const state = JSON.parse(
8685
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
8786
encoding: 'UTF-8',
8887
}),
8988
);
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.
9189

92-
//*** What is happening here --> Ousman */
90+
// Checks if setup is required by checking if the value for the state key 'setupRequired' is true
9391
if (state.setupRequired) {
92+
// 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
9493
state.setupRequired = false;
9594
state.services = [JSON.parse(newService)];
96-
fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state));
9795
} else {
98-
state.setupRequired = false;
96+
// Else the newService is pushed into the services array
9997
state.services.push(JSON.parse(newService));
100-
fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state));
101-
}
98+
}
99+
100+
// Rewrites user/settings.json to show state
101+
fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state));
102102
});
103103

104104
// Load settings JSON and returns updated state back to the render process.
105105
//on ipc 'dashboard' request --> Ousman
106106
ipcMain.on('dashboard', (message) => {
107-
//assign state to the parsed return of setting --> Ousman
107+
// Declares a variable state and initialize it to the returned parse json object from the user/settings.json file
108108
const state = JSON.parse(
109109
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
110110
encoding: 'UTF-8',
@@ -119,27 +119,33 @@ ipcMain.on('dashboard', (message) => {
119119
message.returnValue = dashboardList;
120120
});
121121

122-
//ipc 'deleteService' route
122+
// Deletes the service at position 'index' from the services array within the user/setting.json file,
123+
// resets the user/setting.json file to what it was originally if all of the services are deleted,
124+
// and sends the remaining services back to onDelete function within DeleteService as a response
123125
ipcMain.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-
}
126+
// Declares a variable state and initialize it to the returned parse json object from the user/settings.json file
127+
let state = JSON.parse(
128+
fs.readFileSync(path.resolve(__dirname, './user/settings.json'), {
129+
encoding: 'UTF-8',
130+
}),
131+
);
132+
133+
// Checks if there is more than one services in the services array
134+
if(state.services.length > 1){
135+
// If true, removes the service at position 'index'
136+
state.services.splice(index,1);
137+
}else{
138+
// Else reassign state to what the user/setting.json file was originally before any database was save
139+
state= {"setupRequired":true, "services":["hard","coded","in"] }
140+
}
137141

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-
});
142+
// Rewrites user/settings.json to show state
143+
fs.writeFileSync(path.resolve(__dirname, './user/settings.json'), JSON.stringify(state), {
144+
encoding: 'UTF-8'})
145+
146+
// Send a response back with the updated services
147+
message.sender.send('deleteResponse', state.services);
148+
});
143149

144150

145151
// Queries the database for communications information and returns it back to the render process.

__tests__/enzyme.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
import { configure, shallow } from 'enzyme';
3+
import Adapter from 'enzyme-adapter-react-16';
4+
const { JSDOM } = require('jsdom');
5+
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
6+
const { window } = jsdom;
7+
8+
import DeleteService from '../app/components/DeleteService.jsx'
9+
10+
11+
configure({ adapter: new Adapter() });
12+
13+
describe('Delete a Service', () => {
14+
beforeAll(() => {
15+
let wrapper = shallow(<DeleteService/>);
16+
});
17+
18+
xit('returns a div',()=>{
19+
expect(wrapper.type()).toEqual('div')
20+
})
21+
it('Delete a service with more than one services within user/setting.json',()=>{
22+
expect(1).toEqual(1)
23+
})
24+
describe('Delete a service with only one services within user/setting.json', () => {
25+
it('Delete the service',()=>{
26+
expect(1).toEqual(1)
27+
})
28+
it('Render the AddService Component',()=>{
29+
expect(1).toEqual(1)
30+
})
31+
})
32+
})

app/components/DeleteService.jsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,47 @@ 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

8-
8+
// Deletes a Service
99
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.
10+
// Declares a constant setup and initialize it to the SetupContext.
11+
// SetupContext indicates whether or not an initial setup is necessary.
12+
// An initial setup is necessary when the user has not saved any database to chronos frontend application.
1313
const setup = useContext(SetupContext);
1414

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

18-
// Only happens when a DB button is clicked
19+
/*
20+
Sends a deleteService request with an index to an ipcMain.on function within the Main.js
21+
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.
22+
*/
1923
const onDelete= (index) => {
20-
// IPC communication used to delete the button pressed.
2124
ipcRenderer.send('deleteService', index);
2225
ipcRenderer.on('deleteResponse', (event, services) => {
2326
serviceList = services;
2427
if(serviceList === ["hard","coded","in"]){
25-
setup.setupRequired =setup.toggleSetup(false);
28+
setup.setupRequired = setup.toggleSetup(false);
2629
}
2730
document.location.reload();
28-
2931
});
3032
}
3133

34+
// Declares a constant databaseButtons and initialize it an empty array
3235
const databaseButtons =[];
36+
37+
/* 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*/
3338
for(let i = 0; i<serviceList.length; i++){
3439
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
40+
const moveForward = confirm(`Are you sure you want to delete ${serviceList[i]}? \n If "YES" press the "OK" button, else press the "Cancel" button`);
3741
if (moveForward){onDelete(i)}
3842

3943
}}>{serviceList[i]}</button>)
4044
}
41-
45+
// returns the title of the page with all of the services that can be deleted as buttons
4246
return (
4347
<div className="mainContainer">
4448
<h1 className='overviewTitle'>Press on the Database You Want to Delete</h1>
@@ -47,4 +51,5 @@ const DeleteService = (props) => {
4751
);
4852
};
4953

54+
// export the DeleteService function so that it can imported anywhere
5055
export default DeleteService;

0 commit comments

Comments
 (0)