mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-01 20:36:21 -05:00
Configs are displayed in new ConfigsPage under Admin.
- Configs can be imported and saved. - Configs are validated before imported. - API will refuse to save a config if it was not imported successfully.
This commit is contained in:
parent
d34301a085
commit
acf3dee179
22 changed files with 767 additions and 157 deletions
|
|
@ -83,6 +83,14 @@ function handleEvents(events) {
|
|||
eventToast('Move Failed', message, 'error');
|
||||
}
|
||||
|
||||
if (event === 'config_import_failed') {
|
||||
eventToast('Config Import Failed', message, 'error');
|
||||
}
|
||||
|
||||
if (event === 'config_save_failed') {
|
||||
eventToast('Config Save Failed', message, 'error');
|
||||
}
|
||||
|
||||
if (subject) {
|
||||
newestEvents[subject] = dt;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -313,6 +313,55 @@ export async function getDownloads() {
|
|||
return response
|
||||
}
|
||||
|
||||
export async function getConfigs() {
|
||||
const response = await apiGet(`${API_URI}/configs`);
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
return {
|
||||
configs: data.configs,
|
||||
}
|
||||
} else {
|
||||
const message = getErrorMessage(response, 'Could not get configs.');
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Getting Configs Failed',
|
||||
description: message,
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
export async function postImportConfig(fileName) {
|
||||
const body = {file_name: fileName};
|
||||
const response = await apiPost(`${API_URI}/configs/import`, body);
|
||||
if (!response.ok) {
|
||||
const message = getErrorMessage(response, 'Could not import config.');
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Importing config Failed',
|
||||
description: message,
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
export async function postSaveConfig(fileName) {
|
||||
const body = {file_name: fileName, overwrite: true};
|
||||
const response = await apiPost(`${API_URI}/configs/save`, body);
|
||||
if (!response.ok) {
|
||||
const message = getErrorMessage(response, 'Could not save config.');
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Saving config Failed',
|
||||
description: message,
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
export async function validateRegex(regex) {
|
||||
const response = await apiPost(`${API_URI}/valid_regex`, {regex});
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ import {
|
|||
HandPointMessage,
|
||||
humanFileSize,
|
||||
isoDatetimeToAgoPopup,
|
||||
isoDatetimeToString,
|
||||
mimetypeColor,
|
||||
PageContainer,
|
||||
Paginator,
|
||||
|
|
|
|||
|
|
@ -53,3 +53,9 @@ export function TagPlaceholder() {
|
|||
<PlaceholderImage/>
|
||||
</Placeholder>
|
||||
}
|
||||
|
||||
export function TableRowPlaceholder() {
|
||||
return <Placeholder>
|
||||
<PlaceholderLine/>
|
||||
</Placeholder>
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ import {Header, Loader, Segment} from "../Theme";
|
|||
import {DownloadsPage} from "./Downloads";
|
||||
import {SettingsPage} from "./Settings";
|
||||
import {StatusPage} from "./Status";
|
||||
import {ConfigsPage} from "./Configs";
|
||||
|
||||
class WROLMode extends React.Component {
|
||||
|
||||
|
|
@ -71,6 +72,7 @@ export default function AdminRoute() {
|
|||
{text: 'Downloads', to: '/admin', key: 'admin', end: true},
|
||||
{text: 'Settings', to: '/admin/settings', key: 'settings'},
|
||||
{text: 'Status', to: '/admin/status', key: 'status'},
|
||||
{text: 'Configs', to: '/admin/configs', key: 'configs'},
|
||||
{text: 'WROL Mode', to: '/admin/wrol', key: 'wrol'},
|
||||
];
|
||||
|
||||
|
|
@ -80,6 +82,7 @@ export default function AdminRoute() {
|
|||
<Route path='/' exact element={<DownloadsPage/>}/>
|
||||
<Route path='settings' exact element={<SettingsPage/>}/>
|
||||
<Route path='status' exact element={<StatusPage/>}/>
|
||||
<Route path='configs' exact element={<ConfigsPage/>}/>
|
||||
<Route path='wrol' exact element={<WROLMode/>}/>
|
||||
</Routes>
|
||||
</PageContainer>
|
||||
|
|
|
|||
148
app/src/components/admin/Configs.js
Normal file
148
app/src/components/admin/Configs.js
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
import {useConfigs} from "../../hooks/customHooks";
|
||||
import {APIButton, PageContainer} from "../Common";
|
||||
import {Header, Icon, Popup, Table} from "../Theme";
|
||||
import React from "react";
|
||||
import {
|
||||
Grid,
|
||||
GridColumn,
|
||||
GridRow,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHeader,
|
||||
TableHeaderCell,
|
||||
TableRow
|
||||
} from "semantic-ui-react";
|
||||
import {TableRowPlaceholder} from "../Placeholder";
|
||||
import {useNavigate} from "react-router-dom";
|
||||
|
||||
function WarningIcon({ok}) {
|
||||
return <Icon color={ok ? 'violet' : 'red'} name={ok ? 'check' : 'close'}/>
|
||||
}
|
||||
|
||||
function ConfigTableRow({config, importConfig, saveConfig, fetchConfigs}) {
|
||||
const {file_name, rel_path, valid, successful_import} = config;
|
||||
const navigate = useNavigate();
|
||||
|
||||
const localImportConfig = async () => {
|
||||
await importConfig(file_name);
|
||||
await fetchConfigs();
|
||||
}
|
||||
|
||||
const localSaveConfig = async () => {
|
||||
await saveConfig(file_name);
|
||||
await fetchConfigs();
|
||||
}
|
||||
|
||||
const importButton = <APIButton
|
||||
icon='upload'
|
||||
disabled={!valid}
|
||||
onClick={localImportConfig}
|
||||
/>;
|
||||
|
||||
let saveButton;
|
||||
if (successful_import) {
|
||||
saveButton = <APIButton
|
||||
color='grey'
|
||||
icon='download'
|
||||
onClick={localSaveConfig}
|
||||
/>;
|
||||
} else {
|
||||
// Warn the user they may overwrite because import was not successful.
|
||||
saveButton = <APIButton
|
||||
color='grey'
|
||||
icon='download'
|
||||
onClick={localSaveConfig}
|
||||
confirmButton='Overwrite'
|
||||
confirmContent='Config was not imported successfully, overwrite the config? Data may be lost!'
|
||||
/>;
|
||||
}
|
||||
|
||||
return <TableRow>
|
||||
<TableCell><span onClick={() => navigate(`?preview=${rel_path}`)}>{file_name}</span></TableCell>
|
||||
<TableCell><WarningIcon ok={successful_import}/></TableCell>
|
||||
<TableCell><WarningIcon ok={valid}/></TableCell>
|
||||
<TableCell>{importButton}</TableCell>
|
||||
<TableCell>{saveButton}</TableCell>
|
||||
</TableRow>
|
||||
}
|
||||
|
||||
function ConfigsTable({configs, loading, importConfig, saveConfig, fetchConfigs}) {
|
||||
let body = <TableRow>
|
||||
<TableCell colSpan={4}><TableRowPlaceholder/></TableCell>
|
||||
</TableRow>
|
||||
|
||||
if (loading) {
|
||||
// Configs are being fetched again.
|
||||
} else if (configs === undefined) {
|
||||
body = <TableRow>
|
||||
<TableCell colSpan={2}>Failed to fetch configs</TableCell>
|
||||
</TableRow>;
|
||||
} else if (configs && Object.keys(configs).length === 0) {
|
||||
body = <TableRow>
|
||||
<TableCell colSpan={2}>No configs exist. Try tagging some files.</TableCell>
|
||||
</TableRow>;
|
||||
} else if (configs) {
|
||||
body = Object.entries(configs).map(([key, value]) =>
|
||||
<ConfigTableRow
|
||||
key={key}
|
||||
config={value}
|
||||
importConfig={importConfig}
|
||||
saveConfig={saveConfig}
|
||||
fetchConfigs={fetchConfigs}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return <Table unstackable striped>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHeaderCell width={10}>File Name</TableHeaderCell>
|
||||
<TableHeaderCell width={1}>Imported</TableHeaderCell>
|
||||
<TableHeaderCell width={1}>Valid</TableHeaderCell>
|
||||
<TableHeaderCell width={1}>Import</TableHeaderCell>
|
||||
<TableHeaderCell width={1}>Save</TableHeaderCell>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
|
||||
<TableBody>
|
||||
{body}
|
||||
</TableBody>
|
||||
</Table>
|
||||
}
|
||||
|
||||
export function ConfigsPage() {
|
||||
const {configs, loading, importConfig, saveConfig, fetchConfigs} = useConfigs();
|
||||
|
||||
React.useEffect(() => {
|
||||
fetchConfigs();
|
||||
}, []);
|
||||
|
||||
const refreshButton = <APIButton
|
||||
icon='refresh'
|
||||
onClick={fetchConfigs}
|
||||
/>;
|
||||
const refreshPopupButton = <Popup
|
||||
content='Check if configs are valid'
|
||||
on='hover'
|
||||
trigger={refreshButton}
|
||||
/>;
|
||||
|
||||
return <PageContainer>
|
||||
<Grid columns={2}>
|
||||
<GridRow>
|
||||
<GridColumn>
|
||||
<Header as='h1'>Configs</Header>
|
||||
</GridColumn>
|
||||
<GridColumn textAlign='right'>{refreshPopupButton}</GridColumn>
|
||||
</GridRow>
|
||||
</Grid>
|
||||
|
||||
<ConfigsTable
|
||||
configs={configs}
|
||||
loading={loading}
|
||||
importConfig={importConfig}
|
||||
saveConfig={saveConfig}
|
||||
fetchConfigs={fetchConfigs}
|
||||
/>
|
||||
</PageContainer>
|
||||
}
|
||||
|
|
@ -297,8 +297,17 @@ export class SettingsPage extends React.Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
const controlSegment = <Segment>
|
||||
<Header as='h3'>Control WROLPi</Header>
|
||||
<RestartButton/>
|
||||
<ShutdownButton/>
|
||||
</Segment>;
|
||||
|
||||
if (this.state.ready === false) {
|
||||
return <Loader active inline='centered'/>
|
||||
return <>
|
||||
<Loader active inline='centered'/>
|
||||
{controlSegment}
|
||||
</>
|
||||
} else if (this.state.ready === undefined) {
|
||||
return <ErrorMessage>Unable to fetch settings</ErrorMessage>
|
||||
}
|
||||
|
|
@ -539,11 +548,7 @@ export class SettingsPage extends React.Component {
|
|||
</Form>
|
||||
</Segment>
|
||||
|
||||
<Segment>
|
||||
<Header as='h3'>Control WROLPi</Header>
|
||||
<RestartButton/>
|
||||
<ShutdownButton/>
|
||||
</Segment>
|
||||
{controlSegment}
|
||||
</Container>;
|
||||
}
|
||||
}</SettingsContext.Consumer>
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import {
|
|||
getArchive,
|
||||
getChannel,
|
||||
getChannels,
|
||||
getConfigs,
|
||||
getDirectories,
|
||||
getDownloads,
|
||||
getFiles,
|
||||
|
|
@ -17,7 +18,10 @@ import {
|
|||
getStatus,
|
||||
getVideo,
|
||||
getVideosStatistics,
|
||||
searchArchives, searchChannels,
|
||||
postImportConfig,
|
||||
postSaveConfig,
|
||||
searchArchives,
|
||||
searchChannels,
|
||||
searchDirectories,
|
||||
searchVideos,
|
||||
searchZim,
|
||||
|
|
@ -679,6 +683,44 @@ export const useDownloads = () => {
|
|||
return {onceDownloads, recurringDownloads, pendingOnceDownloads, fetchDownloads}
|
||||
}
|
||||
|
||||
export const useConfigs = () => {
|
||||
const [configs, setConfigs] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const fetchConfigs = async () => {
|
||||
setLoading(true);
|
||||
console.log('fetching configs...');
|
||||
try {
|
||||
const data = await getConfigs();
|
||||
setConfigs(data['configs']);
|
||||
console.debug('fetching configs successful');
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
setConfigs(undefined);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
const importConfig = async (fileName) => {
|
||||
try {
|
||||
await postImportConfig(fileName);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
const saveConfig = async (fileName) => {
|
||||
try {
|
||||
await postSaveConfig(fileName);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
return {configs, loading, fetchConfigs, importConfig, saveConfig}
|
||||
}
|
||||
|
||||
export const useThrottle = () => {
|
||||
const [on, setOn] = useState(null);
|
||||
const {settings, fetchSettings} = React.useContext(SettingsContext);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue