frontend add

This commit is contained in:
shinmj
2021-10-21 09:03:17 +09:00
parent 8caa4bbc5a
commit cb9d50511e
443 changed files with 88282 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
import { loginFormType } from '@components/Auth/LoginForm'
import { LocalStorageWorker } from './index'
// custom class for store emails in local storage
export class EmailStorage {
private storageWorker: LocalStorageWorker
// main key
private storageKey: string
// login info data
private loginInfo: loginFormType
constructor(storageKey: string) {
this.storageWorker = new LocalStorageWorker()
this.storageKey = storageKey
this.loginInfo = { email: null, password: null, isRemember: false }
this.activate()
}
// activate custom storage for login info
activate() {
this.load()
}
load() {
var storageData = this.storageWorker.get(this.storageKey)
if (storageData != null && storageData.length > 0) {
var info = JSON.parse(storageData)
if (info) {
this.loginInfo = info
}
}
}
get() {
return this.loginInfo
}
// add new email (without duplicate)
set(info: loginFormType) {
if (info.isRemember) {
this.loginInfo = info
// save to storage
this.save()
} else {
this.clear()
}
}
// clear all data about login info
clear() {
// remove with key
this.storageWorker.remove(this.storageKey)
}
// save to storage (save as JSON string)
save() {
var jsonInfo = JSON.stringify(this.loginInfo)
this.storageWorker.add(this.storageKey, jsonInfo)
}
}

View File

@@ -0,0 +1,92 @@
// module with classes and logic for working with local storage in browsers via JavaScript
// see also: http://professorweb.ru/my/html/html5/level5/5_1.php
export interface IStorageItem {
key: string
value: any
}
export class StorageItem {
key: string
value: any
constructor(data: IStorageItem) {
this.key = data.key
this.value = data.value
}
}
// class for working with local storage in browser (common that can use other classes for store some data)
export class LocalStorageWorker {
localStorageSupported: boolean
constructor() {
this.localStorageSupported =
typeof window['localStorage'] != 'undefined' &&
window['localStorage'] != null
}
// add value to storage
add(key: string, item: string) {
if (this.localStorageSupported) {
localStorage.setItem(key, item)
}
}
// get all values from storage (all items)
getAllItems(): Array<StorageItem> {
var list = new Array<StorageItem>()
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i)
var value = localStorage.getItem(key)
list.push(
new StorageItem({
key: key,
value: value,
}),
)
}
return list
}
// get only all values from localStorage
getAllValues(): Array<any> {
var list = new Array<any>()
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i)
var value = localStorage.getItem(key)
list.push(value)
}
return list
}
// get one item by key from storage
get(key: string): string {
if (this.localStorageSupported) {
var item = localStorage.getItem(key)
return item
} else {
return null
}
}
// remove value from storage
remove(key: string) {
if (this.localStorageSupported) {
localStorage.removeItem(key)
}
}
// clear storage (remove all items from it)
clear() {
if (this.localStorageSupported) {
localStorage.clear()
}
}
}