cloud_bbb/ts/Manager/RoomRow.tsx

105 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-04-26 11:36:41 +02:00
import React, { useState } from 'react';
import {CopyToClipboard} from 'react-copy-to-clipboard';
import { SubmitInput } from './SubmitInput';
import { Room, api } from './Api';
type Props = {
2020-04-26 22:41:06 +02:00
room: Room;
updateRoom: (room: Room) => void;
deleteRoom: (id: number) => void;
2020-04-26 11:36:41 +02:00
}
type EditableValueProps = {
2020-04-26 22:41:06 +02:00
setValue: (key: string, value: string|number) => void;
setActive: (key: string) => void;
active: string;
field: string;
value: string;
type: 'text' | 'number';
2020-04-26 11:36:41 +02:00
}
const EditableValue: React.FC<EditableValueProps> = ({ setValue, setActive, active, field, value, type }) => {
if (active === field) {
return <SubmitInput
autoFocus={true}
onSubmitValue={(value) => setValue(field, type === 'number' ? parseInt(value):value)}
onClick={event => event.stopPropagation()}
initialValue={value}
type={type}
2020-04-27 16:50:45 +02:00
/>;
}
2020-04-26 11:36:41 +02:00
function onClick(ev) {
ev.stopPropagation();
2020-04-26 11:36:41 +02:00
setActive(field);
}
2020-04-26 11:36:41 +02:00
return <a className="action-rename" onClick={onClick}>{value}</a>;
2020-04-27 16:50:45 +02:00
};
2020-04-26 11:36:41 +02:00
const RoomRow: React.FC<Props> = (props) => {
const [activeEdit, setActiveEdit] = useState('');
const room = props.room;
2020-04-26 11:36:41 +02:00
function updateRoom(key: string, value: string|boolean|number) {
props.updateRoom({
...props.room,
[key]: value,
});
2020-04-26 11:36:41 +02:00
setActiveEdit('');
}
2020-04-26 11:36:41 +02:00
function deleteRow(ev: MouseEvent) {
ev.preventDefault();
2020-04-26 11:36:41 +02:00
OC.dialogs.confirm(
t('bbb', 'Are you sure you want to delete "{name}"? This operation can not be undone', { name: room.name }),
t('bbb', 'Delete "{name}"?', { name: room.name }),
confirmed => {
if (confirmed) {
props.deleteRoom(room.id);
}
},
true
);
}
2020-04-26 11:36:41 +02:00
function edit(field: string, type: 'text' | 'number' = 'text'){
2020-04-27 16:50:45 +02:00
return <EditableValue field={field} value={room[field]} active={activeEdit} setActive={setActiveEdit} setValue={updateRoom} type={type} />;
}
2020-04-26 11:36:41 +02:00
return (
<tr key={room.id}>
<td className="share icon-col">
<CopyToClipboard text={window.location.origin + api.getUrl(`b/${room.uid}`)}>
<span className="icon icon-clippy icon-visible copy-to-clipboard" ></span>
</CopyToClipboard>
</td>
<td className="start icon-col">
<a href={api.getUrl(`b/${room.uid}`)} className="icon icon-play icon-visible" target="_blank" rel="noopener noreferrer"></a>
</td>
<td className="name">
{edit('name')}
</td>
<td className="welcome">
{edit('welcome')}
</td>
<td className="max-participants">
{edit('maxParticipants', 'number')}
</td>
<td className="record">
<input id={`bbb-record-${room.id}`} type="checkbox" className="checkbox" checked={room.record} onChange={(event) => updateRoom('record', event.target.checked)} />
<label htmlFor={`bbb-record-${room.id}`}></label>
</td>
<td className="remove icon-col">
<a className="icon icon-delete icon-visible"
onClick={deleteRow as any}
title="Delete" />
</td>
</tr>
2020-04-27 16:50:45 +02:00
);
};
2020-04-26 11:36:41 +02:00
export default RoomRow;