import React, { useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import { FormInput } from '../form/form-input'; import SettingAPI from '../../api/setting'; import { supportedNetworks } from '../../models/social-network'; import { IApplication } from '../../models/application'; import { Loader } from '../base/loader'; import { react2angular } from 'react2angular'; import { SettingName } from '../../models/setting'; import Icons from '../../../../images/social-icons.svg'; import { Trash } from 'phosphor-react'; import { useTranslation } from 'react-i18next'; import { FabButton } from '../base/fab-button'; declare const Application: IApplication; interface FabSocialsProps { show: boolean, onError: (message: string) => void, onSuccess: (message: string) => void } /** * Allows the Fablab to edit its corporate social networks, or to display them read-only to the end users (show=true) */ export const FabSocials: React.FC = ({ show = false, onError, onSuccess }) => { const { t } = useTranslation('shared'); // regular expression to validate the input fields const urlRegex = /^(https?:\/\/)([\da-z.-]+)\.([-a-z\d.]{2,30})([/\w .-]*)*\/?$/; const { handleSubmit, register, setValue, formState } = useForm(); const settingsList = supportedNetworks.map(el => el as SettingName); const [fabNetworks, setFabNetworks] = useState([]); const [selectedNetworks, setSelectedNetworks] = useState([]); useEffect(() => { SettingAPI.query(settingsList).then(res => { setFabNetworks(Array.from(res, ([name, url]) => ({ name, url }))); }).catch(error => console.error(error)); }, []); useEffect(() => { setSelectedNetworks(fabNetworks.filter(el => el.url !== '')); }, [fabNetworks]); /** * Callback triggered when the social networks are saved */ const onSubmit = (data) => { const updatedNetworks = new Map(); Object.keys(data).forEach(key => updatedNetworks.set(key as SettingName, data[key])); SettingAPI.bulkUpdate(updatedNetworks).then(res => { const errorResults = Array.from(res.values()).filter(item => !item.status); if (errorResults.length > 0) { onError(t('app.shared.fab_socials.networks_update_error')); } else { onSuccess(t('app.shared.fab_socials.networks_update_success')); } }); }; /** * Callback triggered when the user adds a network, from the list of available networks, to the editable networks. */ const selectNetwork = (network) => { setSelectedNetworks([...selectedNetworks, network]); }; /** * Callback triggered when the user removes a network, from the list of editables networks, add put it back to the * list of avaiable networks. */ const remove = (network) => { setSelectedNetworks(selectedNetworks.filter(el => el !== network)); setValue(network.name, ''); }; return (
{show ? (selectedNetworks.length > 0) && <>

{t('app.shared.fab_socials.follow_us')}

{fabNetworks.map((network, index) => selectedNetworks.includes(network) && )}
:
{fabNetworks.map((network, index) => !selectedNetworks.includes(network) && selectNetwork(network)}> )}
{selectNetwork.length &&
{fabNetworks.map((network, index) => selectedNetworks.includes(network) && } addOn={} addOnAction={() => remove(network)} /> )}
} {t('app.shared.fab_socials.save')}
}
); }; const FabSocialsWrapper: React.FC = (props) => { return ( ); }; Application.Components.component('fabSocials', react2angular(FabSocialsWrapper, ['show', 'onError', 'onSuccess']));