Skip to content

Commit 147d5ed

Browse files
authored
Merge pull request #13 from oslabs-beta/elena/updatingToDB
Elena/updating to db
2 parents d58b4df + a455254 commit 147d5ed

File tree

13 files changed

+208
-97
lines changed

13 files changed

+208
-97
lines changed

app/components/Login.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const Login = React.memo(() => {
1414
* Function that will be called when the form button is clicked
1515
* It handles submitting the login information
1616
*/
17-
17+
// v10: ipcRenderer returning a string with the response of user mode (light or dark).
18+
// There is currently no redirection after failed login.
1819
const handleSubmit = (e: any) => {
1920
e.preventDefault();
2021
const inputFields: HTMLInputElement[] = e.currentTarget.querySelectorAll('input');
@@ -24,7 +25,8 @@ const Login = React.memo(() => {
2425
// inputFields.forEach(input => (input.value = ''));
2526
const response: boolean | string = ipcRenderer.sendSync('login', { username, password });
2627
if (typeof (response) === 'string') {
27-
console.log('response', response)
28+
console.log('Hi, login successful and response string (mode) is:', response);
29+
console.log('Redirected to Occupied from Login.');
2830
setUser(username);
2931
setMode(response);
3032
navigate('/');

app/components/Occupied.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,18 @@ interface StyleProps {
5555
}
5656
type ClickEvent = React.MouseEvent<HTMLElement>;
5757

58+
// type OccupiedProps = {
59+
// switch: any;
60+
// setSwitch: any;
61+
// };
62+
63+
//v10: Memoized function, witouth any props. Should theoretically be called only once.
5864
const Occupied = React.memo(() => {
65+
console.log('Hi, inside Occupied. Memoize function invoked');
66+
5967
const { awsData, fetchAwsData, fetchAwsAppInfo, setLoadingState } = useContext(AwsContext);
6068
const { setServicesData, app, setApp } = useContext(ApplicationContext);
69+
// const { user, getApplications, deleteApp, mode } = useContext(DashboardContext);
6170
const { user, applications, getApplications, deleteApp, mode } = useContext(DashboardContext);
6271
const { commsData } = useContext(CommsContext);
6372
const [serviceModalOpen, setServiceModalOpen] = useState<boolean>(false);
@@ -75,12 +84,16 @@ const Occupied = React.memo(() => {
7584
// const [showAwsContainer, setShowAwsContainer] = useState(false);
7685
const navigate = useNavigate();
7786

87+
7888
// Grab services and applications whenever the user changes
89+
// v10: Runs once when Occupied is memoized, and subsequently when user is updated.
7990
useEffect(() => {
91+
console.log('Hi, inside Occupied.ts useEffect function.');
8092
setServicesData([]);
8193
getApplications();
8294
}, [user]);
8395

96+
8497
// Dynamic refs
8598
const delRef = useRef<any>([]);
8699

@@ -91,6 +104,8 @@ const Occupied = React.memo(() => {
91104
};
92105

93106
// Handle clicks on Application cards
107+
// v10 info: when card is clicked (not on the delete button) if the service is an AWS instance,
108+
// you are redirected to AWSGraphsContainer passing in the state object containing typeOfService
94109
const handleClick = (
95110
event: ClickEvent,
96111
selectedApp: string,
@@ -106,9 +121,14 @@ const Occupied = React.memo(() => {
106121
selectedService === 'AWS/EKS'
107122
) {
108123
setAppIndex(i);
124+
// setApp is never invoked, function is not established?
109125
setApp(selectedApp);
110126
navigate(`/aws/:${app}`, { state: { typeOfService: selectedService } });
111127
} else {
128+
console.log('In handleClick Method (local) in Occupied.tsx');
129+
console.log('The following are the new values for appIndex, app, servicesData, and serviceModalOpen:');
130+
console.log(i, ' ', selectedApp, ' ', 'N/A ', serviceModalOpen );
131+
112132
setAppIndex(i);
113133
setApp(selectedApp);
114134
setServicesData([]);

app/containers/MainContainer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import AwsGraphsContainer from '../containers/AWSGraphsContainer';
1414
import '../stylesheets/MainContainer.scss';
1515

1616
const MainContainer = React.memo(() => {
17-
const { mode } = useContext(DashboardContext);
17+
const { mode, applications } = useContext(DashboardContext);
1818
const currentModeCSS = mode === 'light' ? lightAndDark.lightModeMain : lightAndDark.darkModeMain;
19-
19+
2020
return (
2121
<>
2222
<div className="main-container" style={currentModeCSS}>

app/context/ApplicationContext.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const ApplicationContext = React.createContext<any>(null);
2020
const ApplicationContextProvider: React.FC<AppContextProps> = React.memo(props => {
2121
const children = props.children;
2222
const [servicesData, setServicesData] = useState([]);
23+
// v10 note: there is not function for setApp so app is never updated.
2324
const [app, setApp] = useState<string>('');
2425
const [savedMetrics, setSavedMetrics] = useState({});
2526
const [appIndex, setAppIndex] = useState<number>(0);
@@ -54,26 +55,38 @@ const ApplicationContextProvider: React.FC<AppContextProps> = React.memo(props =
5455
* 4. Remove the listener for `servicesResponse`
5556
* @param application - application name
5657
*/
58+
// v10: Invoked by connectToDB, passing in app (card title)
5759
const fetchServicesNames = useCallback((application: string) => {
58-
console.log('app when fetch services name', application);
60+
console.log('Hi, inside ApplicationConext - fetchServicesNames callback. Sending servicesRequest to ipcMain.');
61+
console.log('app when fetch services name: ', application);
5962
// setApp(application);
6063

6164
ipcRenderer.send('servicesRequest');
6265

6366
ipcRenderer.on('servicesResponse', (event: Electron.Event, data: any) => {
6467
let result: any;
6568
// Parse JSON data into object
66-
if (tryParseJSON(data)) result = JSON.parse(data);
69+
// if (tryParseJSON(data)) result = JSON.parse(data);
70+
result = JSON.parse(data);
6771
console.log('result from ipcrenderer services response is: ', result);
72+
console.log('Calling setServicesData passing in above result. Current servicesData is the following: ', servicesData);
6873
setServicesData(result);
74+
console.log('Leaving fetchedServicesNames function.');
6975
ipcRenderer.removeAllListeners('servicesResponse');
7076
});
7177
}, []);
7278

73-
const connectToDB = useCallback((username: string, index: number, application: string, URI: string) => {
79+
/**
80+
* @function connectToTB - invoked in Services Modal when Service Modal component is first rendered or when useEffect invoked.
81+
* creates an event emitter that connects to the user provided URI for the service (should be the database URI...)
82+
* v10 notes: seems to only be set up for local instances, not when a cloud based service is clicked, causes an issue since a user provided
83+
* database should not exist...
84+
* @params application - is the name of the card taht was clicked on
85+
*/
86+
const connectToDB = useCallback((username: string, index: number, application: string, URI: string, databaseType: string) => {
87+
console.log('Hi, inside ApplicationContext, connectToDB function was invoked.');
7488
ipcRenderer.removeAllListeners('databaseConnected');
75-
ipcRenderer.send('connect', username, index, URI);
76-
89+
ipcRenderer.send('connect', username, index, URI, databaseType);
7790
ipcRenderer.on('databaseConnected', (event: Electron.Event, data: any) => {
7891
fetchServicesNames(application);
7992
});

app/context/AwsContext.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,22 @@ const AwsContextProvider: React.FC<Props> = React.memo(({ children }) => {
6969

7070
};
7171

72+
/**
73+
* @event fetchAWSAppInfo - invoked in AWSGraphsContainer
74+
* @desc
75+
* @params username:
76+
* index:
77+
*/
78+
7279
const fetchAwsAppInfo = (username: string, index: number) => {
80+
console.log('Hi, AwsContext -> fetchAwsAppInfo was invoked.');
7381
ipcRenderer.removeAllListeners('awsAppInfoResponse');
74-
82+
console.log('fetchAAWSAppInfo was invoked for card index: ', index);
83+
console.log('Sending request to ipcMain.on for channel awsAppInfoRequest...');
7584
ipcRenderer.send('awsAppInfoRequest', username, index);
7685
ipcRenderer.on('awsAppInfoResponse', (event: Electron.Event, res: any) => {
7786
const appInfo = JSON.parse(res);
78-
87+
console.log('received response: ', appInfo);
7988
setAwsAppInfo(appInfo);
8089
});
8190
};

app/context/DashboardContext.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,29 @@ const DashboardContextProvider = React.memo((props: any) => {
4242
const children = props.children;
4343

4444
// Initial user will always be the guest
45-
const [user, setUser] = useState('guest');
46-
const [applications, setApplications] = useState<string[]>([]);
45+
const [user, setUser] = useState<string>('guest');
46+
const [applications, setApplications] = useState<string[][]>([]);
4747
const [mode, setMode] = useState<string>('light');
4848

49+
4950
const getApplications = useCallback(() => {
5051
const result = ipcRenderer.sendSync('getApps');
5152
setApplications(result);
5253
}, []);
5354

5455
const addApp = useCallback((fields: IFields) => {
5556
const { typeOfService, database, URI, name, description } = fields;
57+
const newApp = [name, database, URI, description, typeOfService];
5658
console.log('what is the service that was passed into add app: ', typeOfService)
5759
const result = ipcRenderer.sendSync(
5860
'addApp',
5961
JSON.stringify([name, database, URI, description, typeOfService])
6062
);
6163
setApplications(result);
62-
console.log('the current application that was added is : ', result)
64+
// console.log('applications: ', applications);
65+
// console.log('new app to add: ', newApp);
66+
// setApplications([...applications, newApp]);
67+
// console.log('the current application that was added is : ', result);
6368
}, []);
6469

6570
const addAwsApp = useCallback((awsFields: AwsFields) => {

app/modals/AddModal.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface IFields {
1313

1414
interface IDashboard {
1515
addApp: (fields: IFields) => void;
16+
setApplications: any;
1617
}
1718

1819
interface AddModalProps {
@@ -23,7 +24,7 @@ type InputElement = React.ChangeEvent<HTMLSelectElement | HTMLInputElement | HTM
2324
type FormElement = React.FormEvent<HTMLFormElement>;
2425

2526
const AddModal: React.FC<AddModalProps> = React.memo(({ setOpen }) => {
26-
const { addApp }: IDashboard = useContext(DashboardContext);
27+
const { addApp, setApplications }: IDashboard = useContext(DashboardContext);
2728

2829
const [fields, setFields] = useState<IFields>({
2930
typeOfService: 'Docker',
@@ -36,6 +37,8 @@ const AddModal: React.FC<AddModalProps> = React.memo(({ setOpen }) => {
3637
// Submit form data and save to database
3738
const handleSubmit = (event: FormElement) => {
3839
event.preventDefault();
40+
// const newApp = [name, database, URI, description, typeOfService];
41+
// setApplications(prev => [...prev, ...newApp])
3942
addApp(fields);
4043
setOpen(false); // Close modal on submit
4144
};

app/modals/ServicesModal.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ interface IService {
1717
microservice: string;
1818
}
1919

20+
// v10: Seems to never have been updated for cloud-based info...
21+
// servicesModal is re-rendered depending on i and application passed in...
2022
const ServicesModal: React.FC<ServicesModalProps> = React.memo(({ i, app }) => {
21-
const { user, applications } = useContext(DashboardContext)
22-
console.log('hereerer', useContext(DashboardContext))
23-
console.log('aappp', applications[i][2])
23+
console.log('Hi, inside ServicesModal. Memoize function invoked in ServicesModal.');
24+
console.log('ServicesModal current props (index, app): ', i, ' ', app);
25+
26+
const { user, applications } = useContext(DashboardContext);
27+
console.log('user from Dashboard Context:', user);
28+
console.log('applications from Dashboard Context: ', applications);
29+
console.log('applications[i][2]: ', applications[i][2]);
2430
const { servicesData, connectToDB } = useContext(ApplicationContext);
2531
const [services, setServices] = useState<Array<string>>([]);
2632

@@ -33,10 +39,19 @@ const ServicesModal: React.FC<ServicesModalProps> = React.memo(({ i, app }) => {
3339
}
3440
};
3541

42+
// v10: connectToDB function definition in Application Context.
43+
// parameters for connectToDB call: user, i (index), app (card title), app URL (url on card)
44+
// applications[i][2] refers to the user provided URI
45+
// adding database type to make connection and fetchServiceNames more efficient
3646
useEffect(() => {
37-
connectToDB(user, i, app, applications[i][2]);
47+
console.log('Hi, inside useEffect in ServicesModal. Calling connectToDB function.');
48+
console.log("Passing the following parameters for user, i, app, applications, ");
49+
console.log(user, ' ', i, ' ', app, ' ', applications, applications[i][1]);
50+
connectToDB(user, i, app, applications[i][2], applications[i][1]);
3851
}, [i]);
3952

53+
54+
4055
return (
4156
<div className="servicesContainer">
4257
{!servicesData.length ? (

electron/databases/mongo.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import mongoose from 'mongoose';
22
// import { MongoError } from 'mongodb';
3-
3+
const testURL = 'mongodb+srv://seconddbtest:[email protected]/?retryWrites=true&w=majority';
44
// Mongoose connection wrapped in function that takes the index of the selected database as the parameter. This index is used to target the correct database for querying.
55
const connectMongoose = async (i:number, URI: string) => {
66
try {
7-
await mongoose.connection.close();
7+
// await mongoose.connection.close();
88
const db = await mongoose.connect(URI);
99

1010
return db;
1111
} catch (err) {
1212
console.log(`${__dirname}/mongo.ts/connectMongoose: ${err}`);
1313
}
1414
};
15+
16+
// const connectMongoose = async (i: number, URI: string) => {
17+
// try {
18+
// const db2 = mongoose.createConnection(testURL);
19+
// console.log('connection to user provided db established..');
20+
// return db2;
21+
// } catch (error) {
22+
// console.log('Error connecting to second db... ', error);
23+
// }
24+
// }
25+
1526
export default connectMongoose;

electron/models/ServicesModel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ const ServicesSchema = new Schema({
1717
});
1818

1919
export default mongoose.model<IService>('services', ServicesSchema);
20+
// export default ServicesSchema;

0 commit comments

Comments
 (0)