Skip to content

Commit 5d29352

Browse files
committed
Converted to DB to load microservices examples
Updated signup requirements User prompted to log in after sign up Co-authored-by: Jon Cruz <[email protected]> Co-authored-by: Elena Atencio <[email protected]> Co-authored-by: John Donato <[email protected]>
1 parent 70e824d commit 5d29352

File tree

5 files changed

+64
-26
lines changed

5 files changed

+64
-26
lines changed

app/components/SignUp.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ const SignUp:React.FC = React.memo(() => {
4848
setFailedSignUp(<p>Sorry, your sign up failed. Please try a different username or email</p>)
4949
} else {
5050
console.log('in frontend', username)
51-
setUser(username);
52-
navigate('/');
51+
// setUser(username);
52+
navigate('/login');
53+
alert('USER CREATED: PLEASE LOG IN')
5354
}
5455
}).catch(error => {
5556
console.error('Failed to sign up:', error);
@@ -66,13 +67,13 @@ const SignUp:React.FC = React.memo(() => {
6667

6768
<form className="form" onSubmit={handleSubmit}>
6869
<label className="username">
69-
<input type="text" name="username" id="username" placeholder="enter username" />
70+
<input type="text" name="username" id="username" minLength={4} placeholder="enter username" />
7071
</label>
7172
<label className="email">
7273
<input type="email" name="email" id="email" placeholder="[email protected]" />
7374
</label>
7475
<label className="password">
75-
<input type="password" name="password" id="password" placeholder="enter password" />
76+
<input type="password" name="password" id="password" minLength={9} placeholder="enter password" />
7677
</label>
7778
<label className="passwordConfirm">
7879
<input type="password" name="passwordConfirm" id="passwordConfirm" placeholder="confirm password" />

app/context/ApplicationContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ const ApplicationContextProvider: React.FC<AppContextProps> = React.memo(props =
5353
});
5454
}, []);
5555

56-
const connectToDB = useCallback((username: string, index: number, application: string) => {
56+
const connectToDB = useCallback((username: string, index: number, application: string, URI: string) => {
5757
ipcRenderer.removeAllListeners('databaseConnected');
58-
ipcRenderer.send('connect', username, index);
58+
ipcRenderer.send('connect', username, index, URI);
5959

6060
ipcRenderer.on('databaseConnected', (event: Electron.Event, data: any) => {
6161
fetchServicesNames(application);

app/modals/ServicesModal.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ interface IService {
1818
}
1919

2020
const ServicesModal: React.FC<ServicesModalProps> = React.memo(({ i, app }) => {
21-
const { user } = useContext(DashboardContext)
21+
const { user, applications } = useContext(DashboardContext)
22+
console.log('hereerer', useContext(DashboardContext))
23+
console.log('aappp', applications[i][2])
2224
const { servicesData, connectToDB } = useContext(ApplicationContext);
2325
const [services, setServices] = useState<Array<string>>([]);
2426

@@ -32,7 +34,7 @@ const ServicesModal: React.FC<ServicesModalProps> = React.memo(({ i, app }) => {
3234
};
3335

3436
useEffect(() => {
35-
connectToDB(user, i, app);
37+
connectToDB(user, i, app, applications[i][2]);
3638
}, [i]);
3739

3840
return (

electron/routes/dashboard.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,7 @@ ipcMain.on('getApps', (message: IpcMainEvent) => {
159159
.then((data) => {
160160
console.log('User found', data);
161161
services = data.services;
162-
const dashboardList: string[][] = services.map((arr: string[]) => [
163-
arr[0],
164-
arr[1],
165-
arr[3],
166-
arr[4],
167-
arr[5],
162+
const dashboardList: string[][] = services.map((arr: string[]) => [...arr
168163
]);
169164
message.returnValue = dashboardList;
170165
})

electron/routes/data.ts

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import { fetchData } from './dataHelpers';
1616
import MetricsModel from '../models/MetricsModel';
1717
const log = require('electron-log');
1818

19+
20+
const mongoose = require('mongoose');
21+
const User = require('../models/UserModel')
22+
1923
const mongoFetch = fetchData.mongoFetch;
2024
const postgresFetch = fetchData.postgresFetch;
2125
const AWS = require('aws-sdk');
@@ -37,24 +41,60 @@ const settingsLocation = path.resolve(__dirname, '../../settings.json');
3741
* @desc Connects user to database and sets global currentDatabaseType which
3842
* is accessed in info.commsData and info.healthData
3943
*/
40-
ipcMain.on('connect', async (message: Electron.IpcMainEvent, username: string, index: number) => {
44+
ipcMain.on('connect', async (message: Electron.IpcMainEvent, username: string, index: number, URI: string) => {
4145
try {
4246
// Extract databaseType and URI from settings.json at particular index
4347
// get index from application context
44-
const fileContents = JSON.parse(fs.readFileSync(settingsLocation, 'utf8'));
45-
const userDatabase = fileContents[username].services[index];
46-
// We get index from sidebar container: which is the mapplication (DEMO)
47-
const [databaseType, URI] = [userDatabase[1], userDatabase[2]];
4848

49-
// Connect to the proper database
50-
if (databaseType === 'MongoDB') await connectMongo(index,URI);
51-
if (databaseType === 'SQL') pool = await connectPostgres(index, URI);
49+
// Connect to User database instantiated in 'dashboard.ts'
50+
if (username !== 'guest') {
51+
52+
const MONGO_URI = URI
53+
mongoose.connect(MONGO_URI, {
54+
useNewUrlParser: true,
55+
useUnifiedtopology: true,
56+
})
57+
58+
// Check for existing user in DB, if found, connect to load application based on database type
59+
return User.findOne({ username: username })
60+
.then(async (data) => {
61+
const databaseType = data.services[index][1]
62+
const appURI = data.services[index][2]
63+
console.log('database type', databaseType)
64+
if (databaseType === 'MongoDB') {
65+
await connectMongo(index, appURI)
66+
currentDatabaseType = databaseType;
67+
message.sender.send('databaseConnected', 'connected!');
68+
} else if (databaseType === 'SQL') {
69+
pool = await connectPostgres(index, appURI);
70+
currentDatabaseType = databaseType;
71+
message.sender.send('databaseConnected', 'connected!');
72+
}
73+
})
74+
.catch((error) => {
75+
console.log(` Error in connect, failed to load application : ${error}`)
76+
// return false;
77+
})
78+
}
5279

53-
// Currently set to a global variable
54-
currentDatabaseType = databaseType;
80+
//LOCAL INSTANCE: SETTINGS.JSON
81+
else {
5582

56-
message.sender.send('databaseConnected', 'connected!');
57-
// eslint-disable-next-line no-shadow
83+
const fileContents = JSON.parse(fs.readFileSync(settingsLocation, 'utf8'));
84+
const userDatabase = fileContents[username].services[index];
85+
// We get index from sidebar container: which is the mapplication (DEMO)
86+
const [databaseType, URI] = [userDatabase[1], userDatabase[2]];
87+
88+
// Connect to the proper database
89+
if (databaseType === 'MongoDB') await connectMongo(index,URI);
90+
if (databaseType === 'SQL') pool = await connectPostgres(index, URI);
91+
92+
// Currently set to a global variable
93+
currentDatabaseType = 'MongoDB';
94+
95+
message.sender.send('databaseConnected', 'connected!');
96+
// eslint-disable-next-line no-shadow
97+
}
5898
} catch ({ message }) {
5999
console.log('Error in "connect" event', message);
60100
}

0 commit comments

Comments
 (0)