Skip to content

Commit

Permalink
Remove try catch loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ziqing26 committed Feb 11, 2022
1 parent 0588bee commit fe96717
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,19 @@ export class InstructorAuditLogsPageComponent implements OnInit {
this.searchResults = [];
const selectedCourse: Course | undefined =
this.courses.find((course: Course) => course.courseId === this.formModel.courseId);
let currentCheckingTime: string = '';
let searchFrom: number = 0;
let searchUntil: number = 0;
try {
const timeZone: string = selectedCourse ? selectedCourse.timeZone : this.timezoneService.guessTimezone();
currentCheckingTime = 'Search period from';
searchFrom = this.timezoneService.resolveLocalDateTime(

const timeZone: string = selectedCourse ? selectedCourse.timeZone : this.timezoneService.guessTimezone();
const searchFrom: number = this.timezoneService.resolveLocalDateTime(
this.formModel.logsDateFrom, this.formModel.logsTimeFrom, timeZone, true);
currentCheckingTime = 'Search period until';
searchUntil = this.timezoneService.resolveLocalDateTime(
const searchUntil: number = this.timezoneService.resolveLocalDateTime(
this.formModel.logsDateTo, this.formModel.logsTimeTo, timeZone, true);
} catch (e) {
this.statusMessageService.showErrorToast(`${(e as Error).message} for ${currentCheckingTime}`);

const indexOfInvalidTime: number = [searchFrom, searchUntil].findIndex(isNaN);
const sequenceOfTimeChecked: string[] = ['Search period from', 'Search period until'];

if (indexOfInvalidTime !== -1) {
const errorMessage: string = `Invalid datetime range for ${sequenceOfTimeChecked[indexOfInvalidTime]}`;
this.statusMessageService.showErrorToast(errorMessage);
this.isLoading = false;
this.isSearching = false;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,34 +383,38 @@ export class InstructorSessionEditPageComponent extends InstructorSessionBasePag
this.sessionEditFormModel.isEditable = false;
this.feedbackSessionModelBeforeEditing = JSON.parse(JSON.stringify(this.sessionEditFormModel));
this.sessionEditFormModel.isSaving = true;
let currentCheckingTime: string = '';
let submissionStartTime: number = 0;
let submissionEndTime: number = 0;
let sessionVisibleTime: number = 0;
let responseVisibleTime: number = 0;
try {
currentCheckingTime = 'Submission opening time';
submissionStartTime = this.timezoneService.resolveLocalDateTime(

const submissionStartTime: number = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.submissionStartDate, this.sessionEditFormModel.submissionStartTime,
this.sessionEditFormModel.timeZone, true);
currentCheckingTime = 'Submission closing time';
submissionEndTime = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.submissionEndDate, this.sessionEditFormModel.submissionEndTime,
const submissionEndTime: number = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.submissionEndDate, this.sessionEditFormModel.submissionEndTime,
this.sessionEditFormModel.timeZone, true);
if (this.sessionEditFormModel.sessionVisibleSetting === SessionVisibleSetting.CUSTOM) {
sessionVisibleTime = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.customSessionVisibleDate, this.sessionEditFormModel.customSessionVisibleTime,
this.sessionEditFormModel.timeZone, true);
currentCheckingTime = 'Session visible time';
if (this.sessionEditFormModel.sessionVisibleSetting === SessionVisibleSetting.CUSTOM) {
sessionVisibleTime = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.customSessionVisibleDate, this.sessionEditFormModel.customSessionVisibleTime,
this.sessionEditFormModel.timeZone, true);
}
currentCheckingTime = 'Response visible time';
if (this.sessionEditFormModel.responseVisibleSetting === ResponseVisibleSetting.CUSTOM) {
responseVisibleTime = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.customResponseVisibleDate, this.sessionEditFormModel.customResponseVisibleTime,
this.sessionEditFormModel.timeZone, true);
}
} catch (e) {
this.statusMessageService.showErrorToast(`${(e as Error).message} for ${currentCheckingTime}`);
}
if (this.sessionEditFormModel.responseVisibleSetting === ResponseVisibleSetting.CUSTOM) {
responseVisibleTime = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.customResponseVisibleDate, this.sessionEditFormModel.customResponseVisibleTime,
this.sessionEditFormModel.timeZone, true);
}

const indexOfInvalidTime: number =
[submissionStartTime, submissionEndTime, sessionVisibleTime, responseVisibleTime]
.findIndex(isNaN);
const sequenceOfTimeChecked: string[] = [
'submission opening time',
'submission closing time',
'session visible time',
'response visible time'];

if (indexOfInvalidTime !== -1) {
const errorMessage: string = `Invalid datetime range for ${sequenceOfTimeChecked[indexOfInvalidTime]}`;
this.statusMessageService.showErrorToast(errorMessage);
this.sessionEditFormModel.isSaving = false;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,35 +268,38 @@ export class InstructorSessionsPageComponent extends InstructorSessionModalPageC
*/
addNewSessionHandler(): void {
this.sessionEditFormModel.isSaving = true;
let currentCheckingTime: string = '';
let submissionStartTime: number = 0;
let submissionEndTime: number = 0;
let sessionVisibleTime: number = 0;
let responseVisibleTime: number = 0;

try {
currentCheckingTime = 'Submission opening time';
submissionStartTime = this.timezoneService.resolveLocalDateTime(
const submissionStartTime: number = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.submissionStartDate, this.sessionEditFormModel.submissionStartTime,
this.sessionEditFormModel.timeZone, true);
currentCheckingTime = 'Submission closing time';
submissionEndTime = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.submissionEndDate, this.sessionEditFormModel.submissionEndTime,
const submissionEndTime: number = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.submissionEndDate, this.sessionEditFormModel.submissionEndTime,
this.sessionEditFormModel.timeZone, true);
if (this.sessionEditFormModel.sessionVisibleSetting === SessionVisibleSetting.CUSTOM) {
sessionVisibleTime = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.customSessionVisibleDate, this.sessionEditFormModel.customSessionVisibleTime,
this.sessionEditFormModel.timeZone, true);
currentCheckingTime = 'Session edit from';
if (this.sessionEditFormModel.sessionVisibleSetting === SessionVisibleSetting.CUSTOM) {
sessionVisibleTime = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.customSessionVisibleDate, this.sessionEditFormModel.customSessionVisibleTime,
this.sessionEditFormModel.timeZone, true);
}
currentCheckingTime = 'Response visible time';
if (this.sessionEditFormModel.responseVisibleSetting === ResponseVisibleSetting.CUSTOM) {
responseVisibleTime = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.customResponseVisibleDate, this.sessionEditFormModel.customResponseVisibleTime,
this.sessionEditFormModel.timeZone, true);
}
} catch (e) {
this.statusMessageService.showErrorToast(`${(e as Error).message} for ${currentCheckingTime}`);
}
if (this.sessionEditFormModel.responseVisibleSetting === ResponseVisibleSetting.CUSTOM) {
responseVisibleTime = this.timezoneService.resolveLocalDateTime(
this.sessionEditFormModel.customResponseVisibleDate, this.sessionEditFormModel.customResponseVisibleTime,
this.sessionEditFormModel.timeZone, true);
}

