Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add frontend error handling #84

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions health_portal/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@ import { UpdateConsentComponent } from "./components/consent/update-consent/upda
import { ShowConsentComponent } from "./components/consent/show-consent/show-consent.component";

import { HealthAdviceComponent } from './components/analytics/health-advice/health-advice.component';
import { AuthGuard } from "./auth/auth.guard";


const routes: Routes = [
{ path: 'signup', component: SignupComponent },
{ path: 'login', component: LoginComponent },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'list-profiles', component: ListProfilesComponent },
{ path: 'create-profile', component: CreateProfileComponent },
{ path: 'view-profile/:profileId', component: ViewProfileComponent },
{ path: 'update-profile/:profileId', component: UpdateProfileComponent },
{ path: 'list-prescription/:profileId', component: ListPrescriptionComponent },
{ path: 'create-prescription/:profileId', component: CreatePrescriptionComponent },
{ path: 'view-prescription/:profileId/:prescriptionId', component: ViewPrescriptionComponent },
{ path: 'update-prescription/:profileId/:prescriptionId', component: UpdatePrescriptionComponent },
{ path: 'health-advice/:profileId', component: HealthAdviceComponent },
{ path: 'create-consent', component: CreateConsentComponent },
{ path: 'update-consent', component: UpdateConsentComponent },
{ path: 'show-consent', component: ShowConsentComponent }
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'list-profiles', component: ListProfilesComponent, canActivate: [AuthGuard] },
{ path: 'create-profile', component: CreateProfileComponent, canActivate: [AuthGuard] },
{ path: 'view-profile/:profileId', component: ViewProfileComponent, canActivate: [AuthGuard] },
{ path: 'update-profile/:profileId', component: UpdateProfileComponent, canActivate: [AuthGuard] },
{ path: 'list-prescription/:profileId', component: ListPrescriptionComponent, canActivate: [AuthGuard] },
{ path: 'create-prescription/:profileId', component: CreatePrescriptionComponent, canActivate: [AuthGuard] },
{ path: 'view-prescription/:profileId/:prescriptionId', component: ViewPrescriptionComponent, canActivate: [AuthGuard] },
{ path: 'update-prescription/:profileId/:prescriptionId', component: UpdatePrescriptionComponent, canActivate: [AuthGuard] },
{ path: 'health-advice/:profileId', component: HealthAdviceComponent, canActivate: [AuthGuard] },
{ path: 'create-consent', component: CreateConsentComponent, canActivate: [AuthGuard] },
{ path: 'update-consent', component: UpdateConsentComponent, canActivate: [AuthGuard] },
{ path: 'show-consent', component: ShowConsentComponent, canActivate: [AuthGuard] }
];

