Skip to content

Commit

Permalink
Merge branch 'GaganSinghGit-create-unit-modal' into add-tii-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jakerenzella committed Nov 9, 2023
2 parents e430d61 + 0a2a1e9 commit 14ffb9b
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 199 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<h3 mat-dialog-title>Create Unit</h3>

<form mat-dialog-content action="" #createUnitForm="ngForm" (ngSubmit)="createUnit(createUnitForm.value)" class="pt-8">
<div class="flex flex-col">
<mat-form-field appearance="outline">
<mat-label>Unit Code</mat-label>
<input matInput placeholder="COS123456" name="unitCode" ngModel />
</mat-form-field>

<mat-form-field appearance="outline">
<mat-label>Unit Name</mat-label>
<input matInput placeholder="Introduction to OnTrack" name="unitName" ngModel />
</mat-form-field>

<mat-form-field appearance="outline">
<mat-label>Teaching Period</mat-label>
<mat-select (valueChange)="handleChangeTeachingPeriod($event)" name="selectedTeachingPeriod" ngModel>
<mat-option value="custom"> Custom teaching period </mat-option>
<mat-option *ngFor="let tp of teachingPeriods; let i = index" [value]="tp.id">
{{ tp.name }}
</mat-option>
</mat-select>
</mat-form-field>

@if(showDates) {
<mat-form-field appearance="outline">
<mat-label>Enter a date range</mat-label>
<mat-date-range-input [rangePicker]="picker">
<input matStartDate placeholder="Start date" name="customStardDate" [(ngModel)]="startDate" />
<input matEndDate placeholder="End date" name="customEndDate" [(ngModel)]="endDate" />
</mat-date-range-input>
<mat-hint>DD/MM/YYYY - DD/MM/YYYY</mat-hint>
<mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
<mat-date-range-picker #picker></mat-date-range-picker>
</mat-form-field>
}

