2021-08-06 04:53:53 +04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
2021-10-30 16:10:34 +03:30
|
|
|
import NavbarContext from 'components/context/NavbarContext';
|
2021-08-06 04:53:53 +04:30
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
|
import client from 'util/client';
|
2021-10-30 16:10:34 +03:30
|
|
|
import { SwitchPreferenceCompat, CheckBoxPreference } from 'components/sourceConfiguration/TwoStatePreference';
|
|
|
|
|
import ListPreference from 'components/sourceConfiguration/ListPreference';
|
|
|
|
|
import EditTextPreference from 'components/sourceConfiguration/EditTextPreference';
|
2021-09-09 17:51:22 +04:30
|
|
|
import List from '@mui/material/List';
|
2021-09-13 19:18:21 +04:30
|
|
|
import cloneObject from 'util/cloneObject';
|
2021-08-06 04:53:53 +04:30
|
|
|
|
|
|
|
|
function getPrefComponent(type: string) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'CheckBoxPreference':
|
|
|
|
|
return CheckBoxPreference;
|
2021-09-13 19:18:21 +04:30
|
|
|
case 'SwitchPreferenceCompat':
|
|
|
|
|
return SwitchPreferenceCompat;
|
|
|
|
|
case 'ListPreference':
|
|
|
|
|
return ListPreference;
|
|
|
|
|
case 'EditTextPreference':
|
|
|
|
|
return EditTextPreference;
|
2021-08-06 04:53:53 +04:30
|
|
|
default:
|
|
|
|
|
return CheckBoxPreference;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function SourceConfigure() {
|
|
|
|
|
const [sourcePreferences, setSourcePreferences] = useState<SourcePreferences[]>([]);
|
|
|
|
|
const { setTitle, setAction } = useContext(NavbarContext);
|
|
|
|
|
|
2021-09-13 19:18:21 +04:30
|
|
|
const [updateTriggerHolder, setUpdateTriggerHolder] = useState<number>(0); // just a hack
|
|
|
|
|
const triggerUpdate = () => setUpdateTriggerHolder(updateTriggerHolder + 1); // just a hack
|
|
|
|
|
|
2021-08-06 04:53:53 +04:30
|
|
|
useEffect(() => { setTitle('Source Configuration'); setAction(<></>); }, []);
|
|
|
|
|
|
|
|
|
|
const { sourceId } = useParams<{ sourceId: string }>();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
client.get(`/api/v1/source/${sourceId}/preferences`)
|
|
|
|
|
.then((response) => response.data)
|
|
|
|
|
.then((data) => setSourcePreferences(data));
|
2021-09-13 19:18:21 +04:30
|
|
|
}, [updateTriggerHolder]);
|
|
|
|
|
|
|
|
|
|
const updateValue = (position: number) => (
|
|
|
|
|
(value: any) => {
|
|
|
|
|
client.post(`/api/v1/source/${sourceId}/preferences`,
|
|
|
|
|
JSON.stringify({ position, value: value.toString() }))
|
|
|
|
|
.then(() => triggerUpdate());
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-08-06 04:53:53 +04:30
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2021-11-15 17:21:31 +03:30
|
|
|
<List style={{ padding: 0 }}>
|
2021-08-06 04:53:53 +04:30
|
|
|
{sourcePreferences.map(
|
2021-09-13 19:18:21 +04:30
|
|
|
(it, index) => {
|
|
|
|
|
const props = cloneObject(it.props);
|
|
|
|
|
props.updateValue = updateValue(index);
|
|
|
|
|
props.key = index;
|
|
|
|
|
|
|
|
|
|
// TypeScript is dumb in detecting extra props
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
return React.createElement(getPrefComponent(it.type), props);
|
|
|
|
|
},
|
2021-08-06 04:53:53 +04:30
|
|
|
)}
|
|
|
|
|
</List>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|