-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#0000 add backup feature. Fix some styles
- Loading branch information
1 parent
8117938
commit dca6ad0
Showing
11 changed files
with
218 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
import { createContext } from 'react'; | ||
import CodesRepository from "../repositories/codes.repository"; | ||
import StorageService from "../services/storage.service"; | ||
import FilesStorageService from "../services/files-storage.service"; | ||
import BackupService from "../services/backup.service"; | ||
|
||
const storageService = new StorageService() | ||
const filesStorageService = new FilesStorageService() | ||
|
||
const DependencyContext = createContext({ | ||
codesRepository: new CodesRepository(new StorageService()), | ||
storageService, | ||
codesRepository: new CodesRepository(storageService), | ||
filesStorageService, | ||
backupService: new BackupService(filesStorageService), | ||
}); | ||
|
||
export default DependencyContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {ICode} from "./ICode"; | ||
|
||
export interface IDownloadableData { | ||
version: string; | ||
|
||
[x: string | number]: unknown; | ||
} | ||
|
||
export interface IDownloadableCodes extends IDownloadableData{ | ||
codes: ICode[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { | ||
IonButtons, | ||
IonHeader, | ||
IonMenuButton, | ||
IonTitle, | ||
IonToolbar, | ||
IonPage, | ||
IonContent, | ||
IonButton, | ||
IonGrid, | ||
IonRow, | ||
IonCol, | ||
IonIcon, | ||
useIonToast | ||
} from "@ionic/react"; | ||
import {FC, useContext} from "react"; | ||
import DependencyContext from "../contexts/dependencyContext"; | ||
import {IDownloadableCodes} from "../interfaces/IDownloadableData"; | ||
import projectPackage from "../../package.json" | ||
import {ICode} from "../interfaces/ICode.js"; | ||
import {cloudDownloadOutline, cloudUploadOutline} from "ionicons/icons"; | ||
|
||
const BackupPage: FC = () => { | ||
const {codesRepository, backupService} = useContext(DependencyContext); | ||
const [present] = useIonToast(); | ||
|
||
const prepareCodesToData = async (): Promise<IDownloadableCodes> => { | ||
return { | ||
version: projectPackage.version, | ||
codes: await codesRepository.getCodes() | ||
} | ||
} | ||
|
||
const getCodesDataFromSystem = (): Promise<ICode[]> => { | ||
return backupService.import(); | ||
}; | ||
|
||
const onImportReplaceClick = async () => { | ||
const codes: ICode[] = await getCodesDataFromSystem(); | ||
await codesRepository.setCodes(codes); | ||
present({ | ||
message: 'Успішно імпортовано', | ||
duration: 1500, | ||
position: "bottom", | ||
color: "success" | ||
}); | ||
console.debug("[BackupPage] onImportReplaceClick", {codes}); | ||
} | ||
|
||
const onImportAddClick = async () => { | ||
const codes: ICode[] = await getCodesDataFromSystem(); | ||
await codesRepository.mergeCodes(codes); | ||
present({ | ||
message: 'Успішно імпортовано', | ||
duration: 1500, | ||
position: "bottom", | ||
color: "success" | ||
}); | ||
console.debug("[BackupPage] onImportAddClick", {codes}); | ||
} | ||
|
||
const onExportClick = async () => { | ||
const codesData = await prepareCodesToData() | ||
await backupService.export(codesData) | ||
console.debug("[BackupPage] onExportClick") | ||
} | ||
|
||
return ( | ||
<IonPage> | ||
<IonHeader> | ||
<IonToolbar> | ||
<IonButtons slot="start"> | ||
<IonMenuButton/> | ||
</IonButtons> | ||
<IonTitle>Бекап</IonTitle> | ||
</IonToolbar> | ||
</IonHeader> | ||
|
||
<IonContent fullscreen> | ||
<IonHeader collapse="condense"> | ||
<IonToolbar> | ||
<IonTitle size="large">Бекап</IonTitle> | ||
</IonToolbar> | ||
</IonHeader> | ||
|
||
<IonGrid> | ||
<IonRow className="ion-justify-content-center"> | ||
<IonCol size="auto"> | ||
<IonButton size="large" onClick={onImportAddClick}> | ||
<IonIcon icon={cloudDownloadOutline} /> | ||
Імпорт | ||
</IonButton> | ||
</IonCol> | ||
</IonRow> | ||
<IonRow className="ion-justify-content-center"> | ||
<IonCol size="auto"> | ||
<IonButton size="large" onClick={onImportReplaceClick}> | ||
<IonIcon icon={cloudDownloadOutline} /> | ||
Імпорт (замінити) | ||
</IonButton> | ||
</IonCol> | ||
</IonRow> | ||
<IonRow className="ion-justify-content-center"> | ||
<IonCol size="auto"> | ||
<IonButton size="large" onClick={onExportClick}> | ||
<IonIcon icon={cloudUploadOutline} /> | ||
Експорт | ||
</IonButton> | ||
</IonCol> | ||
</IonRow> | ||
</IonGrid> | ||
</IonContent> | ||
</IonPage> | ||
) | ||
} | ||
|
||
export default BackupPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import FilesStorageService from "./files-storage.service"; | ||
import {ICode} from "../interfaces/ICode.js"; | ||
import {IDownloadableCodes} from "../interfaces/IDownloadableData"; | ||
|
||
export default class BackupService { | ||
constructor(private filesStorageService: FilesStorageService) {} | ||
async import(): Promise<ICode[]> { | ||
const file: string = await this.filesStorageService.open(); | ||
const {codes} = JSON.parse(file) | ||
return codes; | ||
} | ||
|
||
async export(codesData: IDownloadableCodes) { | ||
const date: Date = new Date(); | ||
// const formattedDate = date.getTime(); | ||
const formattedDate: string = "" + date.getFullYear() + date.getMonth() + date.getDate() + date.getHours() + date.getMinutes(); | ||
const fileName: string = `afu-4-code-backup-${formattedDate}.json` | ||
this.filesStorageService.downloadDataJson(fileName, codesData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export type DownloadableData = Object | Array<string>; | ||
|
||
export default class FilesStorageService { | ||
downloadDataJson(fileName: string, data: DownloadableData) { | ||
const element = document.createElement("a"); | ||
const file: Blob = new Blob([JSON.stringify(data)], {type: 'text/json'}); | ||
element.href = URL.createObjectURL(file); | ||
element.download = fileName; | ||
document.body.appendChild(element); // Required for this to work in FireFox | ||
element.click(); | ||
} | ||
|
||
async open(ext: string | undefined = undefined): Promise<string> { | ||
// open file .json, return file/buffer | ||
return new Promise((resolve, reject) => { | ||
const input = document.createElement('input'); | ||
input.type = 'file'; | ||
input.accept = ext || 'application/json'; | ||
input.onchange = (event: any) => { | ||
const file = event.target.files[0]; | ||
const reader = new FileReader(); | ||
reader.onload = (event: any) => { | ||
console.debug("[FilesStorageService] open", {event, file}); | ||
resolve(event.target.result); | ||
}; | ||
reader.readAsText(file); | ||
}; | ||
input.click(); | ||
}) | ||
} | ||
} |