/* Copyright (C) 2019 Monomax Software Pty Ltd * * This file is part of Dnote. * * Dnote is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Dnote is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Dnote. If not, see . */ import React, { useState } from 'react'; import Flash from './Flash'; import { updateSettings, resetSettings } from '../store/settings/actions'; import { useDispatch, useSelector, useStore } from '../store/hooks'; interface Props {} // isValidURL checks if the given string is a valid URL function isValidURL(url: string): boolean { const a = document.createElement('a'); a.href = url; return a.host && a.host !== window.location.host; } // validateFormState validates the given form state. If any input is // invalid, it throws an error. function validateFormState({ apiUrl, webUrl }) { if (!isValidURL(apiUrl)) { throw new Error('Invalid URL for the API URL'); } if (!isValidURL(webUrl)) { throw new Error('Invalid URL for the web URL'); } } const Settings: React.FunctionComponent = () => { const { settings } = useSelector(state => ({ settings: state.settings })); const store = useStore(); const [apiUrl, setAPIUrl] = useState(settings.apiUrl); const [webUrl, setWebUrl] = useState(settings.webUrl); const [errMsg, setErrMsg] = useState(''); const [successMsg, setSuccessMsg] = useState(''); const dispatch = useDispatch(); function handleRestore() { dispatch(resetSettings()); setSuccessMsg('Restored the default settings'); const { settings: settingsState } = store.getState(); setAPIUrl(settingsState.apiUrl); setWebUrl(settingsState.webUrl); } function handleSubmit(e) { e.preventDefault(); setSuccessMsg(''); setErrMsg(''); try { validateFormState({ apiUrl, webUrl }); } catch (err) { setErrMsg(err.message); return; } dispatch( updateSettings({ apiUrl, webUrl }) ); setSuccessMsg('Succesfully updated the settings.'); } return (

Settings

Customize your Dnote extension

{ setAPIUrl(e.target.value); }} />
{ setWebUrl(e.target.value); }} />
); }; export default Settings;