Add name onboarding and inline name editing
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.
This commit is contained in:
1 parent
6578c2d3f3
commit
13e02168c9
13 files changed
+543
-24
No files matched your search
+102
-16
@@ -1,21 +1,20 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { service } from '@ember/service';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import { action } from '@ember/object';
|
||||
import { on } from '@ember/modifier';
|
||||
import { service } from '@ember/service';
|
||||
import autofocus from '../modifiers/autofocus';
|
||||
|
||||
// Greets the user by the name stored in the `user` service. The name is
|
||||
// clickable and edits in place; saving and cancelling are idempotent so the
|
||||
// blur that follows either path is a no-op.
|
||||
export default class GreetingComponent extends Component {
|
||||
@service clock;
|
||||
@service storage;
|
||||
@service user;
|
||||
|
||||
@tracked name = null;
|
||||
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.loadName();
|
||||
}
|
||||
|
||||
async loadName() {
|
||||
this.name = (await this.storage.get('preferences', 'name')) ?? 'Râu';
|
||||
}
|
||||
@tracked isEditing = false;
|
||||
@tracked draft = '';
|
||||
@tracked refocusName = false;
|
||||
|
||||
get greeting() {
|
||||
const hour = this.clock.time.getHours();
|
||||
@@ -24,10 +23,97 @@ export default class GreetingComponent extends Component {
|
||||
return 'Good evening';
|
||||
}
|
||||
|
||||
@action
|
||||
startEditing() {
|
||||
this.refocusName = false;
|
||||
this.draft = this.user.name ?? '';
|
||||
this.isEditing = true;
|
||||
}
|
||||
|
||||
@action
|
||||
updateDraft(event) {
|
||||
this.draft = event.target.value;
|
||||
}
|
||||
|
||||
@action
|
||||
handleKeyDown(event) {
|
||||
if (event.key === 'Escape') {
|
||||
event.preventDefault();
|
||||
this.cancel({ refocus: true });
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
handleBlur() {
|
||||
this.save();
|
||||
}
|
||||
|
||||
@action
|
||||
async handleSubmit(event) {
|
||||
event.preventDefault();
|
||||
await this.save();
|
||||
}
|
||||
|
||||
async save() {
|
||||
if (!this.isEditing) return;
|
||||
|
||||
const name = (this.draft ?? '').trim();
|
||||
|
||||
if (!name) {
|
||||
// Blurring an empty field reverts rather than clearing the name.
|
||||
this.cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
// Do not refocus the name after saving; otherwise the focus-visible ring
|
||||
// stays drawn around it once the input is replaced by the button.
|
||||
this.refocusName = false;
|
||||
this.isEditing = false;
|
||||
await this.user.setName(name);
|
||||
}
|
||||
|
||||
cancel({ refocus = false } = {}) {
|
||||
if (!this.isEditing) return;
|
||||
|
||||
this.refocusName = refocus;
|
||||
this.isEditing = false;
|
||||
}
|
||||
|
||||
<template>
|
||||
<div class="text-[2rem]">
|
||||
{{this.greeting}},
|
||||
<span class="name">{{this.name}}</span>.
|
||||
</div>
|
||||
{{#if this.user.name}}
|
||||
<div class="text-[2rem]">
|
||||
{{this.greeting}},
|
||||
{{#if this.isEditing}}
|
||||
<form class="inline" {{on "submit" this.handleSubmit}}>
|
||||
<label class="sr-only" for="greeting-name-input">Your name</label>
|
||||
<input
|
||||
id="greeting-name-input"
|
||||
name="name"
|
||||
type="text"
|
||||
required
|
||||
size="8"
|
||||
class="name border-0 border-b border-white/70 bg-transparent px-1 text-[2rem] text-white focus:border-white focus:outline-none"
|
||||
value={{this.draft}}
|
||||
{{on "input" this.updateDraft}}
|
||||
{{on "keydown" this.handleKeyDown}}
|
||||
{{on "blur" this.handleBlur}}
|
||||
{{autofocus select=true}}
|
||||
/>
|
||||
</form>
|
||||
{{else}}
|
||||
<button
|
||||
type="button"
|
||||
class="name cursor-pointer align-baseline no-underline underline-offset-4 transition-colors hover:underline hover:decoration-dotted hover:decoration-white/60 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white/70"
|
||||
aria-label="Edit name"
|
||||
title="Edit name"
|
||||
data-test-greeting-name
|
||||
{{on "click" this.startEditing}}
|
||||
{{autofocus when=this.refocusName}}
|
||||
>
|
||||
{{~this.user.name~}}
|
||||
</button>
|
||||
{{~/if}}.
|
||||
</div>
|
||||
{{/if}}
|
||||
</template>
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { action } from '@ember/object';
|
||||
import { on } from '@ember/modifier';
|
||||
import { service } from '@ember/service';
|
||||
import autofocus from '../modifiers/autofocus';
|
||||
|
||||
// First-run onboarding step: ask the user what to call them. Kept presentational
|
||||
// so later steps can be sequenced from the route/service without touching this
|
||||
// form.
|
||||
export default class WelcomeComponent extends Component {
|
||||
@service user;
|
||||
@service router;
|
||||
|
||||
@action
|
||||
handleSubmit(event) {
|
||||
event.preventDefault();
|
||||
|
||||
const value = String(new FormData(event.target).get('name') ?? '').trim();
|
||||
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
|
||||
// setName updates the tracked name synchronously, so the index guard passes
|
||||
// immediately; the disk write finishes in the background.
|
||||
this.user.setName(value);
|
||||
this.router.replaceWith('index');
|
||||
}
|
||||
|
||||
<template>
|
||||
<div
|
||||
class="col-span-3 row-span-3 flex flex-col items-center justify-center px-6 text-center text-white text-shadow-lg"
|
||||
data-test-welcome
|
||||
>
|
||||
<form class="w-full max-w-xl" {{on "submit" this.handleSubmit}}>
|
||||
<h1 class="text-3xl font-semibold text-shadow-none sm:text-4xl">
|
||||
Hello! What's your name?
|
||||
</h1>
|
||||
|
||||
<label class="sr-only" for="welcome-name">Your name</label>
|
||||
<input
|
||||
id="welcome-name"
|
||||
name="name"
|
||||
type="text"
|
||||
required
|
||||
autocomplete="given-name"
|
||||
placeholder="Your name"
|
||||
data-test-welcome-input
|
||||
class="mt-8 w-full border-0 border-b-2 border-white/70 bg-transparent px-1 pb-2 text-center text-3xl text-white placeholder-white/50 focus:border-white focus:outline-none"
|
||||
{{autofocus}}
|
||||
/>
|
||||
|
||||
<p class="mt-4 text-sm text-white/80">
|
||||
Please enter your first name or the name you prefer to be called.
|
||||
</p>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
data-test-welcome-submit
|
||||
class="mt-8 inline-flex cursor-pointer items-center gap-2 rounded-full border border-white/70 px-6 py-2 text-sm font-semibold text-white transition-colors hover:bg-white/10 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white/70"
|
||||
>
|
||||
Continue
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { modifier } from 'ember-modifier';
|
||||
|
||||
// Focuses an element when it enters the DOM. Pass `select=true` to also select
|
||||
// its current text, and `when=false` to skip focusing (e.g. focus should return
|
||||
// to a re-rendered element only after some interactions).
|
||||
export default modifier(
|
||||
(element, _positional, { select = false, when = true } = {}) => {
|
||||
if (!when) {
|
||||
return;
|
||||
}
|
||||
|
||||
element.focus();
|
||||
|
||||
if (select && typeof element.select === 'function') {
|
||||
element.select();
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -10,6 +10,8 @@ export default class Router extends EmberRouter {
|
||||
}
|
||||
|
||||
Router.map(function () {
|
||||
this.route('welcome');
|
||||
|
||||
this.route('auth', function () {
|
||||
this.route('discourse', function () {
|
||||
this.route('callback');
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import Route from '@ember/routing/route';
|
||||
import { service } from '@ember/service';
|
||||
|
||||
// The dashboard is only reachable once a name is known. When none is stored we
|
||||
// send the user to the welcome flow instead.
|
||||
export default class IndexRoute extends Route {
|
||||
@service user;
|
||||
@service router;
|
||||
|
||||
async beforeModel() {
|
||||
await this.user.ready;
|
||||
|
||||
if (!this.user.hasName) {
|
||||
this.router.replaceWith('welcome');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import Route from '@ember/routing/route';
|
||||
import { service } from '@ember/service';
|
||||
|
||||
// Onboarding entry point. Once a name exists there is nothing to do here, so
|
||||
// we bounce back to the dashboard.
|
||||
export default class WelcomeRoute extends Route {
|
||||
@service user;
|
||||
@service router;
|
||||
|
||||
async beforeModel() {
|
||||
await this.user.ready;
|
||||
|
||||
if (this.user.hasName) {
|
||||
this.router.replaceWith('index');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { pageTitle } from 'ember-page-title';
|
||||
import Welcome from '../components/welcome.gjs';
|
||||
|
||||
<template>
|
||||
{{pageTitle "Welcome"}}
|
||||
<Welcome />
|
||||
</template>
|
||||
@@ -0,0 +1,46 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupApplicationTest } from 'dashtab/tests/helpers';
|
||||
import { visit, fillIn, click } from '@ember/test-helpers';
|
||||
|
||||
module('Acceptance | name onboarding', function (hooks) {
|
||||
setupApplicationTest(hooks);
|
||||
|
||||
hooks.beforeEach(async function () {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.remove('preferences', 'name');
|
||||
// Keep the dashboard widgets from picking up leftover integrations (and
|
||||
// reaching the network) while these tests run.
|
||||
await storage.clear('integrations');
|
||||
});
|
||||
|
||||
test('redirects to the welcome flow when no name is stored', async function (assert) {
|
||||
await visit('/');
|
||||
|
||||
const router = this.owner.lookup('service:router');
|
||||
assert.strictEqual(router.currentRouteName, 'welcome');
|
||||
assert.dom('[data-test-welcome]').exists();
|
||||
});
|
||||
|
||||
test('shows the dashboard when a name is stored', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('preferences', 'name', 'Ada');
|
||||
|
||||
await visit('/');
|
||||
|
||||
const router = this.owner.lookup('service:router');
|
||||
assert.strictEqual(router.currentRouteName, 'index');
|
||||
assert.dom('[data-test-welcome]').doesNotExist();
|
||||
assert.dom('.name').hasText('Ada');
|
||||
});
|
||||
|
||||
test('submitting the welcome form stores the name and enters the dashboard', async function (assert) {
|
||||
await visit('/');
|
||||
await fillIn('[data-test-welcome-input]', 'Ada');
|
||||
await click('[data-test-welcome-submit]');
|
||||
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
const router = this.owner.lookup('service:router');
|
||||
assert.strictEqual(await storage.get('preferences', 'name'), 'Ada');
|
||||
assert.strictEqual(router.currentRouteName, 'index');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
|
||||
// Test double for the `user` service. `name` is tracked so templates that read
|
||||
// it re-render exactly as they do against the real service.
|
||||
export default class FakeUser {
|
||||
@tracked name;
|
||||
calls = [];
|
||||
|
||||
constructor(name = null) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
get hasName() {
|
||||
return Boolean(this.name);
|
||||
}
|
||||
|
||||
async setName(value) {
|
||||
this.calls.push(value);
|
||||
this.name = value;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,93 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'dashtab/tests/helpers';
|
||||
import { render } from '@ember/test-helpers';
|
||||
import {
|
||||
render,
|
||||
click,
|
||||
fillIn,
|
||||
blur,
|
||||
triggerEvent,
|
||||
triggerKeyEvent,
|
||||
} from '@ember/test-helpers';
|
||||
import Greeting from 'dashtab/components/greeting';
|
||||
import FakeUser from 'dashtab/tests/helpers/fake-user';
|
||||
|
||||
function registerServices(owner, { name = 'Ada' } = {}) {
|
||||
const clock = { time: new Date(2026, 7, 3, 13, 5) };
|
||||
const user = new FakeUser(name);
|
||||
|
||||
owner.register('service:clock', clock, { instantiate: false });
|
||||
owner.register('service:user', user, { instantiate: false });
|
||||
|
||||
return { user };
|
||||
}
|
||||
|
||||
module('Integration | Component | greeting', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test('it renders', async function (assert) {
|
||||
// Updating values is achieved using autotracking, just like in app code. For example:
|
||||
// class State { @tracked myProperty = 0; }; const state = new State();
|
||||
// and update using state.myProperty = 1; await rerender();
|
||||
// Handle any actions with function myAction(val) { ... };
|
||||
test('it renders the greeting and the name', async function (assert) {
|
||||
registerServices(this.owner, { name: 'Ada' });
|
||||
|
||||
await render(<template><Greeting /></template>);
|
||||
|
||||
assert.dom().hasText(/Good (morning|afternoon|evening),/);
|
||||
assert.dom('.name').exists();
|
||||
assert.dom().hasText('Good afternoon, Ada.');
|
||||
assert.dom('.name').hasText('Ada');
|
||||
});
|
||||
|
||||
test('clicking the name reveals a focused, selected input', async function (assert) {
|
||||
registerServices(this.owner, { name: 'Ada' });
|
||||
|
||||
await render(<template><Greeting /></template>);
|
||||
await click('[data-test-greeting-name]');
|
||||
|
||||
assert.dom('[data-test-greeting-name]').doesNotExist();
|
||||
assert.dom('input.name').isFocused().hasValue('Ada');
|
||||
});
|
||||
|
||||
test('submitting the edit saves the trimmed name', async function (assert) {
|
||||
const { user } = registerServices(this.owner, { name: 'Ada' });
|
||||
|
||||
await render(<template><Greeting /></template>);
|
||||
await click('[data-test-greeting-name]');
|
||||
await fillIn('input.name', ' Grace ');
|
||||
await triggerEvent('form', 'submit');
|
||||
|
||||
assert.deepEqual(user.calls, ['Grace']);
|
||||
assert.dom('[data-test-greeting-name]').hasText('Grace').isNotFocused();
|
||||
});
|
||||
|
||||
test('blurring the edit saves the name', async function (assert) {
|
||||
const { user } = registerServices(this.owner, { name: 'Ada' });
|
||||
|
||||
await render(<template><Greeting /></template>);
|
||||
await click('[data-test-greeting-name]');
|
||||
await fillIn('input.name', 'Grace');
|
||||
await blur('input.name');
|
||||
|
||||
assert.deepEqual(user.calls, ['Grace']);
|
||||
});
|
||||
|
||||
test('pressing Escape cancels without saving', async function (assert) {
|
||||
const { user } = registerServices(this.owner, { name: 'Ada' });
|
||||
|
||||
await render(<template><Greeting /></template>);
|
||||
await click('[data-test-greeting-name]');
|
||||
await fillIn('input.name', 'Grace');
|
||||
await triggerKeyEvent('input.name', 'keydown', 'Escape');
|
||||
|
||||
assert.deepEqual(user.calls, []);
|
||||
assert.dom('[data-test-greeting-name]').hasText('Ada').isFocused();
|
||||
});
|
||||
|
||||
test('blurring an empty field reverts instead of clearing the name', async function (assert) {
|
||||
const { user } = registerServices(this.owner, { name: 'Ada' });
|
||||
|
||||
await render(<template><Greeting /></template>);
|
||||
await click('[data-test-greeting-name]');
|
||||
await fillIn('input.name', ' ');
|
||||
await blur('input.name');
|
||||
|
||||
assert.deepEqual(user.calls, []);
|
||||
assert.strictEqual(user.name, 'Ada');
|
||||
assert.dom('[data-test-greeting-name]').hasText('Ada');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'dashtab/tests/helpers';
|
||||
import { render, fillIn, click } from '@ember/test-helpers';
|
||||
import Welcome from 'dashtab/components/welcome';
|
||||
import FakeUser from 'dashtab/tests/helpers/fake-user';
|
||||
|
||||
function registerServices(owner) {
|
||||
const router = {
|
||||
replacedWith: null,
|
||||
replaceWith(route) {
|
||||
this.replacedWith = route;
|
||||
},
|
||||
};
|
||||
const user = new FakeUser();
|
||||
|
||||
owner.register('service:router', router, { instantiate: false });
|
||||
owner.register('service:user', user, { instantiate: false });
|
||||
|
||||
return { router, user };
|
||||
}
|
||||
|
||||
module('Integration | Component | welcome', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test('it renders the name prompt and focuses the field', async function (assert) {
|
||||
registerServices(this.owner);
|
||||
|
||||
await render(<template><Welcome /></template>);
|
||||
|
||||
assert.dom('[data-test-welcome]').exists();
|
||||
assert.dom('h1').hasText("Hello! What's your name?");
|
||||
assert.dom('[data-test-welcome-input]').isFocused();
|
||||
});
|
||||
|
||||
test('submitting stores the trimmed name and enters the dashboard', async function (assert) {
|
||||
const { router, user } = registerServices(this.owner);
|
||||
|
||||
await render(<template><Welcome /></template>);
|
||||
await fillIn('[data-test-welcome-input]', ' Ada ');
|
||||
await click('[data-test-welcome-submit]');
|
||||
|
||||
assert.deepEqual(user.calls, ['Ada']);
|
||||
assert.strictEqual(user.name, 'Ada');
|
||||
assert.strictEqual(router.replacedWith, 'index');
|
||||
});
|
||||
|
||||
test('does not submit an empty name', async function (assert) {
|
||||
const { router, user } = registerServices(this.owner);
|
||||
|
||||
await render(<template><Welcome /></template>);
|
||||
await click('[data-test-welcome-submit]');
|
||||
|
||||
assert.deepEqual(user.calls, []);
|
||||
assert.strictEqual(router.replacedWith, null);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'dashtab/tests/helpers';
|
||||
|
||||
module('Unit | Service | user', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
hooks.beforeEach(async function () {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.remove('preferences', 'name');
|
||||
});
|
||||
|
||||
test('it exists', function (assert) {
|
||||
const service = this.owner.lookup('service:user');
|
||||
assert.ok(service);
|
||||
});
|
||||
|
||||
test('defaults to no name', async function (assert) {
|
||||
const service = this.owner.lookup('service:user');
|
||||
await service.ready;
|
||||
|
||||
assert.strictEqual(service.name, null);
|
||||
assert.false(service.hasName);
|
||||
});
|
||||
|
||||
test('loads a stored name, trimmed', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('preferences', 'name', ' Ada ');
|
||||
|
||||
const service = this.owner.lookup('service:user');
|
||||
await service.ready;
|
||||
|
||||
assert.strictEqual(service.name, 'Ada');
|
||||
assert.true(service.hasName);
|
||||
});
|
||||
|
||||
test('setName updates the state and persists it', async function (assert) {
|
||||
const service = this.owner.lookup('service:user');
|
||||
await service.ready;
|
||||
|
||||
await service.setName(' Grace ');
|
||||
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
assert.strictEqual(service.name, 'Grace');
|
||||
assert.true(service.hasName);
|
||||
assert.strictEqual(await storage.get('preferences', 'name'), 'Grace');
|
||||
});
|
||||
|
||||
test('setName with only whitespace clears the name', async function (assert) {
|
||||
const service = this.owner.lookup('service:user');
|
||||
await service.ready;
|
||||
await service.setName('Ada');
|
||||
|
||||
await service.setName(' ');
|
||||
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
assert.strictEqual(service.name, null);
|
||||
assert.false(service.hasName);
|
||||
assert.strictEqual(await storage.get('preferences', 'name'), null);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user