Skip to content

Commit

Permalink
Fix dates not properly saving on model create
Browse files Browse the repository at this point in the history
  • Loading branch information
fromHearts committed Jun 17, 2022
1 parent d60327b commit 6bb2330
Show file tree
Hide file tree
Showing 6 changed files with 16,585 additions and 44,122 deletions.
3 changes: 1 addition & 2 deletions app/controllers/admin/events/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export default Controller.extend(saveModelMixin, {
noticeDuringSave: "Updating event...",
noticeAfterSave: "Event updated!",
modelName: "model",
transitionAfterSuccess: "events.show",
transitionAfterSuccess: "admin.events.edit",
imagePath: "images/events"
});

3 changes: 1 addition & 2 deletions app/controllers/admin/events/new.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import EditController from "roboteam-website/controllers/admin/events/edit";

export default EditController.extend({
export default EditController.extend({
noticeDuringSave: "Adding event...",
noticeAfterSave: "Event added!",
});

53 changes: 31 additions & 22 deletions app/mixins/save-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,40 @@ export default Mixin.create({
},

_save(callback) {
const flashNotice = this.get('flashNotice');

// add time to the model. easy for sorting
const timestamp = new Date();
if (this.get(this.get('modelName')).get('isNew')) {
this.get(this.get('modelName')).set('createdAt', timestamp);
}
this.get(this.get('modelName')).set('updatedAt', timestamp);

this.get(this.get('modelName')).save().then(() => {
this.get(this.get('modelName')).save().then(function() {
if (callback !== undefined) {
callback();
} else {
flashNotice.sendSuccess(this.get('noticeAfterSave'));

if (this.get('transitionToIndexRoute')) {
this.transitionToRoute(this.get('transitionAfterSuccess'));
} else {
this.transitionToRoute(this.get('transitionAfterSuccess'), this.get(this.get('modelName')).get('id'));
}
}
});
},

_done() {
const flashNotice = this.get('flashNotice');

flashNotice.sendSuccess(this.get('noticeAfterSave'));

if (this.get('transitionToIndexRoute')) {
this.transitionToRoute(this.get('transitionAfterSuccess'));
} else {
this.transitionToRoute(this.get('transitionAfterSuccess'), this.get(this.get('modelName')).get('id'));
}
},

actions: {
saveModel: function() {
this.get('flashNotice').sendInfo(this.get('noticeDuringSave'));
// check the required variables and save if it is okay
if (this.validateModel()) {
this._save();
this._save(function() {
this._done();
});
}
},

Expand All @@ -88,29 +92,34 @@ export default Mixin.create({
// check the required variables
// the model should be okay, and there should be a file or an existing imageSrc
if (this.validateModel() && (this.get('file'))) {
this._save(() => {
let self = this;

self._save(function() {
self.get(self.get('modelName')).reload();
const metadata = { contentType: 'image/png' };

// get reference to firebase storage
this.get('firebaseApp').storage().then(storage => {
self.get('firebaseApp').storage().then(storage => {
const storageRef = storage.ref();
const path = this.get('imagePath') + this.get(this.get('modelName')).get('id') + '.png';
storageRef.child(path).put(this.get('file'), metadata).then(async snapshot => {
const path = self.get('imagePath') + self.get(self.get('modelName')).get('id') + '.png';
storageRef.child(path).put(self.get('file'), metadata).then(async snapshot => {
// give the model the imageSrc when finished
const downloadURL = await snapshot.ref.getDownloadURL();
this.get(this.get('modelName')).set('imageSrc', downloadURL);
this._save();
this.set('file', null);
}).catch(() => {
self.get(self.get('modelName')).set('imageSrc', downloadURL);
self._save(function() {
self.set('file', null);
self._done();
});
});
}).catch(() => {
});
});

// else if there was already an image and it didnt change
// then we just save immediately
} else if (this.validateModel() && this.get('model.imageSrc')) {
this._save();
this._save(function() {
this._done();
});
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/models/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ export default Model.extend({
updatedAt: attr('date'),

namedId: computed('title', 'startdate', function() {
if (!this.get('title')) return '';
if (!this.get('title') || !this.get('startdate')) return 'null';
const trimmed = this.get('title').trim();
const suffix = this.get('startdate').getDate() + "-" + (this.get('startdate').getMonth() + 1) + "-" + (this.get('startdate').getFullYear() - 2000);
return trimmed.toLowerCase().replaceAll(' ', '_') + "_" + suffix;
}),

isUpcoming: computed('enddate', function() {
if (!this.get('enddate')) return true;
return this.get('enddate') >= new Date(Date.now() - 86400 * 1000);
})
});
Loading

0 comments on commit 6bb2330

Please sign in to comment.