Commit 5a5afac8 authored by Joseph Izang's avatar Joseph Izang

localize state for localPlugin form. Fix url input control error

parent 0b761821
...@@ -7,48 +7,70 @@ import { FormStateProps, PluginManagerComponent } from '../../types' ...@@ -7,48 +7,70 @@ import { FormStateProps, PluginManagerComponent } from '../../types'
import { localPluginReducerActionType, localPluginToastReducer } from '../reducers/pluginManagerReducer' import { localPluginReducerActionType, localPluginToastReducer } from '../reducers/pluginManagerReducer'
interface LocalPluginFormProps { interface LocalPluginFormProps {
changeHandler: (propertyName: string, value: any) => void
plugin: FormStateProps
closeModal: () => void closeModal: () => void
visible: boolean visible: boolean
pluginManager: PluginManagerComponent pluginManager: PluginManagerComponent
} }
const initialState: FormStateProps = {
name: '',
displayName: '',
url: '',
type: 'iframe',
hash: '',
methods: [],
location: 'sidePanel'
}
const defaultProfile = { const defaultProfile = {
methods: [], methods: [],
location: 'sidePanel', location: 'sidePanel',
type: 'iframe' type: 'iframe',
name: '',
displayName: '',
url: '',
hash: ''
} }
function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginManager }: LocalPluginFormProps) { function LocalPluginForm ({ closeModal, visible, pluginManager }: LocalPluginFormProps) {
const [errorMsg, dispatchToastMsg] = useReducer(localPluginToastReducer, '') const [errorMsg, dispatchToastMsg] = useReducer(localPluginToastReducer, '')
const [defaultPlugin] = useState<FormStateProps>(JSON.parse(localStorage.getItem('plugins/local')) || defaultProfile) const [defaultPlugin] = useState<FormStateProps>(JSON.parse(localStorage.getItem('plugins/local')) || defaultProfile)
const [name, setName] = useState<string>('')
const [displayName, setDisplayName] = useState<string>('')
const [url, setUrl] = useState<string>('')
const [type, setType] = useState<'iframe' | 'ws'>('iframe')
const [location, setLocation] = useState<'sidePanel' | 'mainPanel' | 'none'>('sidePanel')
const [methods, setMethods] = useState<string>('')
const handleModalOkClick = async () => { const handleModalOkClick = async () => {
try { try {
if (!plugin.name) throw new Error('Plugin should have a name') if (!name) throw new Error('Plugin should have a name')
if (pluginManager.appManager.getIds().includes(plugin.name)) { if (pluginManager.appManager.getIds().includes(name)) {
throw new Error('This name has already been used') throw new Error('This name has already been used')
} }
if (!plugin.location) throw new Error('Plugin should have a location') if (!location) throw new Error('Plugin should have a location')
if (!plugin.url) throw new Error('Plugin should have an URL') if (!url) throw new Error('Plugin should have an URL')
plugin.methods = typeof plugin.methods === 'string' ? plugin.methods.split(',').filter(val => val) : [] // eslint-disable-next-line no-debugger
const localPlugin = plugin.type === 'iframe' ? new IframePlugin(plugin) : new WebsocketPlugin(plugin) debugger
const newMethods = typeof methods === 'string' ? methods.split(',').filter(val => val) : []
localPlugin.profile.hash = `local-${plugin.name}`
const targetPlugin = { const targetPlugin = {
name: localPlugin.profile.name, name: name,
displayName: localPlugin.profile.displayName, displayName: displayName,
description: (localPlugin.profile.description !== undefined ? localPlugin.profile.description : ''), description: '',
documentation: localPlugin.profile.url, documentation: url,
events: (localPlugin.profile.events !== undefined ? localPlugin.profile.events : []), events: [],
hash: localPlugin.profile.hash, hash: '',
kind: (localPlugin.profile.kind !== undefined ? localPlugin.profile.kind : ''), kind: '',
methods: localPlugin.profile.methods, methods: newMethods,
type: plugin.type, type: type,
location: plugin.location, location: location,
icon: 'assets/img/localPlugin.webp' icon: 'assets/img/localPlugin.webp'
} }
const localPlugin = type === 'iframe' ? new IframePlugin(initialState) : new WebsocketPlugin(initialState)
localPlugin.profile.hash = `local-${name}`
targetPlugin.description = localPlugin.profile.description !== undefined ? localPlugin.profile.description : ''
targetPlugin.events = localPlugin.profile.events !== undefined ? localPlugin.profile.events : []
targetPlugin.kind = localPlugin.profile.kind !== undefined ? localPlugin.profile.kind : ''
localPlugin.profile = { ...localPlugin.profile, ...targetPlugin } localPlugin.profile = { ...localPlugin.profile, ...targetPlugin }
pluginManager.activateAndRegisterLocalPlugin(localPlugin) pluginManager.activateAndRegisterLocalPlugin(localPlugin)
} catch (error) { } catch (error) {
...@@ -74,8 +96,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa ...@@ -74,8 +96,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa
<label htmlFor="plugin-name">Plugin Name <small>(required)</small></label> <label htmlFor="plugin-name">Plugin Name <small>(required)</small></label>
<input <input
className="form-control" className="form-control"
onChange={e => changeHandler('name', e.target.value)} onChange={e => setName(e.target.value)}
value={ plugin.name || defaultPlugin.name } value={ name || defaultPlugin.name }
id="plugin-name" id="plugin-name"
data-id="localPluginName" data-id="localPluginName"
placeholder="Should be camelCase" /> placeholder="Should be camelCase" />
...@@ -84,8 +106,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa ...@@ -84,8 +106,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa
<label htmlFor="plugin-displayname">Display Name</label> <label htmlFor="plugin-displayname">Display Name</label>
<input <input
className="form-control" className="form-control"
onChange={e => changeHandler('displayName', e.target.value)} onChange={e => setDisplayName(e.target.value)}
value={ plugin.displayName || defaultPlugin.displayName } value={ displayName || defaultPlugin.displayName }
id="plugin-displayname" id="plugin-displayname"
data-id="localPluginDisplayName" data-id="localPluginDisplayName"
placeholder="Name in the header" /> placeholder="Name in the header" />
...@@ -94,8 +116,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa ...@@ -94,8 +116,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa
<label htmlFor="plugin-methods">Api (comma separated list of methods name)</label> <label htmlFor="plugin-methods">Api (comma separated list of methods name)</label>
<input <input
className="form-control" className="form-control"
onChange={e => changeHandler('methods', e.target.value)} onChange={e => setMethods(e.target.value)}
value={plugin.methods || defaultPlugin.methods} value={methods || defaultPlugin.methods}
id="plugin-methods" id="plugin-methods"
data-id="localPluginMethods" data-id="localPluginMethods"
placeholder="Name in the header" /> placeholder="Name in the header" />
...@@ -105,8 +127,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa ...@@ -105,8 +127,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa
<label htmlFor="plugin-url">Url <small>(required)</small></label> <label htmlFor="plugin-url">Url <small>(required)</small></label>
<input <input
className="form-control" className="form-control"
onChange={e => changeHandler('url', e.target.value)} onChange={e => setUrl(e.target.value)}
value={ plugin.url || defaultPlugin.url } value={ url || defaultPlugin.url }
id="plugin-url" id="plugin-url"
data-id="localPluginUrl" data-id="localPluginUrl"
placeholder="ex: https://localhost:8000" /> placeholder="ex: https://localhost:8000" />
...@@ -121,8 +143,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa ...@@ -121,8 +143,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa
value="iframe" value="iframe"
id="iframe" id="iframe"
data-id='localPluginRadioButtoniframe' data-id='localPluginRadioButtoniframe'
checked={plugin.type === 'iframe'} checked={type === 'iframe'}
onChange={(e) => changeHandler('type', e.target.value)} /> onChange={(e) => setType(e.target.value as 'iframe' | 'ws')} />
<label className="form-check-label" htmlFor="iframe">Iframe</label> <label className="form-check-label" htmlFor="iframe">Iframe</label>
</div> </div>
<div className="radio"> <div className="radio">
...@@ -133,8 +155,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa ...@@ -133,8 +155,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa
value="ws" value="ws"
id="ws" id="ws"
data-id='localPluginRadioButtonws' data-id='localPluginRadioButtonws'
checked={plugin.type === 'ws'} checked={type === 'ws'}
onChange={(e) => changeHandler('type', e.target.value)} /> onChange={(e) => setType(e.target.value as 'iframe' | 'ws')} />
<label className="form-check-label" htmlFor="ws">Websocket</label> <label className="form-check-label" htmlFor="ws">Websocket</label>
</div> </div>
</div> </div>
...@@ -148,8 +170,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa ...@@ -148,8 +170,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa
value="sidePanel" value="sidePanel"
id="sidePanel" id="sidePanel"
data-id='localPluginRadioButtonsidePanel' data-id='localPluginRadioButtonsidePanel'
checked={plugin.location === 'sidePanel'} checked={location === 'sidePanel'}
onChange={(e) => changeHandler('location', e.target.value)} /> onChange={(e) => setLocation(e.target.value as 'sidePanel' | 'mainPanel' | 'none')} />
<label className="form-check-label" htmlFor="sidePanel">Side Panel</label> <label className="form-check-label" htmlFor="sidePanel">Side Panel</label>
</div> </div>
<div className="radio"> <div className="radio">
...@@ -160,8 +182,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa ...@@ -160,8 +182,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa
value="mainPanel" value="mainPanel"
id="mainPanel" id="mainPanel"
data-id='localPluginRadioButtonmainPanel' data-id='localPluginRadioButtonmainPanel'
checked={plugin.location === 'mainPanel'} checked={location === 'mainPanel'}
onChange={(e) => changeHandler('location', e.target.value)} /> onChange={(e) => setLocation(e.target.value as 'sidePanel' | 'mainPanel' | 'none')} />
<label className="form-check-label" htmlFor="mainPanel">Main Panel</label> <label className="form-check-label" htmlFor="mainPanel">Main Panel</label>
</div> </div>
<div className="radio"> <div className="radio">
...@@ -172,8 +194,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa ...@@ -172,8 +194,8 @@ function LocalPluginForm ({ changeHandler, plugin, closeModal, visible, pluginMa
value="none" value="none"
id="none" id="none"
data-id='localPluginRadioButtonnone' data-id='localPluginRadioButtonnone'
checked={plugin.location === 'none'} checked={location === 'none'}
onChange={(e) => changeHandler('location', e.target.value)} /> onChange={(e) => setLocation(e.target.value as 'sidePanel' | 'mainPanel' | 'none')} />
<label className="form-check-label" htmlFor="none">None</label> <label className="form-check-label" htmlFor="none">None</label>
</div> </div>
</div> </div>
......
...@@ -5,15 +5,15 @@ import PermisssionsSettings from './permissions/permissionsSettings' ...@@ -5,15 +5,15 @@ import PermisssionsSettings from './permissions/permissionsSettings'
import { Profile } from '@remixproject/plugin-utils' import { Profile } from '@remixproject/plugin-utils'
import LocalPluginForm from './LocalPluginForm' import LocalPluginForm from './LocalPluginForm'
const initialState: FormStateProps = { // const initialState: FormStateProps = {
name: 'test', // name: 'test',
displayName: 'test', // displayName: 'test',
url: '', // url: '',
type: 'iframe', // type: 'iframe',
hash: '', // hash: '',
methods: 'test', // methods: 'test',
location: 'sidePanel' // location: 'sidePanel'
} // }
interface RootViewProps { interface RootViewProps {
pluginComponent: PluginManagerComponent pluginComponent: PluginManagerComponent
...@@ -35,13 +35,8 @@ function RootView ({ pluginComponent, children }: RootViewProps) { ...@@ -35,13 +35,8 @@ function RootView ({ pluginComponent, children }: RootViewProps) {
* Component Local State declaration * Component Local State declaration
*/ */
const [visible, setVisible] = useState<boolean>(true) const [visible, setVisible] = useState<boolean>(true)
const [plugin, setPlugin] = useState<FormStateProps>(initialState)
const [filterPlugins, setFilterPlugin] = useState<string>('') const [filterPlugins, setFilterPlugin] = useState<string>('')
function pluginChangeHandler<P extends keyof FormStateProps> (formProps: P, value: FormStateProps[P]) {
setPlugin({ ...plugin, [formProps]: value })
}
/** /**
* Modal Visibility States * Modal Visibility States
*/ */
...@@ -77,9 +72,7 @@ function RootView ({ pluginComponent, children }: RootViewProps) { ...@@ -77,9 +72,7 @@ function RootView ({ pluginComponent, children }: RootViewProps) {
</div> </div>
<LocalPluginForm <LocalPluginForm
closeModal={closeModal} closeModal={closeModal}
changeHandler={pluginChangeHandler}
visible={visible} visible={visible}
plugin={plugin}
pluginManager={pluginComponent} pluginManager={pluginComponent}
/> />
</Fragment> </Fragment>
......
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