implement the install external apk api

This commit is contained in:
Aria Moradi
2021-09-09 05:14:17 +04:30
parent 7710c287c5
commit 09f92c853d
2 changed files with 63 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ import React from 'react';
import Slide, { SlideProps } from '@material-ui/core/Slide'; import Slide, { SlideProps } from '@material-ui/core/Slide';
import Snackbar from '@material-ui/core/Snackbar'; import Snackbar from '@material-ui/core/Snackbar';
import MuiAlert, { Color as Severity } from '@material-ui/lab/Alert'; import MuiAlert, { Color as Severity } from '@material-ui/lab/Alert';
import cloneObject from 'util/cloneObject';
function removeToast(id: string) { function removeToast(id: string) {
const container = document.querySelector(`#${id}`)!!; const container = document.querySelector(`#${id}`)!!;
@@ -27,7 +28,7 @@ interface IToastProps{
severity: Severity severity: Severity
} }
function Toast(props: IToastProps) { export function Toast(props: IToastProps) {
const { message, severity } = props; const { message, severity } = props;
const [open, setOpen] = React.useState(true); const [open, setOpen] = React.useState(true);
@@ -61,3 +62,20 @@ export default function makeToast(message: string, severity: Severity) {
setTimeout(() => removeToast(container.id), 3500); setTimeout(() => removeToast(container.id), 3500);
} }
export function makeToaster(
[toasts, setToasts] : [React.ReactElement[],
(arg0: React.ReactElement[]) => void],
): [React.ReactElement[], ((message: string, severity: Severity) => void)] {
return [toasts, (message: string, severity: Severity) => {
const curToasts = cloneObject(toasts);
curToasts.push(
<Toast
key={Math.floor(Math.random() * 1000) + 1}
message={message}
severity={severity}
/>,
);
setToasts(curToasts);
}];
}

View File

@@ -6,12 +6,14 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import React, { useContext, useEffect, useState } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import { fromEvent } from 'file-selector';
import ExtensionCard from 'components/manga/ExtensionCard'; import ExtensionCard from 'components/manga/ExtensionCard';
import NavbarContext from 'context/NavbarContext'; import NavbarContext from 'context/NavbarContext';
import client from 'util/client'; import client from 'util/client';
import useLocalStorage from 'util/useLocalStorage'; import useLocalStorage from 'util/useLocalStorage';
import ExtensionLangSelect from 'components/manga/ExtensionLangSelect'; import ExtensionLangSelect from 'components/manga/ExtensionLangSelect';
import { defualtLangs, langCodeToName, langSortCmp } from 'util/language'; import { defualtLangs, langCodeToName, langSortCmp } from 'util/language';
import { makeToaster } from 'components/Toast';
const allLangs: string[] = []; const allLangs: string[] = [];
@@ -81,12 +83,54 @@ export default function MangaExtensions() {
} }
}, [extensionsRaw]); }, [extensionsRaw]);
const [toasts, makeToast] = makeToaster(useState<React.ReactElement[]>([]));
const submitExtension = (file: File) => {
if (file.name.toLowerCase().endsWith('apk')) {
const formData = new FormData();
formData.append('file', file);
makeToast('Installing Extension File....', 'info');
client.post('/api/v1/extension/install',
formData, { headers: { 'Content-Type': 'multipart/form-data' } })
.then(() => {
makeToast('Installed extension successfully!', 'success');
triggerUpdate();
})
.catch(() => makeToast('Extension installion failed!', 'error'));
} else {
makeToast('invalid file type!', 'error');
}
};
const dropHandler = async (e: Event) => {
e.preventDefault();
const files = await fromEvent(e);
submitExtension(files[0] as File);
};
const dragOverHandler = (e: Event) => {
e.preventDefault();
};
useEffect(() => {
document.addEventListener('drop', dropHandler);
document.addEventListener('dragover', dragOverHandler);
return () => {
document.removeEventListener('drop', dropHandler);
document.removeEventListener('dragover', dragOverHandler);
};
}, []);
if (Object.entries(extensions).length === 0) { if (Object.entries(extensions).length === 0) {
return <h3>loading...</h3>; return <h3>loading...</h3>;
} }
const groupsToShow = ['updates pending', 'installed', ...shownLangs]; const groupsToShow = ['updates pending', 'installed', ...shownLangs];
return ( return (
<> <>
{toasts}
{ {
Object.entries(extensions).map(([lang, list]) => ( Object.entries(extensions).map(([lang, list]) => (
((groupsToShow.indexOf(lang) !== -1 && (list as []).length > 0) ((groupsToShow.indexOf(lang) !== -1 && (list as []).length > 0)