1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-08 23:46:14 +01:00
fab-manager/app/frontend/src/javascript/components/socials/fab-socials.tsx

108 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-05-02 17:54:58 +02:00
import React, { useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { FormInput } from '../form/form-input';
import SettingAPI from '../../api/setting';
2022-05-03 18:05:30 +02:00
import { supportedNetworks } from '../../models/social-network';
2022-05-02 17:54:58 +02:00
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;
2022-05-03 18:05:30 +02:00
interface FabSocialsProps {
show: boolean
}
export const FabSocials: React.FC<FabSocialsProps> = ({ show = false }) => {
2022-05-02 17:54:58 +02:00
const { t } = useTranslation('shared');
2022-05-03 18:05:30 +02:00
const { handleSubmit, register, setValue } = useForm();
2022-05-02 17:54:58 +02:00
2022-05-03 18:05:30 +02:00
const settingsList = supportedNetworks.map(el => `socials_${el}` as SettingName);
2022-05-02 17:54:58 +02:00
const [fabNetworks, setFabNetworks] = useState([]);
2022-05-03 18:05:30 +02:00
const [selectedNetworks, setSelectedNetworks] = useState([]);
2022-05-02 17:54:58 +02:00
useEffect(() => {
2022-05-03 18:05:30 +02:00
SettingAPI.query(settingsList).then(res => {
setFabNetworks(Array.from(res, ([name, url]) => ({ name, url })));
2022-05-02 17:54:58 +02:00
}).catch(error => console.error(error));
}, []);
2022-05-03 18:05:30 +02:00
useEffect(() => {
setSelectedNetworks(fabNetworks.filter(el => el.url !== ''));
}, [fabNetworks]);
const onSubmit = (data) => {
const updatedNetworks = new Map<SettingName, string>();
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) {
console.error(errorResults.map(item => item.error[0]).join(' '));
}
});
};
2022-05-02 17:54:58 +02:00
const selectNetwork = (network) => {
2022-05-03 18:05:30 +02:00
setSelectedNetworks([...selectedNetworks, network]);
2022-05-02 17:54:58 +02:00
};
const remove = (network) => {
2022-05-03 18:05:30 +02:00
setSelectedNetworks(selectedNetworks.filter(el => el !== network));
setValue(network.name, '');
2022-05-02 17:54:58 +02:00
};
return (
2022-05-03 18:05:30 +02:00
<>{show
? <div className='social-icons'>
2022-05-02 17:54:58 +02:00
{fabNetworks.map((network, index) =>
2022-05-03 18:05:30 +02:00
selectedNetworks.includes(network) &&
<a key={index} href={network.url} target='_blank' rel="noreferrer">
<img src={`${Icons}#${network.name.replace('socials_', '')}`}></img>
</a>
2022-05-02 17:54:58 +02:00
)}
</div>
2022-05-03 18:05:30 +02:00
: <form onSubmit={handleSubmit(onSubmit)}>
<div className="social-icons">
{fabNetworks.map((network, index) =>
!selectedNetworks.includes(network) &&
<img key={index} src={`${Icons}#${network.name.replace('socials_', '')}`} onClick={() => selectNetwork(network)}></img>
)}
</div>
{fabNetworks.map((network, index) =>
selectedNetworks.includes(network) &&
<FormInput id={network.name}
key={index}
register={register}
defaultValue={network.url}
label={network.name.replace('socials_', '')}
placeholder={t('app.shared.text_editor.url_placeholder')}
icon={<img src={`${Icons}#${network.name.replace('socials_', '')}`}></img>}
addOn={<Trash size={16} />}
addOnAction={() => remove(network)} />
)}
<FabButton type='submit'
className='btn-warning'>
{t('app.shared.buttons.save')}
</FabButton>
</form>
}</>
2022-05-02 17:54:58 +02:00
);
};
2022-05-03 18:05:30 +02:00
const FabSocialsWrapper: React.FC<FabSocialsProps> = (props) => {
2022-05-02 17:54:58 +02:00
return (
<Loader>
<FabSocials {...props} />
</Loader>
);
};
2022-05-03 18:05:30 +02:00
Application.Components.component('fabSocials', react2angular(FabSocialsWrapper, ['show']));