const indexOfInvalidTime: number =
[submissionStartTime, submissionEndTime, sessionVisibleTime, responseVisibleTime]
.findIndex(isNaN);
const sequenceOfTimeChecked: string[] = [
'submission opening time',
'submission closing time',
'session visible time',
'response visible time'];

if (indexOfInvalidTime !== -1) {
const errorMessage: string = `Invalid datetime range for ${sequenceOfTimeChecked[indexOfInvalidTime]}`;
this.statusMessageService.showErrorToast(errorMessage);
this.sessionEditFormModel.isSaving = false;
return;
}
Expand Down Expand Up @@ -327,41 +330,41 @@ export class InstructorSessionsPageComponent extends InstructorSessionModalPageC
return;
}
of(...templateSession.questions).pipe(
concatMap((question: FeedbackQuestion) => {
return this.feedbackQuestionsService.createFeedbackQuestion(
feedbackSession.courseId, feedbackSession.feedbackSessionName, {
questionNumber: question.questionNumber,
questionBrief: question.questionBrief,
questionDescription: question.questionDescription,

questionDetails: question.questionDetails,
questionType: question.questionType,

giverType: question.giverType,
recipientType: question.recipientType,

numberOfEntitiesToGiveFeedbackToSetting: question.numberOfEntitiesToGiveFeedbackToSetting,
customNumberOfEntitiesToGiveFeedbackTo: question.customNumberOfEntitiesToGiveFeedbackTo,

showResponsesTo: question.showResponsesTo,
showGiverNameTo: question.showGiverNameTo,
showRecipientNameTo: question.showRecipientNameTo,
});
}),
).subscribe(() => {}, (resp: ErrorMessageOutput) => {
this.sessionEditFormModel.isSaving = false;
this.statusMessageService.showErrorToast(
`The session is created but the template questions cannot be created: ${resp.error.message}`);
}, () => {
this.navigationService.navigateByURLWithParamEncoding(
this.router,
'/web/instructor/sessions/edit',
{ courseid: feedbackSession.courseId, fsname: feedbackSession.feedbackSessionName })
.then(() => {
this.statusMessageService.showSuccessToast('The feedback session has been added.'
+ 'Click the "Add New Question" button below to begin adding questions for the feedback session.');
});
});
concatMap((question: FeedbackQuestion) => {
return this.feedbackQuestionsService.createFeedbackQuestion(
feedbackSession.courseId, feedbackSession.feedbackSessionName, {
questionNumber: question.questionNumber,
questionBrief: question.questionBrief,
questionDescription: question.questionDescription,

questionDetails: question.questionDetails,
questionType: question.questionType,

giverType: question.giverType,
recipientType: question.recipientType,

numberOfEntitiesToGiveFeedbackToSetting: question.numberOfEntitiesToGiveFeedbackToSetting,
customNumberOfEntitiesToGiveFeedbackTo: question.customNumberOfEntitiesToGiveFeedbackTo,

showResponsesTo: question.showResponsesTo,
showGiverNameTo: question.showGiverNameTo,
showRecipientNameTo: question.showRecipientNameTo,
});
}),
).subscribe(() => {}, (resp: ErrorMessageOutput) => {
this.sessionEditFormModel.isSaving = false;
this.statusMessageService.showErrorToast(
`The session is created but the template questions cannot be created: ${resp.error.message}`);
}, () => {
this.navigationService.navigateByURLWithParamEncoding(
this.router,
'/web/instructor/sessions/edit',
{ courseid: feedbackSession.courseId, fsname: feedbackSession.feedbackSessionName })
.then(() => {
this.statusMessageService.showSuccessToast('The feedback session has been added.'
+ 'Click the "Add New Question" button below to begin adding questions for the feedback session.');
});
});
}, (resp: ErrorMessageOutput) => {
this.sessionEditFormModel.isSaving = false;
this.statusMessageService.showErrorToast(
Expand Down
7 changes: 1 addition & 6 deletions src/web/services/timezone.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ export class TimezoneService {
inst.add(1, 'minute');
}

const resolvedTimestamp: number = inst.toDate().getTime();

if (isNaN(resolvedTimestamp)) {
throw new Error('Invalid datetime range');
}
return resolvedTimestamp;
return inst.toDate().getTime();
}
}

0 comments on commit fe96717

Please sign in to comment.