Skip to content

Commit

Permalink
Added feature of survey ownership
Browse files Browse the repository at this point in the history
Added `ownerId` to the survey model which stores the Firebase user id of
the user who creates the survey.
Modified the `edit-survey` component to store the uid during survey
creation.
Modified the dashboard page so that only surveys belonging to the logged-in
user are shown.
Modified the `view-survey` component accordingly.
  • Loading branch information
loct824 committed Apr 4, 2023
1 parent a882c91 commit c63210d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/app/components/edit-survey/edit-survey.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NzNotificationService } from 'ng-zorro-antd/notification';
import { Survey } from 'src/app/model/Survey';
import { AngularFirestore } from '@angular/fire/compat/firestore';
import { serverTimestamp } from 'firebase/firestore';
import { AuthService } from 'src/app/service/auth.service';

@Component({
selector: 'app-edit-survey',
Expand All @@ -23,6 +24,7 @@ export class EditSurveyComponent {
private fb: FormBuilder,
private modalRef: NzModalRef,
private notification: NzNotificationService,
private auth: AuthService,
) { }

ngOnInit(): void {
Expand Down Expand Up @@ -67,6 +69,7 @@ export class EditSurveyComponent {
title: this.form?.value.title?.trim(),
desc: this.form?.value.desc?.trim(),
isPublished: this.form?.value.isPublished,
ownerId: this.auth.user?.uid,
questions: this.form?.value.questions.map((question: any) => ({
label: question.label?.trim(),
type: question.type?.trim(),
Expand Down
5 changes: 4 additions & 1 deletion src/app/components/view-survey/view-survey.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NzNotificationService } from 'ng-zorro-antd/notification';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { Result } from 'src/app/model/Result';
import { DatePipe } from '@angular/common';
import { AuthService } from 'src/app/service/auth.service';

@Component({
selector: 'app-view-survey',
Expand All @@ -22,12 +23,14 @@ export class ViewSurveyComponent implements OnInit {
private modalRef: NzModalRef,
private notification: NzNotificationService,
private afs: AngularFirestore,
private auth : AuthService
) {

}

ngOnInit(): void {
if (this.survey?.id) {
const currentUserId = this.auth.user?.uid;
if (currentUserId && this.survey?.id) {
this.results$ = this.afs.collection<Result>("results", ref => ref.where("surveyId", "==", this.survey?.id).orderBy("createdTime", "desc"))
.snapshotChanges()
.pipe(
Expand Down
1 change: 1 addition & 0 deletions src/app/model/Survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Survey {
required: boolean;
}[];
isPublished: boolean;
ownerId : String;
createdTime: firebase.firestore.Timestamp;
updatedTime: firebase.firestore.Timestamp;
}
34 changes: 22 additions & 12 deletions src/app/pages/dashboard/dashboard.component.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { Survey } from 'src/app/model/Survey';
import { AngularFirestore } from '@angular/fire/compat/firestore';
import { catchError, map, Observable, of } from 'rxjs';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { NzModalService } from 'ng-zorro-antd/modal';
import { EditSurveyComponent } from 'src/app/components/edit-survey/edit-survey.component';
import { ViewSurveyComponent } from 'src/app/components/view-survey/view-survey.component';
import { AuthService } from 'src/app/service/auth.service';

@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.less']
})
export class DashboardComponent {
export class DashboardComponent implements OnInit{
public isDeleting: boolean = false;
public surveys$: Observable<Survey[]> = this.afs.collection<Survey>("surveys", ref => ref.orderBy("createdTime", "desc"))
.snapshotChanges()
.pipe(
map(actions => actions.map(action => ({ ...action.payload.doc.data() as any, id: action.payload.doc.id }))),
catchError((error) => {
console.error("Error getting surveys:", error);
this.notification.error("Error", `Error getting surveys: ${error}`);
return of([]);
})
)
public surveys$: Observable<Survey[]> = of([]);

constructor(
private modal: NzModalService,
private notification: NzNotificationService,
private afs: AngularFirestore,
private auth: AuthService
) { }

ngOnInit(): void {
const currentUserId = this.auth.user?.uid;
if (currentUserId){
this.surveys$ = this.afs.collection<Survey>("surveys",ref=> ref
.where("ownerId","==",currentUserId)
.orderBy("createdTime","desc"))
.snapshotChanges()
.pipe(
map(actions => actions.map(action => ({ ...action.payload.doc.data() as any, id: action.payload.doc.id }))),
catchError((error) => {
console.error("Error getting surveys:", error);
this.notification.error("Error", `Error getting surveys: ${error}`);
return of([]);
}));
}
}

public addSurvey() {
let modal = this.modal.create({
nzTitle: 'Add Survey',
Expand Down

0 comments on commit c63210d

Please sign in to comment.