Add clock time-format settings and honor them in the clock
Clicking the clock opens a settings dialog; the clock renders the time with a smaller day period when the format shows one.
This commit is contained in:
1 parent
08a0ae078f
commit
f7b174017a
4 files changed
+227
-7
No files matched your search
@@ -1,21 +1,37 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { action } from '@ember/object';
|
||||
import { on } from '@ember/modifier';
|
||||
import { service } from '@ember/service';
|
||||
import Greeting from './greeting.gjs';
|
||||
import ClockForm from './settings/clock-form.gjs';
|
||||
import { timeParts } from '../utils/time-format';
|
||||
|
||||
export default class DefaultClockComponent extends Component {
|
||||
@service clock;
|
||||
@service modal;
|
||||
|
||||
get formattedTime() {
|
||||
const hours = this.clock.time.getHours().toString().padStart(2, '0');
|
||||
const minutes = this.clock.time.getMinutes().toString().padStart(2, '0');
|
||||
return `${hours}:${minutes}`;
|
||||
get parts() {
|
||||
return timeParts(this.clock.time, this.clock.timeFormat);
|
||||
}
|
||||
|
||||
@action
|
||||
openSettings() {
|
||||
this.modal.open({ component: ClockForm, size: 'sm' });
|
||||
}
|
||||
|
||||
<template>
|
||||
<div class="text-center text-white text-shadow-lg">
|
||||
<div class="text-[6rem] font-semibold">
|
||||
{{this.formattedTime}}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
{{on "click" this.openSettings}}
|
||||
aria-label="Edit clock settings"
|
||||
class="cursor-pointer rounded-lg focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white/70"
|
||||
>
|
||||
<span class="text-[6rem] font-semibold">{{this.parts.time}}</span>
|
||||
{{#if this.parts.period}}
|
||||
<span class="ml-2 text-2xl font-semibold">{{this.parts.period}}</span>
|
||||
{{/if}}
|
||||
</button>
|
||||
<Greeting />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import { action } from '@ember/object';
|
||||
import { service } from '@ember/service';
|
||||
import { on } from '@ember/modifier';
|
||||
import SettingsSelect from './integrations/select.gjs';
|
||||
import { TIME_FORMATS } from '../../utils/time-format';
|
||||
|
||||
// Clock settings: choose how times are displayed across the dashboard. The
|
||||
// calendar widget reads the same preference.
|
||||
export default class SettingsClockForm extends Component {
|
||||
@service clock;
|
||||
|
||||
@tracked formatDraft = null;
|
||||
|
||||
get format() {
|
||||
return this.formatDraft ?? this.clock.timeFormat;
|
||||
}
|
||||
|
||||
get options() {
|
||||
return TIME_FORMATS;
|
||||
}
|
||||
|
||||
@action
|
||||
updateFormat(value) {
|
||||
this.formatDraft = value;
|
||||
}
|
||||
|
||||
@action
|
||||
async handleSubmit(event) {
|
||||
event.preventDefault();
|
||||
await this.clock.setTimeFormat(this.format);
|
||||
this.args.close?.();
|
||||
}
|
||||
|
||||
<template>
|
||||
<div class="p-6">
|
||||
<h3
|
||||
id="modal-title"
|
||||
class="text-base font-semibold leading-6 text-gray-900"
|
||||
>Clock</h3>
|
||||
<div class="mt-2">
|
||||
<p class="text-sm text-gray-500">
|
||||
Choose how times are displayed across the dashboard.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<form {{on "submit" this.handleSubmit}} class="mt-5">
|
||||
<SettingsSelect
|
||||
@id="clock-time-format"
|
||||
@name="time-format"
|
||||
@label="Time format"
|
||||
@options={{this.options}}
|
||||
@value={{this.format}}
|
||||
@onChange={{this.updateFormat}}
|
||||
/>
|
||||
|
||||
<div class="mt-5 sm:mt-6 flex gap-3">
|
||||
<button
|
||||
type="submit"
|
||||
class="inline-flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500"
|
||||
>
|
||||
Save
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
{{on "click" @close}}
|
||||
class="inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'dashtab/tests/helpers';
|
||||
import { render, click } from '@ember/test-helpers';
|
||||
import DefaultClock from 'dashtab/components/default-clock.gjs';
|
||||
import ClockForm from 'dashtab/components/settings/clock-form';
|
||||
import { timeParts } from 'dashtab/utils/time-format';
|
||||
|
||||
const DATE = new Date(2026, 7, 3, 13, 5);
|
||||
|
||||
function registerServices(owner, overrides = {}) {
|
||||
const clock = overrides.clock ?? { time: DATE, timeFormat: '24h' };
|
||||
const modal = overrides.modal ?? {
|
||||
opened: null,
|
||||
open(descriptor) {
|
||||
this.opened = descriptor;
|
||||
},
|
||||
};
|
||||
const storage = overrides.storage ?? { get: async () => null };
|
||||
|
||||
owner.register('service:clock', clock, { instantiate: false });
|
||||
owner.register('service:modal', modal, { instantiate: false });
|
||||
owner.register('service:storage', storage, { instantiate: false });
|
||||
|
||||
return { clock, modal, storage };
|
||||
}
|
||||
|
||||
module('Integration | Component | default-clock', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test('renders the time in the configured 24-hour format', async function (assert) {
|
||||
registerServices(this.owner, { clock: { time: DATE, timeFormat: '24h' } });
|
||||
|
||||
await render(<template><DefaultClock /></template>);
|
||||
|
||||
const { time, period } = timeParts(DATE, '24h');
|
||||
|
||||
assert.strictEqual(period, '');
|
||||
assert.dom('[aria-label="Edit clock settings"]').containsText(time);
|
||||
});
|
||||
|
||||
test('renders the day period smaller in 12-hour format', async function (assert) {
|
||||
registerServices(this.owner, { clock: { time: DATE, timeFormat: '12h' } });
|
||||
|
||||
await render(<template><DefaultClock /></template>);
|
||||
|
||||
const { period } = timeParts(DATE, '12h');
|
||||
|
||||
assert.ok(period.length > 0);
|
||||
assert.dom('[aria-label="Edit clock settings"] .text-2xl').hasText(period);
|
||||
});
|
||||
|
||||
test('clicking the clock opens the settings dialog', async function (assert) {
|
||||
const { modal } = registerServices(this.owner);
|
||||
|
||||
await render(<template><DefaultClock /></template>);
|
||||
await click('[aria-label="Edit clock settings"]');
|
||||
|
||||
assert.strictEqual(modal.opened.component, ClockForm);
|
||||
assert.strictEqual(modal.opened.size, 'sm');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'dashtab/tests/helpers';
|
||||
import { render, triggerEvent } from '@ember/test-helpers';
|
||||
import ClockForm from 'dashtab/components/settings/clock-form';
|
||||
|
||||
function chooseOption(value) {
|
||||
const select = document.querySelector('el-select');
|
||||
const event = new Event('change', { bubbles: true });
|
||||
Object.defineProperty(event, 'target', { value: { value } });
|
||||
select.dispatchEvent(event);
|
||||
}
|
||||
|
||||
module('Integration | Component | settings/clock-form', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test('shows the current format and saves the chosen one', async function (assert) {
|
||||
const calls = [];
|
||||
let closed = false;
|
||||
|
||||
this.owner.register(
|
||||
'service:clock',
|
||||
{
|
||||
timeFormat: '24h',
|
||||
async setTimeFormat(format) {
|
||||
calls.push(format);
|
||||
},
|
||||
},
|
||||
{ instantiate: false }
|
||||
);
|
||||
|
||||
const closeDialog = () => {
|
||||
closed = true;
|
||||
};
|
||||
|
||||
await render(<template><ClockForm @close={{closeDialog}} /></template>);
|
||||
|
||||
assert.dom('el-selectedcontent').hasText('24-hour');
|
||||
|
||||
chooseOption('12h');
|
||||
await triggerEvent('form', 'submit');
|
||||
|
||||
assert.deepEqual(calls, ['12h']);
|
||||
assert.true(closed);
|
||||
});
|
||||
|
||||
test('saving the current format is a no-op choice', async function (assert) {
|
||||
const calls = [];
|
||||
|
||||
this.owner.register(
|
||||
'service:clock',
|
||||
{
|
||||
timeFormat: 'locale',
|
||||
async setTimeFormat(format) {
|
||||
calls.push(format);
|
||||
},
|
||||
},
|
||||
{ instantiate: false }
|
||||
);
|
||||
|
||||
const noop = () => {};
|
||||
|
||||
await render(<template><ClockForm @close={{noop}} /></template>);
|
||||
await triggerEvent('form', 'submit');
|
||||
|
||||
assert.deepEqual(calls, ['locale']);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user