cloud_bbb/ts/Manager/App.tsx

132 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-04-26 11:36:41 +02:00
import React, { useEffect, useState } from 'react';
import './App.scss';
import RoomRow from './RoomRow';
2020-04-26 22:41:06 +02:00
import { SortArrow } from './SortArrow';
2020-04-26 11:36:41 +02:00
import { api, Room } from './Api';
import NewRoomForm from './NewRoomForm';
export type SortKey = 'name' | 'welcome' | 'maxParticipants' | 'record';
enum SortOrder { DESC = -1, ASC = 1 };
function sortRooms(key: SortKey, orderBy: SortOrder) {
return (a: Room, b: Room) => {
switch (key) {
case 'name':
case 'welcome':
return a[key].localeCompare(b[key]) * orderBy;
case 'maxParticipants':
return (a.maxParticipants - b.maxParticipants) * orderBy;
case 'record':
if (a.record && !b.record) {
return 1 * orderBy;
}
if (!a.record && b.record) {
return -1 * orderBy;
}
2020-04-26 11:36:41 +02:00
return 0;
2020-04-26 11:36:41 +02:00
}
2020-04-27 16:50:45 +02:00
};
2020-04-26 11:36:41 +02:00
}
type Props = {
}
const App: React.FC<Props> = () => {
const [areRoomsLoaded, setRoomsLoaded] = useState(false);
const [rooms, setRooms] = useState<Room[]>([]);
const [orderBy, setOrderBy] = useState<SortKey>('name');
const [sortOrder, setSortOrder] = useState(SortOrder.ASC);
const rows = rooms.sort(sortRooms(orderBy, sortOrder)).map(room => <RoomRow room={room} key={room.id} updateRoom={updateRoom} deleteRoom={deleteRoom} />);
useEffect(() => {
if (areRoomsLoaded) {
return;
}
api.getRooms().then(rooms => {
setRoomsLoaded(true);
setRooms(rooms);
});
}, [areRoomsLoaded]);
function onOrderBy(key: SortKey) {
if (orderBy === key) {
setSortOrder(sortOrder === SortOrder.ASC ? SortOrder.DESC : SortOrder.ASC);
}
setOrderBy(key);
}
function addRoom(name: string) {
if (!name) {
return;
}
api.createRoom(name).then(room => {
setRooms(rooms.concat([room]));
});
}
function updateRoom(room: Room) {
api.updateRoom(room).then(updatedRoom => {
setRooms(rooms.map(room => {
if (room.id === updatedRoom.id) {
return updatedRoom;
}
return room;
}));
});
}
function deleteRoom(id: number) {
api.deleteRoom(id).then(deletedRoom => {
setRooms(rooms.filter(room => room.id !== deletedRoom.id));
});
}
return (
2020-04-26 13:26:34 +02:00
<div id="bbb-react-root"
2020-04-26 11:36:41 +02:00
onClick={() => { /* @TODO hide edit inputs */ }}>
<table>
<thead>
<tr>
<th />
<th />
<th onClick={() => onOrderBy('name')}>
2020-05-16 14:44:21 +02:00
{t('bbb', 'Name')} <SortArrow name='name' value={orderBy} direction={sortOrder} />
2020-04-26 11:36:41 +02:00
</th>
<th onClick={() => onOrderBy('welcome')}>
2020-05-16 14:44:21 +02:00
{t('bbb', 'Welcome')} <SortArrow name='welcome' value={orderBy} direction={sortOrder} />
2020-04-26 11:36:41 +02:00
</th>
<th onClick={() => onOrderBy('maxParticipants')}>
2020-05-16 14:44:21 +02:00
{t('bbb', 'Max')} <SortArrow name='maxParticipants' value={orderBy} direction={sortOrder} />
2020-04-26 11:36:41 +02:00
</th>
<th onClick={() => onOrderBy('record')}>
2020-05-16 14:44:21 +02:00
{t('bbb', 'Record')} <SortArrow name='record' value={orderBy} direction={sortOrder} />
2020-04-26 11:36:41 +02:00
</th>
<th />
</tr>
</thead>
<tbody>
{rows}
</tbody>
<tfoot>
<tr>
<td colSpan={2} />
<td>
<NewRoomForm addRoom={addRoom} />
</td>
<td colSpan={4} />
</tr>
</tfoot>
</table>
</div>
2020-04-27 16:50:45 +02:00
);
};
2020-04-26 11:36:41 +02:00
export default App;