Last active
March 21, 2018 18:26
-
-
Save kmaraz/64545e6cc6e405c1b25742e952d10782 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class EventsActions { | |
constructor( | |
private ActionTypes: ActionTypes, // List of actions we can do in our app | |
private Dispatcher: FluxDispatcher, // Dispatcher from vendor | |
private API: API, | |
private Reporter: Reporter, // New Relic or stuff | |
private Notifications: Notifications, | |
private ngDialog: Dialog | |
) { | |
'ngInject'; | |
} | |
deleteEvent(eventUuid: string) { | |
// Before the action is executed, we show to the user | |
// the confirmation dialog. Just to be safe | |
this.ngDialog.openConfirm({ | |
template: require('./dialogs/deleteEvent.html') | |
}) | |
.then(() => | |
// Promise chaining is not done very well, but this is only an example. | |
// Here we are doing the API request | |
this.API.remove(eventUuid) | |
.then(() => { | |
// After the request completion, we name our action and dispatch | |
// the data to our store | |
this.Dispatcher.dispatch({ | |
data: { eventUuid }, | |
type: this.ActionTypes.EVENTS_DELETE_EVENT | |
}); | |
})) | |
.catch((error: Error) => { | |
// In this case we do not dispatch another action witch the | |
// error messages, but this could be the place to do it. | |
this.Reporter.error(error); | |
}); | |
} | |
duplicateEvent(eventUuid: string) { | |
this.API.duplicate(eventUuid) | |
.then((response: DuplicateEventResponse) => { | |
this.Notifications.success('Event was duplicated.'); | |
this.Dispatcher.dispatch({ | |
data: response, | |
type: this.ActionTypes.EVENTS_DUPLICATE_EVENT | |
}); | |
}) | |
.catch((error: Error) => { | |
this.Reporter.error(error); | |
}); | |
} | |
fetchAll(filter: Filter) { | |
// Very similar to all other actions. | |
this.changeFilter(filter) | |
.then(() => this.API.getAllEvents()) | |
.then((response: GetAllEventsResponse) => { | |
this.Dispatcher.dispatch({ | |
data: response, | |
type: this.ActionTypes.EVENTS_RECEIVE_ALL_EVENTS | |
}); | |
}) | |
.catch((error: Error) => { | |
this.Reporter.error(error); | |
}); | |
} | |
selectEvent(eventHash: string) { | |
this.Dispatcher.dispatch({ | |
data: { eventHash }, | |
type: this.ActionTypes.EVENTS_SELECT_EVENT | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment