2024-03-27 20:37:37 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2024-03-27 21:27:43 +03:00
|
|
|
import {action} from '@ember/object';
|
2024-03-28 17:19:43 +03:00
|
|
|
|
|
|
|
const EMPTY_SETTINGS = {
|
|
|
|
completedSteps: [],
|
|
|
|
checklistState: 'pending' // pending, started, completed, dismissed
|
|
|
|
};
|
2024-03-27 20:37:37 +03:00
|
|
|
|
|
|
|
export default class OnboardingService extends Service {
|
|
|
|
@service session;
|
|
|
|
|
2024-03-27 21:27:43 +03:00
|
|
|
ONBOARDING_STEPS = [
|
|
|
|
'customize-design',
|
|
|
|
'first-post',
|
|
|
|
'build-audience',
|
|
|
|
'share-publication'
|
|
|
|
];
|
|
|
|
|
2024-03-28 17:19:43 +03:00
|
|
|
get settings() {
|
|
|
|
const userSettings = JSON.parse(this.session.user.accessibility || '{}');
|
|
|
|
|
|
|
|
return userSettings.onboarding || JSON.parse(JSON.stringify(EMPTY_SETTINGS));
|
|
|
|
}
|
|
|
|
|
2024-03-27 20:37:37 +03:00
|
|
|
get isChecklistShown() {
|
2024-06-20 13:01:35 +03:00
|
|
|
return this.session.user.isOwnerOnly
|
2024-03-28 19:10:59 +03:00
|
|
|
&& this.checklistStarted
|
2024-03-28 17:19:43 +03:00
|
|
|
&& !this.checklistCompleted
|
|
|
|
&& !this.checklistDismissed;
|
|
|
|
}
|
|
|
|
|
|
|
|
get checklistState() {
|
|
|
|
return this.settings.checklistState;
|
|
|
|
}
|
|
|
|
|
|
|
|
get checklistStarted() {
|
|
|
|
return this.settings.checklistState === 'started';
|
|
|
|
}
|
|
|
|
|
|
|
|
get checklistCompleted() {
|
|
|
|
return this.settings.checklistState === 'completed';
|
|
|
|
}
|
|
|
|
|
|
|
|
get checklistDismissed() {
|
|
|
|
return this.settings.checklistState === 'dismissed';
|
|
|
|
}
|
|
|
|
|
|
|
|
get completedSteps() {
|
|
|
|
const settings = this.settings;
|
|
|
|
|
|
|
|
return settings.completedSteps || [];
|
2024-03-27 20:37:37 +03:00
|
|
|
}
|
2024-03-27 21:27:43 +03:00
|
|
|
|
|
|
|
get nextStep() {
|
|
|
|
return this.ONBOARDING_STEPS.find(step => !this.isStepCompleted(step));
|
|
|
|
}
|
|
|
|
|
2024-03-28 17:19:43 +03:00
|
|
|
get allStepsCompleted() {
|
|
|
|
return this.ONBOARDING_STEPS.every(step => this.isStepCompleted(step));
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
async startChecklist() {
|
|
|
|
const settings = this.settings;
|
|
|
|
|
|
|
|
settings.completedSteps = [];
|
|
|
|
settings.checklistState = 'started';
|
|
|
|
|
|
|
|
await this._saveSettings(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
async completeChecklist() {
|
|
|
|
const settings = this.settings;
|
|
|
|
|
|
|
|
settings.checklistState = 'completed';
|
|
|
|
|
|
|
|
await this._saveSettings(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
async dismissChecklist() {
|
|
|
|
const settings = this.settings;
|
|
|
|
|
|
|
|
settings.checklistState = 'dismissed';
|
|
|
|
|
|
|
|
await this._saveSettings(settings);
|
|
|
|
}
|
|
|
|
|
2024-03-27 21:27:43 +03:00
|
|
|
@action
|
|
|
|
isStepCompleted(step) {
|
2024-03-28 17:19:43 +03:00
|
|
|
return this.completedSteps.includes(step);
|
2024-03-27 21:27:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2024-03-28 17:19:43 +03:00
|
|
|
async markStepCompleted(step) {
|
|
|
|
if (this.isStepCompleted(step)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const settings = this.settings;
|
|
|
|
settings.completedSteps.push(step);
|
|
|
|
|
|
|
|
await this._saveSettings(settings);
|
|
|
|
}
|
|
|
|
|
2024-03-28 19:10:59 +03:00
|
|
|
@action
|
|
|
|
async reset() {
|
|
|
|
await this._saveSettings(undefined);
|
|
|
|
}
|
|
|
|
|
2024-03-28 17:19:43 +03:00
|
|
|
/* private */
|
|
|
|
|
|
|
|
async _saveSettings(settings) {
|
|
|
|
const userSettings = JSON.parse(this.session.user.accessibility || '{}');
|
|
|
|
|
|
|
|
userSettings.onboarding = settings;
|
|
|
|
|
|
|
|
this.session.user.accessibility = JSON.stringify(userSettings);
|
|
|
|
await this.session.user.save();
|
2024-03-27 21:27:43 +03:00
|
|
|
}
|
2024-03-27 20:37:37 +03:00
|
|
|
}
|