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()
}
}
}

View File

@@ -0,0 +1,40 @@
import { TZ } from '@constants/env'
import { format as fnsFormat, Locale } from 'date-fns'
import { utcToZonedTime } from 'date-fns-tz'
import { ko, enUS } from 'date-fns/locale'
type DateType = number | Date
export const defaultlocales: Record<string, Locale> = { ko, enUS }
const locale =
typeof window !== 'undefined'
? defaultlocales[window.__localeId__]
: defaultlocales[global.__localeId__] // Check browser, server
// by providing a default string of 'PP' or any of its variants for `formatStr`
// it will format dates in whichever way is appropriate to the locale
export const format = (date: DateType, formatStr = 'PP') => {
return fnsFormat(date, formatStr, {
locale,
})
}
export const getCurrentDate = (timezone?: string) => {
return utcToZonedTime(Date.now(), timezone || TZ)
}
export const convertStringToDate = (
date: string | Date,
timezone: string = TZ,
) => {
return utcToZonedTime(new Date(date), timezone)
}
export const convertStringToDateFormat = (
date: string | Date,
formatStr = 'yyyy-MM-dd',
timezone: string = TZ,
) => {
return format(convertStringToDate(date, timezone), formatStr)
}

View File

@@ -0,0 +1,28 @@
import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import translationEn from 'public/locales/en/common.json'
import translationKo from 'public/locales/ko/common.json'
import { DEV } from '@constants/env'
const resources = {
en: {
translation: translationEn,
},
ko: {
translation: translationKo,
},
}
i18n.use(initReactI18next).init({
resources,
lng: 'ko',
fallbackLng: 'ko',
debug: DEV,
keySeparator: false,
interpolation: {
escapeValue: false,
},
})
export default i18n