Skip to content

Commit a3aec8e

Browse files
authored
Merge pull request #11 from oslabs-beta/elena/updatingToDB
updated addAWSApp in dashboard.ts to save data to db or locally depending on if user is logged in or not
2 parents 64959e9 + dfc4055 commit a3aec8e

File tree

6 files changed

+94
-15
lines changed

6 files changed

+94
-15
lines changed

app/context/DashboardContext.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const DashboardContextProvider = React.memo((props: any) => {
6464

6565
const addAwsApp = useCallback((awsFields: AwsFields) => {
6666
const { typeOfService, instance, region, accessKey, secretAccessKey, name, description, awsUrl } = awsFields;
67+
// call to ipcRenderer returns an array consisting of name, 'AWS', region, 'AWS/(instance)', instance
6768
const result = ipcRenderer.sendSync(
6869
'addAwsApp', //"addApp"
6970
JSON.stringify([name, 'AWS', region, description, typeOfService, instance, accessKey, secretAccessKey, awsUrl])

app/modals/AwsModal.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type FormElement = React.FormEvent<HTMLFormElement>;
2828
const AwsModal: React.FC<AddModalProps> = React.memo(({ setOpen }) => {
2929
const { addAwsApp }: IDashboard = useContext(DashboardContext);
3030

31+
// instance is instanceID
3132
const [awsFields, setAwsFields] = useState<AwsFields>({
3233
typeOfService: 'AWS/EC2',
3334
instance: '',

electron/routes/dashboard.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ const User = require('../models/UserModel')
88
const mongoose = require('mongoose');
99
// const db = require('../databases/mongo')
1010

11-
const MONGO_URI = ''
11+
const MONGO_URI = 'mongodb+srv://chronoslany:[email protected]/?retryWrites=true&w=majority';
1212

13-
mongoose.connect(MONGO_URI, {
14-
useNewUrlParser: true,
15-
useUnifiedtopology: true,
16-
})
13+
main().catch(err => console.log(err));
14+
async function main() {
15+
await mongoose.connect(MONGO_URI);
16+
console.log('user info db connection established...')
17+
}
18+
// mongoose.connect(MONGO_URI, {
19+
// useNewUrlParser: true,
20+
// useUnifiedtopology: true,
21+
// })
1722

23+
// may need to first check if db has a current user and if not, change it to guest
1824
// GLOBAL VARIABLES
1925
let currentUser = 'guest';
2026
const settingsLocation = path.resolve(__dirname, '../../settings.json');
@@ -67,6 +73,8 @@ function clearGuestSettings() {
6773
*/
6874
ipcMain.on('addApp', (message: IpcMainEvent, application: any) => {
6975
const newApp = JSON.parse(application)
76+
console.log('parsed newApp: ', newApp);
77+
console.log('currentUser', currentUser);
7078
const createdOn = moment().format('lll');
7179
newApp.push(createdOn);
7280

@@ -103,10 +111,33 @@ ipcMain.on('addApp', (message: IpcMainEvent, application: any) => {
103111

104112
/**
105113
* @event addAwsApp
114+
* @param name, 'AWS', region, description, typeOfService, instanceID, accessKey, secretAccessKey, awsURL
106115
* @desc Adds an AWS application to the user's list in the settings.json with the provided fields
107116
* @return New list of applications
108117
*/
109118
ipcMain.on('addAwsApp', (message: IpcMainEvent, application: any) => {
119+
120+
const newAwsApp = JSON.parse(application);
121+
console.log('parsed newApp: ', newAwsApp);
122+
console.log('currentUser', currentUser);
123+
const createdOn = moment().format('lll');
124+
newAwsApp.push(createdOn);
125+
126+
if(currentUser !== 'guest'){
127+
return User.findOneAndUpdate({ username: currentUser }, {
128+
$push: {services: newAwsApp}
129+
}, {new: true})
130+
.then((data) => {
131+
console.log('User updated', data);
132+
// returning each array element name, 'AWS', region, 'AWS/(instance)', Date
133+
message.returnValue = data.services.map((arr: string[]) => [arr[0], arr[1], arr[2], arr[4], arr[5]])
134+
})
135+
.catch((error) => {
136+
console.log(`addAWSApp failed : ${error}`)
137+
})
138+
} else {
139+
// if user is not guest, should not have to pull info from settings.json file
140+
console.log('current user is a guest, data will be saved locally...')
110141
// Retrieves file contents from settings.json
111142
const settings = JSON.parse(fs.readFileSync(settingsLocation).toString('utf8'));
112143
const services = settings[currentUser].services;
@@ -115,10 +146,10 @@ ipcMain.on('addAwsApp', (message: IpcMainEvent, application: any) => {
115146
// name, instance, region, description, typeOfService, accessKey, secretAccessKey
116147

117148
// Add new applicaiton to list
118-
const newAwsApp = JSON.parse(application);
149+
// const newAwsApp = JSON.parse(application);
119150

120151
// Add a creation date to the application on the 5th index
121-
const createdOn = moment().format('lll');
152+
// const createdOn = moment().format('lll');
122153
newAwsApp.splice(5, 0, createdOn);
123154

124155
// Add app to list of applications
@@ -130,6 +161,7 @@ ipcMain.on('addAwsApp', (message: IpcMainEvent, application: any) => {
130161

131162
// Sync event - return new applications list
132163
message.returnValue = services.map((arr: string[]) => [arr[0], arr[1], arr[2], arr[4], arr[5]]);
164+
}
133165
});
134166

135167
/**

electron/routes/data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const postgresFetch = fetchData.postgresFetch;
2525
const AWS = require('aws-sdk');
2626

2727
require('dotenv').config({
28-
path: path.join(__dirname, './.env'),
28+
path: path.join(__dirname, './.env'),
2929
});
3030
// Initiate pool variable for SQL setup
3131
let pool: any;

package-lock.json

Lines changed: 51 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"electron-log": "^5.0.0-beta.23",
2222
"jest-environment-jsdom": "^29.5.0",
2323
"moment": "^2.29.4",
24+
"mongodb": "^5.4.0",
2425
"mongoose": "^6.8.0",
2526
"pg": "^8.8.0",
2627
"react": "^17.0.0",

0 commit comments

Comments
 (0)