Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user settings in localStorage #220

Merged
merged 7 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { action } from '@ember/object';

export default class ApplicationRoute extends Route {

@service localData;
@service logger;
@service coms;

beforeModel () {
async beforeModel () {
super.beforeModel(...arguments);
await this.localData.setDefaultValues();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️


// See a list of allowed types in logger.js
// Add or remove all your log types here:
// this.logger.add('message');
Expand Down
14 changes: 8 additions & 6 deletions app/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Route from '@ember/routing/route';
import { storageFor as localStorageFor } from 'ember-local-storage';
import config from 'hyperchannel/config/environment';
import { inject as service } from '@ember/service';
import { alias } from '@ember/object/computed';

export default class IndexRoute extends Route {

userSettings = localStorageFor('user-settings');
@service localData;
@alias('localData.stores.userSettings') userSettings;

redirect () {
let currentSpace = this.userSettings.currentSpace || config.defaultSpaceId;
let currentChannel = this.userSettings.currentChannel || 'kosmos';
async redirect () {
const currentSpace = await this.userSettings.getItem('currentSpace');
const currentChannel = await this.userSettings.getItem('currentChannel');
// TODO if current space from setting is not available, use first available space and channel

if (currentSpace && currentChannel) {
this.transitionTo('space.channel', currentSpace, currentChannel);
Expand Down
11 changes: 6 additions & 5 deletions app/routes/space/base_channel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { alias } from '@ember/object/computed';
import { scheduleOnce } from '@ember/runloop';
import { storageFor as localStorageFor } from 'ember-local-storage';

function focusMessageInput() {
if (window.innerWidth > 900) {
Expand All @@ -15,7 +15,8 @@ function focusMessageInput() {
export default class BaseChannelRoute extends Route {

@service coms;
userSettings = localStorageFor('user-settings');
@service localData;
@alias('localData.stores.userSettings') userSettings;

model (params) {
let space = this.modelFor('space');
Expand All @@ -35,12 +36,12 @@ export default class BaseChannelRoute extends Route {
}

@action
didTransition () {
async didTransition () {
let space = this.modelFor('space');
let channel = this.controller.model;

this.userSettings.currentSpace = space.id;
this.userSettings.currentChannel = channel.slug;
await this.userSettings.setItem('currentSpace', space.id);
await this.userSettings.setItem('currentChannel', channel.slug);

// Mark all other channels as inactive/invisible
this.coms.spaces.forEach((space) => {
Expand Down
2 changes: 0 additions & 2 deletions app/services/coms.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import UserChannel from 'hyperchannel/models/user_channel';
import Message from 'hyperchannel/models/message';
import config from 'hyperchannel/config/environment';
import moment from 'moment';
import { storageFor as localStorageFor } from 'ember-local-storage';
import { tracked } from '@glimmer/tracking';

/**
Expand All @@ -21,7 +20,6 @@ export default class ComsService extends Service {
// Utils
@service logger;
// Data storage
@localStorageFor('user-settings') userSettings;
@service('remotestorage') storage;
// Message transport
@service('sockethub-irc') irc;
Expand Down
38 changes: 38 additions & 0 deletions app/services/local-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Service from '@ember/service';
import { isEmpty } from '@ember/utils';
import config from 'hyperchannel/config/environment';
import * as localforage from 'localforage';

const defaultValues = {
userSettings: {
nickname: null,
currentSpace: config.defaultSpaceId,
currentChannel: 'kosmos'
}
};

export default class LocalDataService extends Service {

constructor () {
super(...arguments);
this.stores = {
userSettings: localforage.createInstance({
name: 'hyperchannel', storeName: 'userSettings'
})
}
}

async setDefaultValues () {
for (const storeName of Object.keys(defaultValues)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you can try is assign const defaultValuesKeys = Object.keys(defaultValues); outside the class...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make CodeClimate happy

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't help. :/

const store = this.stores[storeName];

for (const key of Object.keys(defaultValues[storeName])) {
const value = await store.getItem(key);
if (isEmpty(value)) {
await store.setItem(key, defaultValues[storeName][key]);
}
}
}
}

}
16 changes: 0 additions & 16 deletions app/storages/user-settings.js

This file was deleted.

Loading