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

[chore] refactor app structure #42

Merged
merged 1 commit into from May 29, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 8 additions & 15 deletions client/src/client/app/+scheduler/components/calendar/calendar.ts
@@ -1,19 +1,12 @@
import { ChangeDetectionStrategy } from '@angular/core';
import { Input, OnChanges, ElementRef } from '@angular/core';
import {
BaseComponent,
Shift,
Employee,
ShiftTemplate,
ShiftService,
EmployeeService,
ShiftTemplateService
} from '../../../shared/index';
import { BaseComponent, Model } from '../../../shared/index';
import { Headers } from './headers/index';
import { Month } from './month/index';
import { Week } from './week/index';
import * as moment from 'moment';
import { Observable } from 'rxjs/Observable';
import * as Service from '../../../shared/services/index';

@BaseComponent({
selector: 'calendar',
Expand All @@ -26,16 +19,16 @@ import { Observable } from 'rxjs/Observable';
export class Calendar implements OnChanges {
@Input() currentDate: moment.Moment;
@Input() calendarMode: Number;
shiftTemplates: Observable<ShiftTemplate[]>;
shifts: Observable<Shift[]>;
shiftTemplates: Observable<Model.Admin.ShiftTemplate[]>;
shifts: Observable<Model.Admin.Shift[]>;
fetching: boolean = true;
employees: Observable<Employee[]>;
employees: Observable<Model.Admin.Employee[]>;
width: number;

constructor(
private shiftService: ShiftService,
private shiftTemplateService: ShiftTemplateService,
private employeeService: EmployeeService,
private shiftService: Service.Admin.Shift,
private shiftTemplateService: Service.Admin.ShiftTemplate,
private employeeService: Service.Admin.Employee,
private ref: ElementRef
) {
this.employees = this.employeeService.employees;
Expand Down
@@ -1,11 +1,6 @@
import { Input } from '@angular/core';
import { COMMON_DIRECTIVES } from '@angular/common';
import {
BaseComponent,
Shift,
ShiftTemplate,
Employee
} from '../../../../shared/index';
import { BaseComponent, Model } from '../../../../shared/index';
import { IDay } from '../../../interfaces/index';
import { ShiftComponent } from '../shift/index';
import * as _ from 'lodash';
Expand All @@ -19,14 +14,14 @@ import * as moment from 'moment';
})

export class DailyShifts {
@Input() day:IDay;
@Input() shiftTemplates: Array<ShiftTemplate> = [];
@Input() employees: Array<Employee>;
@Input() showAll:boolean = false;
@Input() availabeEmployees: Array<Employee> = [];
@Input() day: IDay;
@Input() shiftTemplates: Model.Admin.ShiftTemplate[] = [];
@Input() employees: Model.Admin.Employee[];
@Input() showAll: boolean = false;
@Input() availabeEmployees: Model.Admin.Employee[] = [];

public showLimit:Number = 6;
private shifts:Array<Shift> = [];
public showLimit: Number = 6;
private shifts: Model.Admin.Shift[] = [];

ngOnInit() {
this.init();
Expand All @@ -50,8 +45,8 @@ export class DailyShifts {
this.onEmployeeAssigned();
}

private mergeShiftWithShiftTemplate(): Array<Shift> {
let results = <Shift[]>[];
private mergeShiftWithShiftTemplate(): Array<Model.Admin.Shift> {
let results = <Model.Admin.Shift[]>[];
let tmpShifts = _.clone(this.shifts);
this.shiftTemplates = _.sortBy(this.shiftTemplates, 'sort');
_.forEach(this.shiftTemplates, (template) => {
Expand All @@ -66,8 +61,8 @@ export class DailyShifts {
return results;
}

private generateShiftFromTemplate(template: ShiftTemplate, today: moment.Moment): Shift {
let shift = new Shift({});
private generateShiftFromTemplate(template: Model.Admin.ShiftTemplate, today: moment.Moment): Model.Admin.Shift {
let shift = new Model.Admin.Shift({});
shift.name = template.name;
shift.shiftTemplateId = template.id;
shift.startTime = moment(today.format('YYYY-MM-DDT') + template.startTime.format('HH:mm'), 'YYYY-MM-DDTHH:mm');
Expand Down
@@ -1,7 +1,7 @@
import { Input, OnChanges } from '@angular/core';
import { COMMON_DIRECTIVES } from '@angular/common';
import * as moment from 'moment';
import { BaseComponent, Shift, ShiftTemplate, Employee } from '../../../../shared/index';
import { BaseComponent, Model } from '../../../../shared/index';
import { IMonth, IWeek } from '../../../interfaces/index';
import { Week } from '../week/index';

Expand All @@ -13,10 +13,10 @@ import { Week } from '../week/index';
})

export class Month implements OnChanges {
@Input() shifts: Array<Shift>;
@Input() shiftTemplates: Array<ShiftTemplate>;
@Input() shifts: Model.Admin.Shift[];
@Input() shiftTemplates: Model.Admin.ShiftTemplate[];
@Input() currentDate: moment.Moment;
@Input() employees: Array<Employee>;
@Input() employees: Model.Admin.Employee[];
month: IMonth;
weeks: Array<IWeek>;

Expand Down
@@ -1,8 +1,10 @@
import { BaseComponent, Shift, ShiftService } from '../../../../shared/index';
import { BaseComponent, Model } from '../../../../shared/index';
import { DialogRef, ModalComponent } from 'angular2-modal/angular2-modal';
import { BSModalContext } from 'angular2-modal/plugins/bootstrap/index';
import { ControlGroup, FormBuilder, Validators, FORM_PROVIDERS, FORM_DIRECTIVES } from '@angular/common';
import { TimepickerComponent } from 'ng2-bootstrap/ng2-bootstrap';
import * as Service from '../../../../shared/services/index';

import * as moment from 'moment';

export class ShiftModalWindowData extends BSModalContext {
Expand All @@ -22,7 +24,7 @@ export class ShiftModalWindowData extends BSModalContext {
export class ShiftModalWindow implements ModalComponent<ShiftModalWindowData> {
public context: ShiftModalWindowData;
public shiftForm: ControlGroup;
public newShift: Shift = new Shift({});
public newShift: Model.Admin.Shift = new Model.Admin.Shift({});
public hstep:number = 1;
public mstep:number = 15;
public startTime: moment.Moment = moment().startOf('hour');
Expand All @@ -31,7 +33,7 @@ export class ShiftModalWindow implements ModalComponent<ShiftModalWindowData> {
public wrongAnswer: boolean;

constructor(private dialog: DialogRef<ShiftModalWindowData>,
private shiftService: ShiftService,
private shiftService: Service.Admin.Shift,
builder: FormBuilder) {
this.context = dialog.context;
this.wrongAnswer = true;
Expand All @@ -56,7 +58,7 @@ export class ShiftModalWindow implements ModalComponent<ShiftModalWindowData> {
this.dialog.close();
}

onSubmit(shift: Shift): boolean {
onSubmit(shift: Model.Admin.Shift): boolean {
this.newShift.startTime = moment(this.startTime);
this.newShift.endTime = moment(this.endTime);
this.shiftService.create(this.newShift);
Expand Down
33 changes: 16 additions & 17 deletions client/src/client/app/+scheduler/components/calendar/shift/shift.ts
Expand Up @@ -3,15 +3,14 @@ import { COMMON_DIRECTIVES } from '@angular/common';
import { DROPDOWN_DIRECTIVES} from 'ng2-bootstrap/ng2-bootstrap';
import {
BaseComponent,
ShiftService,
Employee,
Operator,
Shift
Model,
Operator
} from '../../../../shared/index';
import * as _ from 'lodash';
const toastr = require('toastr');
import { Action, Dispatcher } from '@ngrx/store';
import { ShiftAction } from '../../../../shared/index';
import * as Actions from '../../../../shared/actions/index';
import * as Service from '../../../../shared/services/index';

@BaseComponent({
selector: 'shift',
Expand All @@ -21,41 +20,41 @@ import { ShiftAction } from '../../../../shared/index';
})

export class ShiftComponent {
@Input() shift: Shift;
@Input() shifts: Array<Shift>;
@Input() employees: Array<Employee>;
@Input() shift: Model.Admin.Shift;
@Input() shifts: Model.Admin.Shift[];
@Input() employees: Model.Admin.Employee[];
@Input() canEdit: boolean;
@Output() onEmployeeAssigned: EventEmitter<any> = new EventEmitter<any>();
addSub:any = null;
updateSub:any = null;
editingSub:any = null;
editing: boolean = false;

constructor(public shiftService: ShiftService, private dispatcher: Dispatcher<Action>) {}
constructor(private shiftService: Service.Admin.Shift, private dispatcher: Dispatcher<Action>) {}

edit() {
if (this.canEdit) {
this.editing = true;
this.addSub = this.dispatcher
.filter(({type}: Action) => type === ShiftAction.CREATED)
.filter(({type}: Action) => type === Actions.Admin.Shift.CREATED)
.subscribe(({payload}: Action) => this.onAdded(payload));

this.updateSub = this.dispatcher
.filter(({type}: Action) => type === ShiftAction.UPDATED)
.filter(({type}: Action) => type === Actions.Admin.Shift.UPDATED)
.subscribe(({payload}: Action) => this.onAdded(payload));

this.editingSub = this.dispatcher
.filter(({type}: Action) => type === ShiftAction.EDITING)
.filter(({type}: Action) => type === Actions.Admin.Shift.EDITING)
.subscribe(({payload}: Action) => this.onEditing(payload));

this.shiftService.editing(this.shift);
}
}

select(employee: Employee) {
select(employee: Model.Admin.Employee) {
if (this.alreadyAssigned(employee)) return;
if (this.shift.id) {
let tmp = new Shift({id: this.shift.id, employeeId: employee.id});
let tmp = new Model.Admin.Shift({id: this.shift.id, employeeId: employee.id});
this.shiftService.update(tmp);
} else {
let shift = _.clone(this.shift);
Expand All @@ -78,22 +77,22 @@ export class ShiftComponent {
if (this.editingSub) this.editingSub.unsubscribe();
}

private onEditing(shift: Shift) {
private onEditing(shift: Model.Admin.Shift) {
if (!_.isEqual(shift, this.shift)) {
this.editing = false;
this.cleanUp();
}
}

private onAdded(shift: Shift) {
private onAdded(shift: Model.Admin.Shift) {
this.shift = shift;
this.editing = false;
Operator.update(this.shifts, shift);
this.onEmployeeAssigned.emit({});
this.cleanUp();
}

private alreadyAssigned(employee: Employee): boolean {
private alreadyAssigned(employee: Model.Admin.Employee): boolean {
let shift = _.find(this.shifts, {employeeId : employee.id});
if (shift) {
toastr.error(`${employee.firstName} 已經排在 ${shift.name}`);
Expand Down
@@ -1,8 +1,7 @@
import { Input } from '@angular/core';
import {
BaseComponent,
ShiftTemplate,
Employee
Model
} from '../../../../shared/index';
import { IDay } from '../../../interfaces/index';
import { DailyShifts } from '../daily-shifts/index';
Expand All @@ -17,7 +16,7 @@ import { WeekDayHeader } from '../week-day-header/index';

export class WeekDay {
@Input() day: IDay;
@Input() shiftTemplates: Array<ShiftTemplate>;
@Input() employees: Array<Employee>;
@Input() showAll:boolean = false;
@Input() shiftTemplates: Model.Admin.ShiftTemplate[];
@Input() employees: Model.Admin.Employee[];
@Input() showAll: boolean = false;
}
@@ -1,25 +1,25 @@
import { Injectable } from '@angular/core';
import { IDay, IWeek } from '../../../../interfaces/index';
import { Shift } from '../../../../../shared/index';
import { Model } from '../../../../../shared/index';
import * as moment from 'moment';

@Injectable()
export class WeekHelper {
newWeek(currentDate: moment.Moment): IWeek {
return { date: currentDate, days: <IDay[]>[], shifts: <Shift[]>[] };
return { date: currentDate, days: <IDay[]>[], shifts: <Model.Admin.Shift[]>[] };
}

getWeekWithShifts(week: IWeek, shifts: Shift[]): IWeek {
getWeekWithShifts(week: IWeek, shifts: Model.Admin.Shift[]): IWeek {
week.shifts = this.getWeeklyShifts(week.date, shifts);
let days = this.initDays(week.date);
week.days = this.getDaysWithShifts(days, week.shifts);
return week;
}

getWeeklyShifts(currentDate: moment.Moment, shifts: Shift[]): Shift[] {
getWeeklyShifts(currentDate: moment.Moment, shifts: Model.Admin.Shift[]): Model.Admin.Shift[] {
let weekStart = currentDate.clone().startOf('week');
let weekEnd = currentDate.clone().endOf('week');
let weeklyShifts = _.filter(shifts, (shift: Shift) => {
let weeklyShifts = _.filter(shifts, (shift: Model.Admin.Shift) => {
return weekStart <= shift.startTime && shift.endTime <= weekEnd;
});
return weeklyShifts;
Expand All @@ -36,7 +36,7 @@ export class WeekHelper {
return result;
}

getDaysWithShifts(days: IDay[], shifts: Shift[]): IDay[] {
getDaysWithShifts(days: IDay[], shifts: Model.Admin.Shift[]): IDay[] {
let groups = _.groupBy(shifts, (shift) => {
return moment(shift.startTime).startOf('day').format('YYYYMMDD');
});
Expand Down
23 changes: 9 additions & 14 deletions client/src/client/app/+scheduler/components/calendar/week/week.ts
@@ -1,16 +1,11 @@
import { Input, OnChanges } from '@angular/core';
import * as moment from 'moment';
import {
BaseComponent,
Shift,
ShiftTemplate,
Employee
} from '../../../../shared/index';
import { BaseComponent, Model } from '../../../../shared/index';
import { IWeek } from '../../../interfaces/index';
import { WeekDay } from '../week-day/index';
import { WeeklyHoursCalculator } from '../../../services/index';
import { Action, Dispatcher } from '@ngrx/store';
import { ShiftAction } from '../../../../shared/index';
import * as Actions from '../../../../shared/actions/index';
import { WeekHelper } from './services/index';

@BaseComponent({
Expand All @@ -24,11 +19,11 @@ import { WeekHelper } from './services/index';

export class Week implements OnChanges {
@Input() week: IWeek;
@Input() shifts: Array<Shift>;
@Input() shifts: Array<Model.Admin.Shift>;
@Input() currentDate: moment.Moment;
@Input() showAll: boolean;
@Input() shiftTemplates: Array<ShiftTemplate>;
@Input() employees: Array<Employee>;
@Input() shiftTemplates: Array<Model.Admin.ShiftTemplate>;
@Input() employees: Array<Model.Admin.Employee>;
@Input() width: number;
dailyWidth: number;
private addSub:any = null;
Expand All @@ -39,11 +34,11 @@ export class Week implements OnChanges {
private dispatcher: Dispatcher<Action>,
private weekHelper: WeekHelper) {
this.addSub = dispatcher
.filter(({type}: Action) => type === ShiftAction.CREATED)
.filter(({type}: Action) => type === Actions.Admin.Shift.CREATED)
.subscribe(({payload}: Action) => this.onShiftAdded(payload));

this.updateSub = dispatcher
.filter(({type}: Action) => type === ShiftAction.UPDATED)
.filter(({type}: Action) => type === Actions.Admin.Shift.UPDATED)
.subscribe(({payload}: Action) => this.onShiftUpdated(payload));
}

Expand Down Expand Up @@ -74,11 +69,11 @@ export class Week implements OnChanges {
this.calculatorService.init(this.employees, this.week);
}

private onShiftUpdated(shift: Shift) {
private onShiftUpdated(shift: Model.Admin.Shift) {
this.calculatorService.updateShift(shift);
}

private onShiftAdded(shift: Shift) {
private onShiftAdded(shift: Model.Admin.Shift) {
this.calculatorService.addShift(shift);
}
}
4 changes: 2 additions & 2 deletions client/src/client/app/+scheduler/interfaces/day.ts
@@ -1,7 +1,7 @@
import * as moment from 'moment';
import { Shift } from '../../shared/index';
import { Model } from '../../shared/index';

export interface IDay {
date: moment.Moment;
shifts: Array<Shift>;
shifts: Array<Model.Admin.Shift>;
}