Introduce storage service, split up component
This commit is contained in:
1 parent
153bbeaa98
commit
541634d5f4
8 files changed
+150
-39
No files matched your search
@@ -1,54 +1,22 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import { action } from '@ember/object';
|
||||
import { registerDestructor } from '@ember/destroyable';
|
||||
import { service } from '@ember/service';
|
||||
import Greeting from './greeting.gjs';
|
||||
|
||||
export default class DefaultClockComponent extends Component {
|
||||
@tracked time = new Date();
|
||||
|
||||
constructor(owner, args) {
|
||||
super(owner, args);
|
||||
this.startClock();
|
||||
|
||||
registerDestructor(this, () => {
|
||||
if (this.intervalId) {
|
||||
clearInterval(this.intervalId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
startClock() {
|
||||
this.intervalId = setInterval(() => {
|
||||
this.time = new Date();
|
||||
}, 2000);
|
||||
}
|
||||
@service clock;
|
||||
|
||||
get formattedTime() {
|
||||
const hours = this.time.getHours();
|
||||
const minutes = this.time.getMinutes().toString().padStart(2, '0');
|
||||
const hours = this.clock.time.getHours();
|
||||
const minutes = this.clock.time.getMinutes().toString().padStart(2, '0');
|
||||
return `${hours}:${minutes}`;
|
||||
}
|
||||
|
||||
get greeting() {
|
||||
const hour = this.time.getHours();
|
||||
|
||||
if (hour < 12) {
|
||||
return "Good morning";
|
||||
} else if (hour < 18) {
|
||||
return "Good afternoon";
|
||||
} else {
|
||||
return "Good evening";
|
||||
}
|
||||
}
|
||||
|
||||
<template>
|
||||
<div class="text-center text-white text-shadow-lg">
|
||||
<div class="text-[6rem] font-semibold">
|
||||
{{this.formattedTime}}
|
||||
</div>
|
||||
<div class="text-[2rem]">
|
||||
{{this.greeting}}, <span class="name">{{@name}}</span>.
|
||||
</div>
|
||||
<Greeting />
|
||||
</div>
|
||||
</template>
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import { service } from '@ember/service';
|
||||
|
||||
export default class GreetingComponent extends Component {
|
||||
@service clock;
|
||||
@service storage;
|
||||
|
||||
@tracked name = null;
|
||||
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.loadName();
|
||||
this.storage.remove("prefs:name", "Satoshi")
|
||||
}
|
||||
|
||||
async loadName() {
|
||||
this.name = (await this.storage.get("prefs:name")) || "Râu";
|
||||
}
|
||||
|
||||
get greeting() {
|
||||
const hour = this.clock.time.getHours();
|
||||
if (hour < 12) return "Good morning";
|
||||
if (hour < 18) return "Good afternoon";
|
||||
return "Good evening";
|
||||
}
|
||||
|
||||
<template>
|
||||
<div class="text-[2rem]">
|
||||
{{this.greeting}}, <span class="name">{{this.name}}</span>.
|
||||
</div>
|
||||
</template>
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import Service from '@ember/service';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
|
||||
export default class ClockService extends Service {
|
||||
@tracked time = new Date();
|
||||
intervalId;
|
||||
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.start();
|
||||
}
|
||||
|
||||
start() {
|
||||
this.intervalId = setInterval(() => {
|
||||
this.time = new Date();
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
willDestroy() {
|
||||
clearInterval(this.intervalId);
|
||||
super.willDestroy();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import Service from '@ember/service';
|
||||
|
||||
const isExtension = typeof browser !== 'undefined' || typeof chrome !== 'undefined';
|
||||
const platform = typeof browser !== 'undefined' ? 'firefox' : typeof chrome !== 'undefined' ? 'chrome' : 'web';
|
||||
|
||||
const storage = isExtension
|
||||
? {
|
||||
get: (key) => (platform === 'firefox' ? browser.storage.local.get(key) : chrome.storage.local.get(key))
|
||||
.then(obj => obj[key]),
|
||||
set: (key, value) => (platform === 'firefox' ? browser.storage.local.set : chrome.storage.local.set)({ [key]: value }),
|
||||
remove: (key) => (platform === 'firefox' ? browser.storage.local.remove : chrome.storage.local.remove)(key)
|
||||
}
|
||||
: {
|
||||
get: (key) => Promise.resolve(localStorage.getItem(key)),
|
||||
set: (key, value) => Promise.resolve(localStorage.setItem(key, JSON.stringify(value))),
|
||||
remove: (key) => Promise.resolve(localStorage.removeItem(key))
|
||||
};
|
||||
|
||||
export default class StorageService extends Service {
|
||||
isExtension = isExtension;
|
||||
platform = platform;
|
||||
|
||||
async get(key) {
|
||||
const value = await storage.get(key);
|
||||
return this.isExtension ? value : JSON.parse(value);
|
||||
}
|
||||
|
||||
async set(key, value) {
|
||||
return storage.set(key, value);
|
||||
}
|
||||
|
||||
async remove(key) {
|
||||
return storage.remove(key);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,6 @@ import DefaultClock from '../components/default-clock.gjs';
|
||||
{{pageTitle "New Tab"}}
|
||||
|
||||
<div class="col-start-2 row-start-2 flex items-center justify-center">
|
||||
<DefaultClock @name="Râu" />
|
||||
<DefaultClock />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,28 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'dashtab/tests/helpers';
|
||||
import { render } from '@ember/test-helpers';
|
||||
import Greeting from 'dashtab/components/greeting';
|
||||
|
||||
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) { ... };
|
||||
|
||||
await render(<template><Greeting /></template>);
|
||||
|
||||
assert.dom().hasText('');
|
||||
|
||||
// Template block usage:
|
||||
await render(<template>
|
||||
<Greeting>
|
||||
template block text
|
||||
</Greeting>
|
||||
</template>);
|
||||
|
||||
assert.dom().hasText('template block text');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'dashtab/tests/helpers';
|
||||
|
||||
module('Unit | Service | clock', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
// TODO: Replace this with your real tests.
|
||||
test('it exists', function (assert) {
|
||||
let service = this.owner.lookup('service:clock');
|
||||
assert.ok(service);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'dashtab/tests/helpers';
|
||||
|
||||
module('Unit | Service | storage', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
// TODO: Replace this with your real tests.
|
||||
test('it exists', function (assert) {
|
||||
let service = this.owner.lookup('service:storage');
|
||||
assert.ok(service);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user