Ask for the user's name on first launch through a /welcome route, store it in the preferences namespace via a user service, and redirect between welcome and the dashboard based on whether a name exists. Replace the hardcoded greeting name and make it editable in place, with save/cancel handling and a first-run prompt matching the dashboard style.
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
import Service, { service } from '@ember/service';
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
// Owns the name the user wants to be greeted by. `ready` resolves once the
|
|
// stored value has been read so the route guards can wait for it. Storage
|
|
// failures degrade to "no name" (which shows the welcome flow) rather than
|
|
// breaking navigation on every new tab.
|
|
export default class UserService extends Service {
|
|
@service storage;
|
|
|
|
@tracked name = null;
|
|
ready = null;
|
|
|
|
constructor() {
|
|
super(...arguments);
|
|
this.ready = this.load();
|
|
}
|
|
|
|
async load() {
|
|
try {
|
|
const stored = await this.storage.get('preferences', 'name');
|
|
this.name = stored?.trim() || null;
|
|
} catch (err) {
|
|
console.error('Error loading the name:', err);
|
|
this.name = null;
|
|
}
|
|
}
|
|
|
|
get hasName() {
|
|
return Boolean(this.name);
|
|
}
|
|
|
|
// Sets the tracked value synchronously (so navigation never waits on the
|
|
// disk write) and swallows storage errors, so callers can `await` safely.
|
|
async setName(value) {
|
|
const name = value?.trim() ?? '';
|
|
this.name = name || null;
|
|
|
|
try {
|
|
if (name) {
|
|
await this.storage.set('preferences', 'name', name);
|
|
} else {
|
|
await this.storage.remove('preferences', 'name');
|
|
}
|
|
} catch (err) {
|
|
console.error('Error saving the name:', err);
|
|
}
|
|
}
|
|
}
|