Skip to content

Commit

Permalink
fix(settings): load theme if page was loaded before (#83)
Browse files Browse the repository at this point in the history
resolves #82
  • Loading branch information
DanielHabenicht authored Jul 11, 2019
1 parent 5e2141b commit b7c5725
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Phonebook.Frontend/src/migration/Migration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { migration1, migration2 } from 'src/migration/migrations/';
import { migration1, migration2, migration3 } from 'src/migration/migrations/';

const MIGRATIONS: Migration[] = [migration1, migration2];
const MIGRATIONS: Migration[] = [migration1, migration2, migration3];
const MIGRATION_KEY: string = 'PhonebookMigrationLevel';

export function migrate(): number | undefined {
Expand Down
23 changes: 23 additions & 0 deletions Phonebook.Frontend/src/migration/migrations/3/migration3.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { migration3 } from './migration3';

const APPSTATE_KEY: string = 'appstate';

describe('Migration: 3', () => {
it(migration3.name, () => {
localStorage.setItem(
APPSTATE_KEY,
`{"serviceWorkerNotificationDisplayed":false,"version":"1.0.0","displayedNotificationVersion":1,"sendFeedback":false}`
);
migration3.script();
expect(localStorage.getItem(APPSTATE_KEY)).toEqual(
`{"serviceWorkerNotificationDisplayed":false,"version":"1.0.0","displayedNotificationVersion":1,"sendFeedback":false,"activeTheme":"magenta_light_theme"}`
);
});

it('shoud do no harm if data is already migrated', () => {
const alreadyMigratedString = `{"serviceWorkerNotificationDisplayed":false,"version":"1.0.0","displayedNotificationVersion":1,"sendFeedback":false,"activeTheme":"blue_dark_theme"}`;
localStorage.setItem(APPSTATE_KEY, alreadyMigratedString);
migration3.script();
expect(localStorage.getItem(APPSTATE_KEY)).toEqual(alreadyMigratedString);
});
});
20 changes: 20 additions & 0 deletions Phonebook.Frontend/src/migration/migrations/3/migration3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Migration } from 'src/migration/Migration';

const APPSTATE_KEY: string = 'appstate';
export const migration3: Migration = {
name: 'Add activeTheme Property to AppState',
id: 3,
script: () => {
const appStateString = localStorage.getItem(APPSTATE_KEY);
if (appStateString) {
const appState: any = JSON.parse(appStateString);
if (appState.activeTheme === undefined) {
const newAppState = {
...appState,
activeTheme: 'magenta_light_theme'
};
localStorage.setItem(APPSTATE_KEY, JSON.stringify(newAppState));
}
}
}
};
1 change: 1 addition & 0 deletions Phonebook.Frontend/src/migration/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './1/migration1';
export * from './2/migration2';
export * from './3/migration3';

0 comments on commit b7c5725

Please sign in to comment.