import React, { useState } from 'react'; import { transform } from '../../../../../services/transformer/notificationAdapterTransformer'; import { xhrPost } from '../../../../../services/xhr'; import Help from './NotificationHelpDisplay'; import { useSelector } from 'react-redux'; import { Banner, Button, Form, Modal, Select, Switch } from '@douyinfe/semi-ui'; import './NotificationAdapterMutator.less'; const sortAdapter = (a, b) => { if (a.name < b.name) { return -1; } if (a.name > b.name) { return 1; } return 0; }; const validate = (selectedAdapter) => { const results = []; for (let uiElement of Object.values(selectedAdapter.fields || [])) { if (uiElement.value == null) { results.push('All fields are mandatory and must be set.'); continue; } if (uiElement.type === 'number') { const numberValue = parseFloat(uiElement.value); if(isNaN(numberValue) || numberValue < 0) { results.push('A number field cannot contain anything else and must be > 0.'); continue; } } if (uiElement.type === 'boolean' && typeof uiElement.value !== 'boolean') { results.push('A boolean field cannot be of a different type.'); continue; } if (typeof uiElement.value === 'string' && uiElement.value.length === 0) { results.push('All fields are mandatory and must be set.'); } } return [...new Set(results)]; }; function spreadPrefilledAdapterWithValues(prefilled, fields) { if (prefilled != null && fields != null) { Object.keys(fields).forEach((fieldKey) => { prefilled.fields[fieldKey].value = fields[fieldKey]; }); } } export default function NotificationAdapterMutator({ onVisibilityChanged, visible = false, selected = [], editNotificationAdapter, onData, } = {}) { const adapter = useSelector((state) => state.notificationAdapter); const preFilledSelectedAdapter = editNotificationAdapter == null ? null : adapter.find((a) => a.id === editNotificationAdapter.id); spreadPrefilledAdapterWithValues(preFilledSelectedAdapter, editNotificationAdapter?.fields); const [selectedAdapter, setSelectedAdapter] = useState(preFilledSelectedAdapter); const [validationMessage, setValidationMessage] = useState(null); const [successMessage, setSuccessMessage] = useState(null); const onSubmit = (doStore) => { if (doStore) { const validationResults = validate(selectedAdapter); if (validationResults.length > 0) { setValidationMessage(validationResults.join('
')); return; } onData( transform({ id: selectedAdapter.id, name: selectedAdapter.name, fields: selectedAdapter.fields || {}, }) ); setSelectedAdapter(null); onVisibilityChanged(false); } else { setSelectedAdapter(null); onVisibilityChanged(false); } }; const onTry = () => { setValidationMessage(null); setSuccessMessage(null); const validationResults = validate(selectedAdapter); if (validationResults.length > 0) { setValidationMessage(validationResults.join('
')); return; } xhrPost('/api/jobs/notificationAdapter/try', { id: selectedAdapter.id, fields: { ...selectedAdapter.fields, }, }) .then(() => { setSuccessMessage('It seems like it worked! Please check your service.'); }) .catch((error) => setValidationMessage(`This did not work :-( I've received the following error: ${error.json.message}`) ); }; const setValue = (selectedAdapter, uiElement, key, value) => { uiElement.value = value; setSelectedAdapter({ ...selectedAdapter, fields: { ...selectedAdapter.fields, [key]: { ...selectedAdapter.fields[key], value, }, }, }); }; const getFieldsFor = (selectedAdapter_) => { const selectedAdapter = Object.assign({}, selectedAdapter_); return Object.keys(selectedAdapter.fields || []).map((key) => { const uiElement = selectedAdapter.fields[key]; return (
{uiElement.type === 'boolean' ? ( { setValue(selectedAdapter, uiElement, key, checked); }} /> ) : ( { setValue(selectedAdapter, uiElement, key, value); }} /> )} ); }); }; return ( } > {validationMessage != null && ( Error} style={{ marginBottom: '1rem' }} description={

} /> )} {successMessage != null && ( Yay!} style={{ marginBottom: '1rem' }} description={

} /> )}

When Fredy found new listings, we like to report them to you. To do so, notification adapter can be configured.{' '}
There are multiple ways how Fredy can send new listings to you. Chose your weapon...