0
owncast/web/pages/components/config/edit-social-links.tsx

297 lines
8.2 KiB
TypeScript
Raw Normal View History

2021-01-19 10:34:06 -08:00
import React, { useState, useContext, useEffect } from 'react';
2021-01-24 01:22:28 -08:00
import { Typography, Table, Button, Modal, Input } from 'antd';
2021-01-23 20:16:01 -08:00
import { ColumnsType } from 'antd/lib/table';
import { DeleteOutlined } from '@ant-design/icons';
import SocialDropdown from './social-icons-dropdown';
import { fetchData, NEXT_PUBLIC_API_HOST, SOCIAL_PLATFORMS_LIST } from '../../../utils/apis';
import { ServerStatusContext } from '../../../utils/server-status-context';
import { API_SOCIAL_HANDLES, postConfigUpdateToAPI, RESET_TIMEOUT, SUCCESS_STATES, DEFAULT_SOCIAL_HANDLE, OTHER_SOCIAL_HANDLE_OPTION } from './constants';
import { SocialHandle } from '../../../types/config-section';
2021-01-27 10:31:53 -08:00
import { isValidUrl } from '../../../utils/urls';
import configStyles from '../../../styles/config-pages.module.scss';
2021-01-19 10:34:06 -08:00
const { Title } = Typography;
export default function EditSocialLinks() {
2021-01-19 10:34:06 -08:00
const [availableIconsList, setAvailableIconsList] = useState([]);
const [currentSocialHandles, setCurrentSocialHandles] = useState([]);
2021-01-23 20:16:01 -08:00
const [displayModal, setDisplayModal] = useState(false);
2021-01-24 01:22:28 -08:00
const [displayOther, setDisplayOther] = useState(false);
2021-01-23 20:16:01 -08:00
const [modalProcessing, setModalProcessing] = useState(false);
2021-01-24 01:22:28 -08:00
const [editId, setEditId] = useState(-1);
2021-01-23 20:16:01 -08:00
// current data inside modal
const [modalDataState, setModalDataState] = useState(DEFAULT_SOCIAL_HANDLE);
const [submitStatus, setSubmitStatus] = useState(null);
const [submitStatusMessage, setSubmitStatusMessage] = useState('');
2021-01-19 10:34:06 -08:00
const serverStatusData = useContext(ServerStatusContext);
const { serverConfig, setFieldInConfigState } = serverStatusData || {};
const { instanceDetails } = serverConfig;
const { socialHandles: initialSocialHandles } = instanceDetails;
2021-01-23 20:16:01 -08:00
let resetTimer = null;
2021-01-19 10:34:06 -08:00
const getAvailableIcons = async () => {
try {
const result = await fetchData(SOCIAL_PLATFORMS_LIST, { auth: false });
const list = Object.keys(result).map(item => ({
key: item,
...result[item],
}));
setAvailableIconsList(list);
} catch (error) {
console.log(error)
// do nothing
}
};
2021-01-24 01:22:28 -08:00
const selectedOther = modalDataState.platform !== '' && !availableIconsList.find(item => item.key === modalDataState.platform);
2021-01-19 10:34:06 -08:00
useEffect(() => {
getAvailableIcons();
}, []);
useEffect(() => {
2021-01-24 01:22:28 -08:00
if (instanceDetails.socialHandles) {
setCurrentSocialHandles(initialSocialHandles);
}
2021-01-19 10:34:06 -08:00
}, [instanceDetails]);
2021-01-23 20:16:01 -08:00
const resetStates = () => {
setSubmitStatus(null);
setSubmitStatusMessage('');
resetTimer = null;
clearTimeout(resetTimer);
2021-01-24 01:22:28 -08:00
};
const resetModal = () => {
2021-01-23 20:16:01 -08:00
setDisplayModal(false);
setEditId(-1);
2021-01-24 01:22:28 -08:00
setDisplayOther(false);
setModalProcessing(false);
setModalDataState({...DEFAULT_SOCIAL_HANDLE});
};
const handleModalCancel = () => {
resetModal();
};
const updateModalState = (fieldName: string, value: string) => {
setModalDataState({
...modalDataState,
[fieldName]: value,
});
};
const handleDropdownSelect = (value: string) => {
if (value === OTHER_SOCIAL_HANDLE_OPTION) {
setDisplayOther(true);
updateModalState('platform', '');
} else {
setDisplayOther(false);
updateModalState('platform', value);
}
};
const handleOtherNameChange = event => {
const { value } = event.target;
updateModalState('platform', value);
};
const handleUrlChange = event => {
const { value } = event.target;
updateModalState('url', value);
};
2021-01-23 20:16:01 -08:00
// posts all the variants at once as an array obj
const postUpdateToAPI = async (postValue: any) => {
await postConfigUpdateToAPI({
apiPath: API_SOCIAL_HANDLES,
data: { value: postValue },
onSuccess: () => {
2021-01-24 01:22:28 -08:00
setFieldInConfigState({ fieldName: 'socialHandles', value: postValue, path: 'instanceDetails' });
2021-01-23 20:16:01 -08:00
// close modal
setModalProcessing(false);
handleModalCancel();
setSubmitStatus('success');
2021-01-24 01:22:28 -08:00
setSubmitStatusMessage('Social Handles updated.');
2021-01-23 20:16:01 -08:00
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
},
onError: (message: string) => {
setSubmitStatus('error');
setSubmitStatusMessage(message);
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
},
});
};
// on Ok, send all of dataState to api
// show loading
// close modal when api is done
const handleModalOk = () => {
2021-01-24 01:22:28 -08:00
setModalProcessing(true);
const postData = currentSocialHandles.length ? [
2021-01-23 20:16:01 -08:00
...currentSocialHandles,
2021-01-24 01:22:28 -08:00
]: [];
2021-01-23 20:16:01 -08:00
if (editId === -1) {
postData.push(modalDataState);
} else {
postData.splice(editId, 1, modalDataState);
}
postUpdateToAPI(postData);
};
2021-01-24 01:22:28 -08:00
const handleDeleteItem = index => {
2021-01-23 20:16:01 -08:00
const postData = [
...currentSocialHandles,
];
postData.splice(index, 1);
2021-01-24 01:22:28 -08:00
postUpdateToAPI(postData);
2021-01-23 20:16:01 -08:00
};
const socialHandlesColumns: ColumnsType<SocialHandle> = [
{
title: "#",
dataIndex: "key",
key: "key"
},
{
title: "Platform",
dataIndex: "platform",
key: "platform",
render: (platform: string) => {
2021-01-24 01:22:28 -08:00
const platformInfo = availableIconsList.find(item => item.key === platform);
2021-01-23 20:16:01 -08:00
if (!platformInfo) {
return platform;
}
const { icon, platform: platformName } = platformInfo;
return (
<>
<span className="option-icon">
<img src={`${NEXT_PUBLIC_API_HOST}${icon}`} alt="" className="option-icon" />
</span>
<span className="option-label">{platformName}</span>
</>
);
},
},
{
2021-01-24 01:22:28 -08:00
title: "Url Link",
2021-01-23 20:16:01 -08:00
dataIndex: "url",
key: "url",
},
{
title: '',
dataIndex: '',
key: 'edit',
render: (data, record, index) => {
return (
<span className="actions">
<Button type="primary" size="small" onClick={() => {
setEditId(index);
2021-01-24 01:22:28 -08:00
setModalDataState({...currentSocialHandles[index]});
2021-01-23 20:16:01 -08:00
setDisplayModal(true);
}}>
Edit
</Button>
<Button
className="delete-button"
icon={<DeleteOutlined />}
size="small"
2021-01-24 01:22:28 -08:00
onClick={() => handleDeleteItem(index)}
2021-01-23 20:16:01 -08:00
/>
</span>
)},
},
];
const {
icon: newStatusIcon = null,
message: newStatusMessage = '',
} = SUCCESS_STATES[submitStatus] || {};
const statusMessage = (
<div className={`status-message ${submitStatus || ''}`}>
{newStatusIcon} {newStatusMessage} {submitStatusMessage}
</div>
);
2021-01-19 10:34:06 -08:00
const okButtonProps = {
disabled: !isValidUrl(modalDataState.url)
};
2021-01-19 10:34:06 -08:00
return (
<div className={configStyles.socialLinksEditor}>
2021-01-19 10:34:06 -08:00
<Title level={2}>Social Links</Title>
<p>Add all your social media handles and links to your other profiles here.</p>
2021-01-23 20:16:01 -08:00
{statusMessage}
<Table
className="dataTable"
2021-01-23 20:16:01 -08:00
pagination={false}
size="small"
2021-01-24 01:22:28 -08:00
rowKey={record => record.url}
2021-01-23 20:16:01 -08:00
columns={socialHandlesColumns}
dataSource={currentSocialHandles}
/>
<Modal
title="Edit Social Handle"
visible={displayModal}
onOk={handleModalOk}
onCancel={handleModalCancel}
confirmLoading={modalProcessing}
okButtonProps={okButtonProps}
2021-01-23 20:16:01 -08:00
>
2021-01-24 01:22:28 -08:00
<SocialDropdown
iconList={availableIconsList}
selectedOption={selectedOther ? OTHER_SOCIAL_HANDLE_OPTION : modalDataState.platform}
onSelected={handleDropdownSelect}
/>
{
displayOther
? (
<>
<Input
placeholder="Other"
defaultValue={modalDataState.platform}
onChange={handleOtherNameChange}
/>
<br/>
</>
) : null
}
<br/>
URL
<Input
placeholder="Url to page"
defaultValue={modalDataState.url}
value={modalDataState.url}
onChange={handleUrlChange}
/>
2021-01-23 20:16:01 -08:00
{statusMessage}
</Modal>
<br />
<Button type="primary" onClick={() => {
2021-01-24 01:22:28 -08:00
resetModal();
2021-01-23 20:16:01 -08:00
setDisplayModal(true);
}}>
2021-01-24 01:22:28 -08:00
Add a new social link
2021-01-23 20:16:01 -08:00
</Button>
2021-01-19 10:34:06 -08:00
</div>
);
}