fix: load WorldStore on direct /hunt navigation
HuntPageComponent never called WorldStore.load(), so opening /hunt directly (bookmark/hard refresh) without first visiting /world left currentLocation() at null forever, stranding the page and the context panel on their empty states with no recovery. Add ngOnInit that calls worldStore.load() only when no location is present yet, mirroring WorldPageComponent's existing call and avoiding a duplicate request.
This commit is contained in:
@@ -76,6 +76,7 @@ const threeEncounterHunt: HuntResult = {
|
|||||||
describe('HuntPageComponent', () => {
|
describe('HuntPageComponent', () => {
|
||||||
let worldStore: {
|
let worldStore: {
|
||||||
currentLocation: ReturnType<typeof signal<CurrentLocationResponse | null>>;
|
currentLocation: ReturnType<typeof signal<CurrentLocationResponse | null>>;
|
||||||
|
load: ReturnType<typeof vi.fn>;
|
||||||
};
|
};
|
||||||
let huntingStore: {
|
let huntingStore: {
|
||||||
currentHunt: ReturnType<typeof signal<HuntResult | null>>;
|
currentHunt: ReturnType<typeof signal<HuntResult | null>>;
|
||||||
@@ -89,7 +90,7 @@ describe('HuntPageComponent', () => {
|
|||||||
let router: Router;
|
let router: Router;
|
||||||
|
|
||||||
async function setup(location: CurrentLocationResponse | null, hunt: HuntResult | null = null) {
|
async function setup(location: CurrentLocationResponse | null, hunt: HuntResult | null = null) {
|
||||||
worldStore = { currentLocation: signal(location) };
|
worldStore = { currentLocation: signal(location), load: vi.fn(() => Promise.resolve()) };
|
||||||
const currentHunt = signal(hunt);
|
const currentHunt = signal(hunt);
|
||||||
huntingStore = {
|
huntingStore = {
|
||||||
currentHunt,
|
currentHunt,
|
||||||
@@ -196,6 +197,18 @@ describe('HuntPageComponent', () => {
|
|||||||
expect(huntingStore.startHunt).not.toHaveBeenCalled();
|
expect(huntingStore.startHunt).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('loads the world state on init when no location has been loaded yet (direct navigation/hard refresh)', async () => {
|
||||||
|
await setup(null);
|
||||||
|
|
||||||
|
expect(worldStore.load).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not call load again when a location is already present', async () => {
|
||||||
|
await setup(burnedRoad);
|
||||||
|
|
||||||
|
expect(worldStore.load).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
it('shows a loading state and disables the triggering action', async () => {
|
it('shows a loading state and disables the triggering action', async () => {
|
||||||
const fixture = await setup(burnedRoad);
|
const fixture = await setup(burnedRoad);
|
||||||
huntingStore.loading.set(true);
|
huntingStore.loading.set(true);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Component, inject } from '@angular/core';
|
import { Component, OnInit, inject } from '@angular/core';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import { EncounterCardComponent } from '../encounter-card/encounter-card.component';
|
import { EncounterCardComponent } from '../encounter-card/encounter-card.component';
|
||||||
import { HuntingStore } from '../hunting.store';
|
import { HuntingStore } from '../hunting.store';
|
||||||
@@ -10,11 +10,17 @@ import { WorldStore } from '../../world/world.store';
|
|||||||
templateUrl: './hunt-page.component.html',
|
templateUrl: './hunt-page.component.html',
|
||||||
styleUrl: './hunt-page.component.scss',
|
styleUrl: './hunt-page.component.scss',
|
||||||
})
|
})
|
||||||
export class HuntPageComponent {
|
export class HuntPageComponent implements OnInit {
|
||||||
protected readonly worldStore = inject(WorldStore);
|
protected readonly worldStore = inject(WorldStore);
|
||||||
protected readonly huntingStore = inject(HuntingStore);
|
protected readonly huntingStore = inject(HuntingStore);
|
||||||
private readonly router = inject(Router);
|
private readonly router = inject(Router);
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
if (this.worldStore.currentLocation() === null) {
|
||||||
|
void this.worldStore.load();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected startHunt(): void {
|
protected startHunt(): void {
|
||||||
void this.huntingStore.startHunt();
|
void this.huntingStore.startHunt();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user