@NgModule({
Expand Down
22 changes: 22 additions & 0 deletions health_portal/src/app/app.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.bar {
font-size: xx-large;
font-weight: bold;
color: #e4ffc3;
background-color: #02792a;
display: flex;
align-items: center;
justify-content: center;
height: 80px;
}

button {
color: #e4ffc3;
background-color: #029835;
border: none;
padding: 15px 32px;
text-align: center;
font-size: 20px;
border-radius: 75%;
margin: 8px;
float: right;
}
10 changes: 7 additions & 3 deletions health_portal/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<h1>Awesome Health: Your Personalized Health Portal</h1>

<button type="button" routerLink="/signup">Signup</button>
<button type="button" routerLink="/login">Login</button>

<router-outlet></router-outlet>
<div class="bar">
Awesome Health: Your Personalized Health Portal
</div>

<br>

<router-outlet></router-outlet>
16 changes: 16 additions & 0 deletions health_portal/src/app/auth/auth.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { AuthGuard } from './auth.guard';

describe('AuthGuard', () => {
let guard: AuthGuard;

beforeEach(() => {
TestBed.configureTestingModule({});
guard = TestBed.inject(AuthGuard);
});

it('should be created', () => {
expect(guard).toBeTruthy();
});
});
28 changes: 28 additions & 0 deletions health_portal/src/app/auth/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from "../service/auth.service";

@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {

constructor(
private AuthService: AuthService,
private router: Router
) { }

canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
// console.log(this.AuthService.getIdFromJwt())
if (!this.AuthService.getIdFromJwt()){
this.router.navigate(['/'])
return false;
} else {
return true;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { Consent } from "../../../documents/consent";
})
export class CreateConsentComponent {

consent: Consent = new Consent();
permission: boolean;
userId: string;
profileId: string = "1";
Expand All @@ -29,23 +28,20 @@ export class CreateConsentComponent {
}

createConsent() {
this.consent.consentId = this.consentId;
this.consent.userId = this.userId;
this.consent.profileId = this.profileId;
this.consent.permission = this.permission;
this.consent.updatedAt = new Date();
console.log('consent data', this.consent);
this.ConsentService.createConsent(this.consent)
.subscribe(
(response) => {
console.log('Consent created:', response);
},
(error) => {
console.error('Error creating consent:', error);
}
);
alert("Consent created successfully!");
this.router.navigate(['/show-consent'])
const consentData = {};
consentData["consentId"] = this.consentId;
consentData["userId"] = this.userId;
consentData["profileId"] = this.profileId;
consentData["permission"] = this.permission;
consentData["updatedAt"] = new Date();
console.log('consent data', consentData);
if (this.permission === undefined){
alert("Permission cannot be null.");
} else {
this.ConsentService.createConsent(consentData).subscribe((response) => {});
alert("Consent created successfully!");
this.router.navigate(['/show-consent'])
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@ export class UpdateConsentComponent {
consentData["userId"] = this.userId;
consentData["profileId"] = this.profileId;
consentData["permission"] = this.permission;
console.log('consent id', this.consentId);
console.log('consent data', consentData);
this.ConsentService.updateConsent(this.consentId, consentData)
.subscribe(
(response) => {
console.log('Consent updated:', response);
},
(error) => {
console.error('Error updating consent:', error);
}
);
alert("Consent updated successfully!");
this.router.navigate(['/show-consent'])
if (this.permission === undefined) {
alert("Permission cannot be null.");
} else {
this.ConsentService.updateConsent(this.consentId, consentData)
.subscribe(
(response) => {
console.log('Consent updated:', response);
},
(error) => {
console.error('Error updating consent:', error);
}
);
alert("Consent updated successfully!");
this.router.navigate(['/show-consent'])
}
}

deleteConsent() {
Expand Down
13 changes: 8 additions & 5 deletions health_portal/src/app/components/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ export class LoginComponent {

login() {
this.service.login(this.loginForm.value).subscribe((response) => {
console.log(response);
if (response.token) {
console.log(response)
alert("Welcome! You've been logged in :)");
const token = response.token;
localStorage.setItem('JWT', token);
this.router.navigateByUrl('/dashboard');
},
(error) => {
console.log(error)
alert("Errors occurred, please check if your password and email matches");
}
})
);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ <h3>Create Profile</h3>
<ng-container formArrayName="tableRows" *ngFor="let group of getFormControls.controls; let i=index">
<tr [formGroupName]="i">
<td>
<input type="text" class="form-control" formControlName="diseaseName" placeholder="Enter First">
<input type="text" class="form-control" formControlName="diseaseName">
<div class="text-danger" *ngIf="getFormControls.controls[i].get('diseaseName')?.touched">
<span *ngIf="getFormControls.controls[i].get('diseaseName')?.errors?.['required']">Required</span>
<span *ngIf="getFormControls.controls[i].get('diseaseName')?.errors?.['minlength']">3 Chars
Required</span>
</div>
</td>
<td>
<input type="text" class="form-control" formControlName="diagnosedAt" placeholder="Enter Last">
<input type="text" class="form-control" formControlName="diagnosedAt">
<div class="text-danger" *ngIf="getFormControls.controls[i].get('diagnosedAt')?.touched">
<span *ngIf="getFormControls.controls[i].get('diagnosedAt')?.errors?.['required']">Required</span>
</div>
</td>
<td>
<input type="text" class="form-control" formControlName="treatment" placeholder="Enter Last">
<input type="text" class="form-control" formControlName="treatment">
<div class="text-danger" *ngIf="getFormControls.controls[i].get('treatment')?.touched">
<span *ngIf="getFormControls.controls[i].get('treatment')?.errors?.['required']">Required</span>
<span *ngIf="getFormControls.controls[i].get('treatment')?.errors?.['required']">Required</span>
</div>
</td>
<td>
Expand All @@ -44,7 +44,7 @@ <h3>Create Profile</h3>
</tr>
</ng-container>
</tbody>

<div>
<label for="age">Age</label>
<input type="number" class="form-control" id="age" required [(ngModel)]="profile.age" name="age">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class CreateProfileComponent {
private profileService: ProfileService,
private router: Router,
private fb: FormBuilder
) {
) {
this.medicalHistoryForm = this.fb.group({
tableRows: this.fb.array([],[Validators.required])
});
Expand All @@ -38,7 +38,7 @@ export class CreateProfileComponent {
return this.fb.group({
diseaseName: ['',[Validators.required]],
diagnosedAt: ['',[Validators.required]],
treatment:[''],
treatment:['',[Validators.required]],
});
}

Expand All @@ -58,7 +58,7 @@ export class CreateProfileComponent {
}

save() {
this.profile.userId = this.userId;
this.profile.userId = this.userId;
this.profile.medicalHistory = this.medicalHistoryForm.value.tableRows;
console.log(this.medicalHistoryForm.value)
this.profileService.createProfile(this.profile)
Expand All @@ -80,7 +80,7 @@ export class CreateProfileComponent {
onSubmit() {
this.submitted = true;
console.log(this.list);
this.save();
this.save();
}

gotoList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ <h3>Update Profile</h3>
<ng-container formArrayName="tableRows" *ngFor="let group of getFormControls.controls; let i=index">
<tr [formGroupName]="i">
<td>
<input type="text" class="form-control" formControlName="diseaseName" placeholder="Enter First" [value]="getFormControls.controls[i].get('diseaseName').value">
<input type="text" class="form-control" formControlName="diseaseName" [value]="getFormControls.controls[i].get('diseaseName').value">
<div class="text-danger" *ngIf="getFormControls.controls[i].get('diseaseName')?.touched">
<span *ngIf="getFormControls.controls[i].get('diseaseName')?.errors?.['required']">Required</span>
<span *ngIf="getFormControls.controls[i].get('diseaseName')?.errors?.['minlength']">3 Chars
Required</span>
</div>
</td>
<td>
<input type="text" class="form-control" formControlName="diagnosedAt" placeholder="Enter Last">
<input type="text" class="form-control" formControlName="diagnosedAt">
<div class="text-danger" *ngIf="getFormControls.controls[i].get('diagnosedAt')?.touched">
<span *ngIf="getFormControls.controls[i].get('diagnosedAt')?.errors?.['required']">Required</span>
</div>
</td>
<td>
<input type="text" class="form-control" formControlName="treatment" placeholder="Enter Last">
<input type="text" class="form-control" formControlName="treatment">
<div class="text-danger" *ngIf="getFormControls.controls[i].get('treatment')?.touched">
<span *ngIf="getFormControls.controls[i].get('treatment')?.errors?.['required']">Required</span>
<span *ngIf="getFormControls.controls[i].get('treatment')?.errors?.['required']">Required</span>
</div>
</td>
<td>
Expand All @@ -44,7 +44,7 @@ <h3>Update Profile</h3>
</tr>
</ng-container>
</tbody>

<div>
<label for="age">Age</label>
<input type="number" class="form-control" id="age" required [(ngModel)]="profile.age" name="age">
Expand All @@ -69,6 +69,8 @@ <h3>Update Profile</h3>
<label for="languagePreference">Primary Language Preference</label>
<input type="text" class="form-control" id="languagePreference" required [(ngModel)]="profile.languagePreference" name="languagePreference">
</div>
<br>

<button type="submit" class="btn btn-success">Submit</button>
</form>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class UpdateProfileComponent {
private profileService: ProfileService,
private router: Router,
private fb: FormBuilder
) {
) {
this.medicalHistoryForm = this.fb.group({
tableRows: this.fb.array([],[Validators.required])
});
Expand All @@ -50,7 +50,7 @@ export class UpdateProfileComponent {
return this.fb.group({
diseaseName: [medicalHistory ? medicalHistory.diseaseName : '',[Validators.required]],
diagnosedAt: [medicalHistory ? medicalHistory.diagnosedAt : '',[Validators.required]],
treatment:[medicalHistory ? medicalHistory.treatment : ''],
treatment:[medicalHistory ? medicalHistory.treatment : '',[Validators.required]],
});
}

Expand All @@ -70,7 +70,7 @@ export class UpdateProfileComponent {
}

save() {
this.profile.userId = this.userId;
this.profile.userId = this.userId;
this.profile.medicalHistory = this.medicalHistoryForm.value.tableRows;
console.log(this.medicalHistoryForm.value)
this.profileService.createProfile(this.profile)
Expand All @@ -92,11 +92,11 @@ export class UpdateProfileComponent {
onSubmit() {
this.submitted = true;
console.log(this.list);
this.save();
this.save();
}

gotoList() {
this.router.navigate(['/list-profiles']);
}

}
}
Loading
Loading