Skip to content

Commit

Permalink
fix: call abstract Notification methods with fallback
Browse files Browse the repository at this point in the history
Only call Notification methods if they are defined, falling back to `undefined` if not through the use of the optional chaining operator.
  • Loading branch information
davwheat authored and luceos committed May 13, 2022
1 parent 57d3c9a commit 81cf604
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions framework/core/js/src/forum/components/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ export interface INotificationAttrs extends ComponentAttrs {
notification: NotificationModel;
}

// TODO [Flarum 2.0]: Remove `?.` from abstract function calls.

/**
* The `Notification` component abstract displays a single notification.
* Subclasses should implement the `icon`, `href`, and `content` methods.
*/
export default abstract class Notification<CustomAttrs extends INotificationAttrs = INotificationAttrs> extends Component<CustomAttrs> {
view(vnode: Mithril.Vnode<CustomAttrs, this>) {
const notification = this.attrs.notification;
const href = this.href();
const href = this.href?.() ?? '';

const fromUser = notification.fromUser();

Expand All @@ -32,9 +34,9 @@ export default abstract class Notification<CustomAttrs extends INotificationAttr
onclick={this.markAsRead.bind(this)}
>
{avatar(fromUser || null)}
{icon(this.icon(), { className: 'Notification-icon' })}
{icon(this.icon?.(), { className: 'Notification-icon' })}
<span className="Notification-title">
<span className="Notification-content">{this.content()}</span>
<span className="Notification-content">{this.content?.()}</span>
<span className="Notification-title-spring" />
{humanTime(notification.createdAt())}
</span>
Expand All @@ -51,7 +53,7 @@ export default abstract class Notification<CustomAttrs extends INotificationAttr
}}
/>
)}
<div className="Notification-excerpt">{this.excerpt()}</div>
<div className="Notification-excerpt">{this.excerpt?.()}</div>
</Link>
);
}
Expand Down

0 comments on commit 81cf604

Please sign in to comment.