import { Component } from "@angular/core";
import { action } from "@storybook/addon-actions";
import { Meta, moduleMetadata, Story } from "@storybook/angular";
import { delay, of } from "rxjs";
import { LogService } from "@bitwarden/common/abstractions/log.service";
import { ValidationService } from "@bitwarden/common/abstractions/validation.service";
import { ButtonModule } from "../button";
import { IconButtonModule } from "../icon-button";
import { BitActionDirective } from "./bit-action.directive";
const template = `
`;
@Component({
template,
selector: "app-promise-example",
})
class PromiseExampleComponent {
action = async () => {
await new Promise((resolve, reject) => {
setTimeout(resolve, 2000);
});
};
}
@Component({
template,
selector: "app-observable-example",
})
class ObservableExampleComponent {
action = () => {
return of("fake observable").pipe(delay(2000));
};
}
@Component({
template,
selector: "app-rejected-promise-example",
})
class RejectedPromiseExampleComponent {
action = async () => {
await new Promise((resolve, reject) => {
setTimeout(() => reject(new Error("Simulated error")), 2000);
});
};
}
export default {
title: "Component Library/Async Actions/Standalone",
decorators: [
moduleMetadata({
declarations: [
BitActionDirective,
PromiseExampleComponent,
ObservableExampleComponent,
RejectedPromiseExampleComponent,
],
imports: [ButtonModule, IconButtonModule],
providers: [
{
provide: ValidationService,
useValue: {
showError: action("ValidationService.showError"),
} as Partial,
},
{
provide: LogService,
useValue: {
error: action("LogService.error"),
} as Partial,
},
],
}),
],
} as Meta;
const PromiseTemplate: Story = (args: PromiseExampleComponent) => ({
props: args,
template: ``,
});
export const UsingPromise = PromiseTemplate.bind({});
const ObservableTemplate: Story = (
args: ObservableExampleComponent
) => ({
template: ``,
});
export const UsingObservable = ObservableTemplate.bind({});
const RejectedPromiseTemplate: Story = (
args: ObservableExampleComponent
) => ({
template: ``,
});
export const RejectedPromise = RejectedPromiseTemplate.bind({});