Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
refactor(attendances): move actions to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabauke committed Dec 21, 2022
1 parent 2ffa8e2 commit 4665c97
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 110 deletions.
102 changes: 84 additions & 18 deletions app/index/attendances/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* @public
*/
import Controller from "@ember/controller";
import { computed } from "@ember/object";
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import AttendanceValidator from "timed/validations/attendance";

/**
Expand All @@ -14,37 +15,102 @@ import AttendanceValidator from "timed/validations/attendance";
* @extends Ember.Controller
* @public
*/
export default Controller.extend({
AttendanceValidator,
export default class AttendanceController extends Controller {
@service notify;
@service tracking;

AttendacenValidator = AttendanceValidator;

/**
* All attendances currently in the store
*
* @property {Attendance[]} _allAttendances
* @private
*/
_allAttendances: computed("store", function () {
get _allAttendances() {
return this.store.peekAll("attendance");
}),
}

/**
* The attendances filtered by the selected day
*
* @property {Attendance[]} attendances
* @public
*/
attendances: computed(
"_allAttendances.@each.{date,isDeleted,user}",
"model",
"user.id",
function () {
return this._allAttendances.filter((a) => {
return (
a.get("date").isSame(this.model, "day") &&
a.get("user.id") === this.get("user.id") &&
!a.get("isDeleted")
);
get attendances() {
return this._allAttendances.filter((a) => {
return (
a.get("date").isSame(this.model, "day") &&
a.get("user.id") === this.user.id &&
!a.get("isDeleted")
);
});
}

/**
* Save an attendance
*
* @method saveAttendance
* @param {Changeset} attendance The attendance to save
* @public
*/
@action
async saveAttendance(attendance) {
try {
await attendance.save();

this.notify.success("Attendance was saved");
} catch (e) {
/* istanbul ignore next */
this.notify.error("Error while saving the attendance");
}
}

/**
* Delete an attendance
*
* @method deleteAttendance
* @param {Attendance} attendance The attendance to delete
* @public
*/
@action
async deleteAttendance(attendance) {
try {
await this.store.peekRecord("attendance", attendance.id).destroyRecord();

this.notify.success("Attendance was deleted");
} catch (e) {
/* istanbul ignore next */
this.notify.error("Error while deleting the attendance");
}
}

/**
* Add a new attendance
*
* @method addAttendance
* @public
*/
@action
async addAttendance() {
try {
const date = this.tracking.date.clone();

const from = date.clone().set({ h: 8, m: 0, s: 0, ms: 0 });
const to = date.clone().set({ h: 11, m: 30, s: 0, ms: 0 });

const attendance = this.store.createRecord("attendance", {
date,
from,
to,
});

await attendance.save();

this.notify.success("Attendance was added");
} catch (e) {
/* istanbul ignore next */
this.notify.error("Error while adding the attendance");
}
),
});
}
}
93 changes: 6 additions & 87 deletions app/index/attendances/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* @public
*/
import Route from "@ember/routing/route";
import { inject as service } from "@ember/service";
import RouteAutostartTourMixin from "timed/mixins/route-autostart-tour";

/**
Expand All @@ -14,15 +13,9 @@ import RouteAutostartTourMixin from "timed/mixins/route-autostart-tour";
* @extends Ember.Route
* @public
*/
export default Route.extend(RouteAutostartTourMixin, {
/**
* The notify service
*
* @property {EmberNotify.NotifyService} notify
* @public
*/
notify: service("notify"),

export default class AttendaceIndexRoute extends Route.extend(
RouteAutostartTourMixin
) {
/**
* Setup controller hook, set the current user
*
Expand All @@ -31,82 +24,8 @@ export default Route.extend(RouteAutostartTourMixin, {
* @public
*/
setupController(controller, ...args) {
this._super(controller, ...args);
super.setupController(controller, ...args);

controller.set("user", this.modelFor("protected"));
},

/**
* The actions for the index attendance route
*
* @property {Object} actions
* @public
*/
actions: {
/**
* Save an attendance
*
* @method saveAttendance
* @param {Changeset} attendance The attendance to save
* @public
*/
async saveAttendance(attendance) {
try {
await attendance.save();

this.notify.success("Attendance was saved");
} catch (e) {
/* istanbul ignore next */
this.notify.error("Error while saving the attendance");
}
},

/**
* Delete an attendance
*
* @method deleteAttendance
* @param {Attendance} attendance The attendance to delete
* @public
*/
async deleteAttendance(attendance) {
try {
await this.store
.peekRecord("attendance", attendance.get("id"))
.destroyRecord();

this.notify.success("Attendance was deleted");
} catch (e) {
/* istanbul ignore next */
this.notify.error("Error while deleting the attendance");
}
},

/**
* Add a new attendance
*
* @method addAttendance
* @public
*/
async addAttendance() {
try {
const date = this.controllerFor("index").get("date").clone();

const from = date.clone().set({ h: 8, m: 0, s: 0, ms: 0 });
const to = date.clone().set({ h: 11, m: 30, s: 0, ms: 0 });

const attendance = this.store.createRecord("attendance", {
date,
from,
to,
});

await attendance.save();

this.notify.success("Attendance was added");
} catch (e) {
/* istanbul ignore next */
this.notify.error("Error while adding the attendance");
}
},
},
});
}
}
10 changes: 5 additions & 5 deletions app/index/attendances/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
type="button"
class="btn btn-success"
data-test-add-attendance
{{on "click" (route-action "addAttendance")}}
{{on "click" this.addAttendance}}
>Add attendance</button>
</div>
<div class="attendances">
Expand All @@ -14,8 +14,8 @@
data-test-attendance-slider
data-test-attendance-slider-id={{attendance.id}}
@attendance={{(changeset attendance this.AttendanceValidator)}}
@onSave={{(route-action "saveAttendance")}}
@onDelete={{(route-action "deleteAttendance")}}
@onSave={{this.saveAttendance}}
@onDelete={{this.deleteAttendance}}
/>
{{/each}}
{{else}}
Expand Down Expand Up @@ -43,13 +43,13 @@
<button
type="button"
class="btn btn-danger"
{{on "click" (route-action "deleteAttendance" model)}}
{{on "click" (fn this.deleteAttendance model)}}
><FaIcon @icon="trash" /></button>
<button
type="submit"
class="btn btn-primary"
disabled={{not (and model.isDirty model.isValid)}}
{{on "click" (route-action "saveAttendance" model)}}
{{on "click" (fn this.saveAttendance model)}}
><FaIcon @icon="save" /></button>
</td>
</tr>
Expand Down
2 changes: 2 additions & 0 deletions app/index/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ export default class IndexController extends Controller {

set date(value) {
this.day = value.format("YYYY-MM-DD");
// share the newly selected date
this.tracking.date = value;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions app/services/tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@ export default class TrackingService extends Service {
* This is used to prevent doubled time accumulation in task sum displays.
*/
@tracked generatingReports = false;
@tracked _date;

constructor(...args) {
super(...args);

this.fetchActiveActivity.perform();
}

get date() {
return this._date ?? moment();
}

set date(date) {
this._date = date;
}

/**
* Init hook, get the current activity
*
Expand Down

0 comments on commit 4665c97

Please sign in to comment.