Commit 9976aca7 authored by Genar Trias Ortiz's avatar Genar Trias Ortiz 🎉

trying to generate form for all providers

parent 4e4ad0ef
Pipeline #57153888 canceled with stages
in 5 minutes and 44 seconds
......@@ -21,8 +21,8 @@ class Settings extends React.Component<Props, State> {
this.props.dispatch({type: types.DELETE_SETTINGS})
}
addProvider = (form: any): any => {
this.props.dispatch({type: types.ADD_PROVIDER})
addProvider = (providerId: string): any => {
this.props.dispatch({type: types.ADD_PROVIDER, providerId})
}
render() {
......@@ -36,7 +36,7 @@ class Settings extends React.Component<Props, State> {
dispatch={this.props.dispatch}
/>
<div className='btn-group'>
<button className='with-bg' onClick={this.addProvider}>
<button className='with-bg' onClick={() => this.addProvider('subsonic')}>
<Translate value="buttons.addProvider" />
</button>
<button className='with-bg btn btn-danger' onClick={this.deleteCollection}>
......
......@@ -19,9 +19,7 @@ const SettingsForm = (props: Props) => {
// Convert schema object to form elements
const populateFromSchema = (schema) => {
console.log(props.schema)
const { fields } = props.schema
const { fields } = schema
const populatedFields = fields.map((field, index) => {
if (field.type === TYPES.title) {
......
......@@ -5,6 +5,7 @@ import SettingsBuilder from '../services/settings/SettingsBuilder'
type State = {
error: string,
saving: boolean,
providers: Array<any>,
settings: ISettings,
settingsForm: any
}
......@@ -15,6 +16,7 @@ export const defaultState = {
error: '',
saving: false,
settingsForm: settingsBuilder.getFormSchema(),
providers: [],
settings: {
providers: [],
app: {
......@@ -33,16 +35,20 @@ export default (state: State = defaultState, action: any = {}) => {
}
case types.ADD_PROVIDER: {
const { settings } = state
const { providers } = state
let providerAutoinc = 0
for (let i = 0; i < settings.providers.length; i++) {
if (settings.providers[i].key === action.providerId + '-' + i) {
for (let i = 0; i < providers.length; i++) {
if (providers[i] && providers[i].key === action.providerId + '-' + i) {
providerAutoinc++
}
}
const settingsForm = settingsBuilder.getFormSchema()
const newProvider = {key: action.providerId + '-' + providerAutoinc}
return {...state, providers: [...state.settings.providers, newProvider]}
const draftProviders = [...providers, newProvider]
return {...state, settingsForm, providers: draftProviders}
}
default:
......
......@@ -2,13 +2,29 @@ import { ISettingsBuilder } from '../../interfaces/ISettingsBuilder'
import sections from './sections'
export default class SettingsBuilder implements ISettingsBuilder {
getFormSchema() {
getFormSchema(providers: Array<any> = []) {
const totalFields = Object.keys(sections).reduce((accumulator, sectionId: string) => {
console.log(sections[sectionId])
if (sections[sectionId].isRepeatable) {
return accumulator
}
return [...accumulator, ...sections[sectionId].getFormSchema()]
}, [])
console.log('totalFields: ', totalFields)
const providerFields = Object.keys(sections).reduce((accumulator, sectionId: string) => {
if (!sections[sectionId].isRepeatable) {
return accumulator
}
return [...accumulator, ...sections[sectionId].getFormSchema()]
}, [])
return (
{
providerFields,
fields: totalFields
}
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment