Skip to content

Instantly share code, notes, and snippets.

@pavel-agarkov
Last active July 4, 2018 17:28
Show Gist options
  • Save pavel-agarkov/67e4b48e7c89f746cbbd8840d371fcb6 to your computer and use it in GitHub Desktop.
Save pavel-agarkov/67e4b48e7c89f746cbbd8840d371fcb6 to your computer and use it in GitHub Desktop.
Example of ngrx actions generation
export interface CommandErrorPayload {
readonly errorMessage: string;
readonly originalError?: any;
}
export class StandardAction<TActionType extends string, TActionPayload> {
type: TActionType;
constructor(readonly payload: TActionPayload) { }
}
export interface ActionType<TActionType extends string, TActionPayload> extends Function {
prototype: StandardAction<TActionType, TActionPayload>;
new(payload: TActionPayload): StandardAction<TActionType, TActionPayload>;
}
export function createCommandActions<
TCommandName extends string, TSuccessName extends string, TFailureName extends string,
TCommandPayload, TSuccessPayload = TCommandPayload, TFailurePayload = TCommandPayload & CommandErrorPayload>
(options: {
commandName: TCommandName,
successName: TSuccessName,
failureName: TFailureName,
commandPayload: TCommandPayload,
successPayload?: TSuccessPayload,
failurePayload?: TFailurePayload
}) {
return [
class {
type = options.commandName;
constructor(readonly payload: TCommandPayload) { }
},
class {
type = options.successName;
constructor(readonly payload: TSuccessPayload) { }
},
class {
type = options.failureName;
constructor(readonly payload: TFailurePayload) { }
}
] as [
ActionType<TCommandName, TCommandPayload>,
ActionType<TSuccessName, TSuccessPayload>,
ActionType<TFailureName, TFailurePayload>
];
}
import { createCommandActions } from './command-actions-generator';
export const [LikePost, PostLiked, LikePostFailed] = createCommandActions({
commandName: 'LikePost',
successName: 'PostLiked',
failureName: 'LikePostFailed',
commandPayload: new PostIdPayload(),
successPayload: new PostLikesPayload()
});
export const [DislikePost, PostDisliked, DislikePostFailed] = createCommandActions({
commandName: 'DislikePost',
successName: 'PostDisliked',
failureName: 'DislikePostFailed',
commandPayload: new PostIdPayload(),
successPayload: new PostLikesPayload()
});
export const [AddPostToFavorites, PostAddedToFavorites, AddPostToFavoritesFailed] = createCommandActions({
commandName: 'AddPostToFavorites',
successName: 'PostAddedToFavorites',
failureName: 'AddPostToFavoritesFailed',
commandPayload: new PostIdPayload()
});
export const [RemovePostFromFavorites, PostRemovedFromFavorites, RemovePostFromFavoritesFailed] = createCommandActions({
commandName: 'RemovePostFromFavorites',
successName: 'PostRemovedFromFavorites',
failureName: 'RemovePostFromFavoritesFailed',
commandPayload: new PostIdPayload()
});
export class PostIdPayload {
readonly id: string;
}
export class PostLikesPayload {
readonly id: string;
readonly likes: number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment