Ghost/ghost/admin/app/components/announcement-settings/visibility.js
Elena Baidakova 4207c9d0d1
Added browser tests for announcement bar (#16742)
refs TryGhost/Team#3122

<!-- Leave the line below if you'd like GitHub Copilot to generate a
summary from your commit -->
<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 87727d9</samp>

Added `data-testid` attributes to various elements in the announcement
bar settings feature to enable Playwright testing. Fixed a potential bug
with the `visibilitySettings` getter in the `visibility.js` component.
Added Playwright tests for the announcement bar settings feature in
`announcement-bar-settings.spec.js`.
2023-05-05 14:11:26 +04:00

57 lines
1.7 KiB
JavaScript

import Component from '@glimmer/component';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
export default class AnnouncementSettingsVisibilityComponent extends Component {
@service settings;
@service membersUtils;
visibilityOptions = {
freeMembers: 'free_members',
paidMembers: 'paid_members',
visitors: 'visitors'
};
get visibilitySettings() {
return this.settings.announcementVisibility || [];
}
get isPaidAvailable() {
return this.membersUtils.isStripeEnabled;
}
get isFreeMembersChecked() {
return this.visibilitySettings.includes(this.visibilityOptions.freeMembers);
}
get isPaidMembersChecked() {
return this.visibilitySettings.includes(this.visibilityOptions.paidMembers);
}
get isVisitorsChecked() {
return this.visibilitySettings.includes(this.visibilityOptions.visitors);
}
get isMembersEnabled() {
return this.settings.membersEnabled;
}
@action
updateVisibility(event) {
let updatedVisibilityOptions = [...this.visibilitySettings];
const value = event.target.value;
if (event.target.checked) {
updatedVisibilityOptions.push(value);
} else {
updatedVisibilityOptions = updatedVisibilityOptions.filter(item => item !== value);
}
this.settings.announcementVisibility = updatedVisibilityOptions;
// update preview if there are no visibility options or just one to avoid update flickering on every check
if (!updatedVisibilityOptions.length || updatedVisibilityOptions.length === 1) {
this.args.onChange?.();
}
}
}