Skip to content

Commit 6cccea8

Browse files
committed
Delete card functionality refactor
Cards are now deleted without the ref hook I believe the delRef hook was being used to prevent an issue with event bubbling. Becuase the delete button is nested inside of the Card component and card has an onClick the prior teams were calling the onClick on the card as well as the delete button. event.stopPropagation was used to prevent the bubbling the del ref hook is now able to be removed
1 parent e0e538e commit 6cccea8

4 files changed

Lines changed: 19 additions & 24 deletions

File tree

app/components/ApplicationsCard/ApplicationsCard.tsx

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ const ApplicationsCard = (props) => {
1515
const { application, i, setModal, classes } = props
1616
const { deleteApp,user,applications } = useContext(DashboardContext)
1717
const { setAppIndex, setApp, setServicesData, app,example,connectToDB,setChart } = useContext(ApplicationContext)
18-
const [ cardName,dbType,dbURI,description,serviceType ] = applications[i]
18+
const [ cardName,dbType,dbURI,description,serviceType ] = application
1919

2020
//dynamic refs
21-
const delRef = useRef<any>([]);
21+
// const delRef = useRef<any>([]);
2222

2323
const navigate = useNavigate();
2424

@@ -54,19 +54,18 @@ const ApplicationsCard = (props) => {
5454
navigate(`/applications/example/${services.join(' ')}`)
5555
}
5656
else {
57-
setAppIndex(i);
58-
setApp(selectedApp);
5957
setServicesData([]);
6058
//When we open the service modal a connection is made to the db in a useEffect inside of the service modal component
6159
setModal({isOpen:true,type:'serviceModal'})
6260
}
63-
}
61+
// }
6462
};
6563

6664
// Asks user to confirm deletion
67-
const confirmDelete = (event: ClickEvent, application: string, i: number) => {
65+
const confirmDelete = (event: ClickEvent, i: number) => {
66+
event.stopPropagation()
6867
const message = `The application '${app}' will be permanently deleted. Continue?`;
69-
if (confirm(message)) deleteApp(i);
68+
if (confirm(message)) deleteApp(i,"");
7069
};
7170

7271
return (
@@ -75,7 +74,7 @@ const ApplicationsCard = (props) => {
7574
key={`card-${i}`}
7675
className={classes.paper}
7776
variant="outlined"
78-
onClick={event => handleClick(event, application[0], application[3], i)}
77+
onClick={() => handleClick(application[0], application[3], i)}
7978
>
8079
<div className="databaseIconContainer">
8180
<div className="databaseIconHeader">
@@ -86,25 +85,18 @@ const ApplicationsCard = (props) => {
8685
avatar={
8786
<IconButton
8887
id="iconButton"
89-
ref={element => {
90-
delRef.current[i] = element;
91-
}}
9288
className={classes.iconbutton}
9389
aria-label="Delete"
94-
onClick={event => confirmDelete(event, application[0], i)}
90+
onClick={event => confirmDelete(event, i)}
9591
>
9692
<HighlightOffIcon
9793
className={classes.btnStyle}
9894
id="deleteIcon"
99-
ref={element => {
100-
delRef.current[i] = element;
101-
}}
10295
/>
10396
</IconButton>
10497
}
10598
/>
10699
<CardContent>
107-
{/* <p id="databaseName">Name:</p> */}
108100
<Typography noWrap id="databaseName" className={classes.fontStyles}>
109101
{application[0]}
110102
</Typography>

app/containers/SidebarContainer.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const SidebarContainer = React.memo(() => {
1818
// Extract isLoading and setLoading state from AwsContext. Initial value: isLoading = true
1919
const { isLoading, setLoadingState } = useContext(AwsContext);
2020
// clear interval and set loading state to true when leaving graph containers
21-
const { addApp,setApplications } = useContext(DashboardContext)
21+
const { addApp,setApplications,deleteApp } = useContext(DashboardContext)
2222
/**
2323
* @function handleCLick - check if the 'intervalID' exists. If so, theres a timer running and the fuunction clears the timer using @function clearInterval - function.
2424
* Checks if variable 'isLoading' is false and if so the content is not loading and therefore, sets it to true using the setLoadingState function.
@@ -69,11 +69,12 @@ const SidebarContainer = React.memo(() => {
6969

7070
const handleExitExample = () => {
7171
setExample(false)
72-
setApplications([])
72+
// setApplications([])
7373
setAppIndex(0);
7474
setApp('');
7575
setServicesData([]);
7676
setChart('communications')
77+
deleteApp(0,'all')
7778
}
7879

7980
return (

app/context/DashboardContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ const DashboardContextProvider = React.memo((props: any) => {
8383
// console.log('the current application that was added is : ', result)
8484
}, []);
8585

86-
const deleteApp = useCallback((index: number) => {
87-
const result = ipcRenderer.sendSync('deleteApp', index);
86+
const deleteApp = useCallback((index: number,action:string) => {
87+
const result = ipcRenderer.sendSync('deleteApp', index,action);
8888
setApplications(result);
8989
}, []);
9090

electron/routes/dashboard.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,25 @@ ipcMain.on('getApps', (message: IpcMainEvent) => {
198198
* If user is logged in, makes an update query request to mongoDB to delete the desired application in services array
199199
* @return Returns the new list of applications
200200
*/
201-
ipcMain.on('deleteApp', (message: IpcMainEvent, index) => {
201+
ipcMain.on('deleteApp', (message: IpcMainEvent, index:number, action:string) => {
202202
//If user is not logged in
203203
if (currentUser === 'guest') {
204204
// Retrives file contents from settings.json
205205
const settings = JSON.parse(fs.readFileSync(settingsLocation).toString('utf8'));
206-
const userServices = settings[currentUser].services;
206+
let guestServices = settings[currentUser].services;
207207

208208
// Remove application from settings.json
209-
userServices.splice(index, 1);
209+
console.log({action})
210+
if(action === 'all') guestServices.splice(0);
211+
else guestServices.splice(index, 1);
210212

211213
// Update settings.json with new list
212214
fs.writeFileSync(settingsLocation, JSON.stringify(settings, null, '\t'), {
213215
encoding: 'utf8',
214216
});
215217

216218
// Sync event - return new applications list
217-
message.returnValue = userServices.map((arr: string[]) => [arr[0], arr[1], arr[3], arr[4]]);
219+
message.returnValue = guestServices.map((arr: string[]) => [...arr]);
218220
}
219221

220222
//If user is logged in

0 commit comments

Comments
 (0)