Skip to content

Commit

Permalink
Merge pull request #50 from gravity-ui/add-full-reset
Browse files Browse the repository at this point in the history
feat: add resetToDefaultState method
  • Loading branch information
vanilla-wave authored Feb 21, 2024
2 parents 31c2346 + 22db0b0 commit 79290f4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ type Listener = () => void;

let instanceCounter = 0;

const getDefaultBaseState = (): BaseState => ({
availablePresets: [],
activePresets: [],
suggestedPresets: [],
wizardState: 'hidden' as const,
});

const getDefaultProgressState = () => ({
presetPassedSteps: {},
finishedPresets: [],
});

export class Controller<HintParams, Presets extends string, Steps extends string> {
static findNextUnpassedStep(presetSteps: string[], passedSteps: string[]): string | undefined {
if (!presetSteps) {
Expand Down Expand Up @@ -61,15 +73,9 @@ export class Controller<HintParams, Presets extends string, Steps extends string
) {
this.options = options;

const defaultBaseState: BaseState = {
availablePresets: [],
activePresets: [],
suggestedPresets: [],
wizardState: 'hidden' as const,
};
this.state = {
base: {
...defaultBaseState,
...getDefaultBaseState(),
...options.baseState,
},
};
Expand Down Expand Up @@ -459,14 +465,10 @@ export class Controller<HintParams, Presets extends string, Steps extends string

this.logger.debug('Loading onboarding progress data');
try {
const defaultProgress = {
presetPassedSteps: {},
finishedPresets: [],
};
const newProgressState = await this.progressLoadingPromise;

this.state.progress = {
...defaultProgress,
...getDefaultProgressState(),
...newProgressState,
};
this.status = 'active';
Expand All @@ -478,6 +480,16 @@ export class Controller<HintParams, Presets extends string, Steps extends string
}
}

async resetToDefaultState() {
this.state = {
base: getDefaultBaseState(),
progress: getDefaultProgressState(),
};

await this.updateBaseState();
await this.updateProgress();
}

private closeHint = (stepSlug?: Steps) => {
const currentHintStep = this.hintStore.state.hint?.step.slug;
this.logger.debug('Close hint(internal)', currentHintStep);
Expand Down
22 changes: 22 additions & 0 deletions src/tests/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,25 @@ describe('store api', function () {
});
});
});

it('resetToDefaultState -> hidden and empty ', async function () {
const options = getOptions();

const controller = new Controller(options);
await controller.ensureRunning();

await controller.resetToDefaultState();

expect(controller.state).toEqual({
base: {
activePresets: [],
availablePresets: [],
suggestedPresets: [],
wizardState: 'hidden',
},
progress: {
finishedPresets: [],
presetPassedSteps: {},
},
});
});

0 comments on commit 79290f4

Please sign in to comment.