Skip to content

Commit

Permalink
Add preliminary implementation of local backup service
Browse files Browse the repository at this point in the history
  • Loading branch information
bkimminich committed Jul 13, 2020
1 parent 11571b4 commit 00a8573
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
11 changes: 11 additions & 0 deletions frontend/src/app/Models/backup.model.ts
@@ -0,0 +1,11 @@
/*
* Copyright (c) 2014-2020 Bjoern Kimminich.
* SPDX-License-Identifier: MIT
*/

export interface Backup {
continueCode?: string
language?: string
banners?: { welcomeBannerStatus?: string; cookieConsentStatus?: string }
scoreBoard?: { showOnlyTutorialChallenges?: string; displayedChallengeCategories?: string; displayedDifficulties?: string; showDisabledChallenges?: string; showSolvedChallenges?: string }
}
19 changes: 19 additions & 0 deletions frontend/src/app/Services/local-backup.service.spec.ts
@@ -0,0 +1,19 @@
import { inject, TestBed } from '@angular/core/testing'

import { LocalBackupService } from './local-backup.service'
import { CookieService } from 'ngx-cookie-service'

describe('LocalBackupService', () => {
let service: LocalBackupService

beforeEach(() => {
TestBed.configureTestingModule({
providers: [CookieService]
})
service = TestBed.inject(LocalBackupService)
})

it('should be created', inject([CookieService], (service: LocalBackupService) => {
expect(service).toBeTruthy()
}))
})
47 changes: 47 additions & 0 deletions frontend/src/app/Services/local-backup.service.ts
@@ -0,0 +1,47 @@
import { Injectable } from '@angular/core'
import { Backup } from '../Models/backup.model'
import { CookieService } from 'ngx-cookie-service'

@Injectable({
providedIn: 'root'
})
export class LocalBackupService {

constructor (private cookieService: CookieService) { }

save () {
const backup: Backup = { }

backup.scoreBoard = {
displayedDifficulties: localStorage.getItem('displayedDifficulties'),
showSolvedChallenges: localStorage.getItem('showSolvedChallenges'),
showDisabledChallenges: localStorage.getItem('showDisabledChallenges'),
showOnlyTutorialChallenges: localStorage.getItem('showOnlyTutorialChallenges'),
displayedChallengeCategories: localStorage.getItem('displayedChallengeCategories')
}
backup.banners = {
welcomeBannerStatus: this.cookieService.get('welcomebanner_status'),
cookieConsentStatus: this.cookieService.get('cookieconsent_status')
}
backup.language = this.cookieService.get('language')
backup.continueCode = this.cookieService.get('continueCode')

return JSON.stringify(backup)
}

restore (backupData: string) {
const backup: Backup = JSON.parse(backupData)

localStorage.setItem('displayedDifficulties', JSON.stringify(backup.scoreBoard.displayedDifficulties))
localStorage.setItem('showSolvedChallenges', JSON.stringify(backup.scoreBoard.showSolvedChallenges))
localStorage.setItem('showDisabledChallenges', JSON.stringify(backup.scoreBoard.showDisabledChallenges))
localStorage.setItem('showOnlyTutorialChallenges', JSON.stringify(backup.scoreBoard.showOnlyTutorialChallenges))
localStorage.setItem('displayedChallengeCategories', JSON.stringify(backup.scoreBoard.displayedChallengeCategories))
let expires = new Date()
expires.setFullYear(expires.getFullYear() + 1)
this.cookieService.set('welcomebanner_status', JSON.stringify(backup.banners.welcomeBannerStatus), expires, '/')
this.cookieService.set('cookieconsent_status', JSON.stringify(backup.banners.cookieConsentStatus), expires, '/')
this.cookieService.set('language', JSON.stringify(backup.language), expires, '/')
this.cookieService.set('continueCode', JSON.stringify(backup.continueCode), expires, '/')
}
}

0 comments on commit 00a8573

Please sign in to comment.