<mat-dialog-actions align="end">
<button mat-raised-button color="primary">Create</button>
</mat-dialog-actions>
</div>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Component, OnInit } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';
import { TeachingPeriod } from 'src/app/api/models/teaching-period';
import { TeachingPeriodService } from 'src/app/api/services/teaching-period.service';
import { UnitService } from 'src/app/api/services/unit.service';
import { AlertService } from 'src/app/common/services/alert.service';
@Component({
selector: 'create-new-unit-modal-content',
templateUrl: 'create-new-unit-modal-content.component.html',
})
export class CreateNewUnitModalContentComponent implements OnInit {
constructor(
private dialogRef: MatDialogRef<CreateNewUnitModalContentComponent>,
private unitService: UnitService,
private teachingPeriodsService: TeachingPeriodService,
private alerts: AlertService,
) {}
showDates = false;
startDate: Date;
endDate: Date;
selectedTeachingPeriod: number = null;
teachingPeriods: TeachingPeriod[];

ngOnInit(): void {
this.teachingPeriodsService.fetchAll().subscribe((teachingPeriods) => {
this.teachingPeriods = teachingPeriods;
});
}

public createUnit(unit: { unitName: string; unitCode: string; selectedTeachingPeriod: number }): void {
let newUnit;

if (this.selectedTeachingPeriod === null) {
newUnit = {
code: unit.unitCode,
name: unit.unitName,
start_date: this.startDate,
end_date: this.endDate,
};
} else {
newUnit = {
code: unit.unitCode,
name: unit.unitName,
teaching_period_id: this.selectedTeachingPeriod,
};
}

this.unitService
.create({
unit: newUnit,
})
.subscribe({
next: (unit) => {
this.alerts.success(`Unit ${unit.code} - ${unit.name} has been created.`);
this.dialogRef.close(unit);
},
error: (error) => {
this.alerts.error(`Unit Creation Failed: ${error}`);
},
});
}
public handleChangeTeachingPeriod(teachingPeriod: number | string): void {
if (typeof teachingPeriod === 'string') {
teachingPeriod = null;
this.showDates = true;
} else {
this.showDates = false;
this.selectedTeachingPeriod = teachingPeriod;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Component } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { CreateNewUnitModalContentComponent } from './create-new-unit-modal-content.component';

@Component({
selector: 'create-new-unit-modal',
template: '',
})
export class CreateNewUnitModal {
constructor(public dialog: MatDialog) {}
public show(): void {
this.dialog.open(CreateNewUnitModalContentComponent, {
width: '500px',
});
}
}
41 changes: 14 additions & 27 deletions src/app/admin/states/f-units/f-units.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ <h3>Units</h3>
<td mat-cell *matCellDef="let element">
@if (element.teachingPeriod) {

{{ element.teachingPeriod.name }}

} @else {
Custom
}
{{ element.teachingPeriod.name }}

</td>
} @else { Custom }
</td>
</ng-container>

<!-- Start Date Column -->
Expand All @@ -73,28 +70,18 @@ <h3>Units</h3>
<th mat-header-cell *matHeaderCellDef mat-sort-header>Active</th>
<td mat-cell *matCellDef="let element">
<!-- If element.teachingPeriod exists -->
@if (element.teachingPeriod) {

@if (element.teachingPeriod.active && element.active) {
<i class="fa fa-check"></i>
}
@if (!element.teachingPeriod.active || !element.active) {
<i class="fa fa-times"></i>
}

} @else {

@if (element.active) {
<i class="fa fa-check"></i>
}
@if (!element.active) {
<i class="fa fa-times"></i>
}

}
@if (element.teachingPeriod) { @if (element.teachingPeriod.active && element.active) {
<i class="fa fa-check"></i>
} @if (!element.teachingPeriod.active || !element.active) {
<i class="fa fa-times"></i>
} } @else { @if (element.active) {
<i class="fa fa-check"></i>
} @if (!element.active) {
<i class="fa fa-times"></i>
} }

<!-- If element.teachingPeriod does not exist -->
</td>
</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
Expand All @@ -106,7 +93,7 @@ <h3>Units</h3>
></tr>
</table>
<span class="flex items-center">
<mat-paginator class="mat-elevation-z0" [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
<mat-paginator class="mat-elevation-z0" [pageSizeOptions]="[10, 25, 100]"></mat-paginator>
<span class="flex-grow"></span>
<button mat-raised-button color="primary" (click)="createUnit()">
<mat-icon aria-hidden="false" aria-label="Example home icon" class="icon_display">add</mat-icon> Create Unit
Expand Down
10 changes: 5 additions & 5 deletions src/app/admin/states/f-units/f-units.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Component, Inject, AfterViewInit, ViewChild, OnDestroy } from '@angular/core';
import { createUnitModal } from 'src/app/ajs-upgraded-providers';
import { Component, AfterViewInit, ViewChild, OnDestroy } from '@angular/core';
import { Unit } from 'src/app/api/models/unit';
import { UnitRole } from 'src/app/api/models/unit-role';
import { UnitService } from 'src/app/api/services/unit.service';
import { MatTable, MatTableDataSource } from '@angular/material/table';
import { MatSort, Sort } from '@angular/material/sort';
import { MatPaginator } from '@angular/material/paginator';
import { Subscription } from 'rxjs';
import { CreateNewUnitModal } from '../../modals/create-new-unit-modal/create-new-unit-modal.component';

@Component({
selector: 'f-units',
Expand Down Expand Up @@ -37,7 +37,7 @@ export class FUnitsComponent implements AfterViewInit, OnDestroy {
private subscriptions: Subscription[] = [];

constructor(
@Inject(createUnitModal) private createUnitModal: any,
private createUnitDialog: CreateNewUnitModal,
private unitService: UnitService,
) {
this.dataload = false;
Expand All @@ -47,7 +47,7 @@ export class FUnitsComponent implements AfterViewInit, OnDestroy {
this.dataSource = new MatTableDataSource(this.unitService.cache.currentValuesClone());
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.dataSource.filterPredicate = (data: any, filter: string) => data.matches(filter);
this.dataSource.filterPredicate = (data, filter: string) => data.matches(filter);

this.subscriptions.push(
this.unitService.cache.values.subscribe((units) => {
Expand All @@ -61,7 +61,7 @@ export class FUnitsComponent implements AfterViewInit, OnDestroy {
}

createUnit() {
this.createUnitModal.show(this.dataSource);
this.createUnitDialog.show();
}

applyFilter(event: Event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class TeachingPeriodUnitImportDialogComponent implements OnInit {
private userService: UserService,
private unitService: UnitService,
private globalStateService: GlobalStateService,
@Inject(MAT_DIALOG_DATA) public data: TeachingPeriodUnitImportData
@Inject(MAT_DIALOG_DATA) public data: TeachingPeriodUnitImportData,
) {}

ngOnInit(): void {
Expand All @@ -99,7 +99,7 @@ export class TeachingPeriodUnitImportDialogComponent implements OnInit {
private _filter(name: string): User[] {
const filterValue = name.toLowerCase();

return this.teachingStaff.filter(option => option.name.toLowerCase().includes(filterValue));
return this.teachingStaff.filter((option) => option.name.toLowerCase().includes(filterValue));
}

private loadAllUnits() {
Expand Down Expand Up @@ -184,11 +184,11 @@ export class TeachingPeriodUnitImportDialogComponent implements OnInit {
convenorFormControl: formControl,
filteredStaff: formControl.valueChanges.pipe(
startWith(''),
map(value => {
map((value) => {
const name = typeof value === 'string' ? value : value?.name;
return name ? this._filter(name as string) : this.teachingStaff;
})
)
}),
),
});
}

Expand Down Expand Up @@ -230,24 +230,26 @@ export class TeachingPeriodUnitImportDialogComponent implements OnInit {
}

private createNewUnit(unitToImport: UnitImportData, idx: number) {
this.unitService.create({
unit: {
code: unitToImport.unitCode,
name: unitToImport.unitName,
main_convenor_user_id: unitToImport.convenor?.id,
teaching_period_id: this.data.teachingPeriod.id,
}
}).subscribe({
next: (newUnit: Unit) => {
unitToImport.done = true;
this.importUnit(idx + 1);
},
error: (failure) => {
unitToImport.done = false;
console.log(failure);
this.importUnit(idx + 1);
}
});
this.unitService
.create({
unit: {
code: unitToImport.unitCode,
name: unitToImport.unitName,
main_convenor_user_id: unitToImport.convenor?.id,
teaching_period_id: this.data.teachingPeriod.id,
},
})
.subscribe({
next: (newUnit: Unit) => {
unitToImport.done = true;
this.importUnit(idx + 1);
},
error: (failure) => {
unitToImport.done = false;
console.log(failure);
this.importUnit(idx + 1);
},
});
}

private importUnit(idx: number) {
Expand All @@ -257,7 +259,7 @@ export class TeachingPeriodUnitImportDialogComponent implements OnInit {

const code = unitToImport.sourceUnit ? unitToImport.sourceUnit.code : unitToImport.unitCode;

if (unitToImport.done !== undefined || this.teachigPeriod.hasUnitWithCode(code)){
if (unitToImport.done !== undefined || this.teachigPeriod.hasUnitWithCode(code)) {
// Skip units already done
this.importUnit(idx + 1);
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/app/admin/states/units/units.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ angular.module('doubtfire.admin.states.units', [])
roleWhitelist: ['Admin', 'Convenor']
$stateProvider.state "admin/units", unitsAdminViewStateData
)
.controller("AdministerUnitsState", ($scope, $state, $modal, DoubtfireConstants, CreateUnitModal, alertService, GlobalStateService, newUnitService) ->
.controller("AdministerUnitsState", ($scope, $state, $modal, DoubtfireConstants, CreateUnitModal, alertService, GlobalStateService, newUnitService, CreateNewUnitModal) ->
GlobalStateService.setView("OTHER")
$scope.dataLoaded = false

Expand Down Expand Up @@ -64,4 +64,7 @@ angular.module('doubtfire.admin.states.units', [])

$scope.createUnit = ->
CreateUnitModal.show $scope.units

$scope.openCreateModal = ->
CreateNewUnitModal.show $scope.units
)
Loading

0 comments on commit 14ffb9b

Please sign in to comment.