Files
uPortal2/frontend/src/pages/PlanMeetingPage.jsx
T
2026-10-25 10:11:32 +03:00

151 lines
4.9 KiB
React

import React, { useEffect, useState } from 'react'
import { api } from '../api.js'
import { notifyTelegramEnabled, prefillLastRoomDisabled } from '../settings.js'
// Formats a Date as the value a `datetime-local` input expects, in local time
// (YYYY-MM-DDTHH:mm).
function toLocalInputValue(date) {
const pad = (n) => String(n).padStart(2, '0')
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}`
}
// Dedicated page for planning a meeting. Kept separate from the meetings list
// so creating a meeting is a deliberate, focused flow. On success the caller
// is expected to navigate back to the meetings list.
export default function PlanMeetingPage({ onCreated, onCancel }) {
const [title, setTitle] = useState('')
const [intent, setIntent] = useState('')
const [room, setRoom] = useState('')
// Prefill with the current date/time so the field is never empty by default.
const [scheduledAt, setScheduledAt] = useState(() => toLocalInputValue(new Date()))
// When checked the author is recorded as joining straight away. Checked by
// default, since planning a meeting usually means you intend to attend.
const [autoJoin, setAutoJoin] = useState(true)
// When checked the Telegram bot announces the meeting. Off by default; the
// profile's device-local preference decides whether it starts checked.
const [hasNotification, setHasNotification] = useState(notifyTelegramEnabled)
const [saving, setSaving] = useState(false)
const [error, setError] = useState('')
// Offer the most recently joined room as the default unless the device-local
// preference disables it. The recent-rooms list is ordered newest first, and
// the room field is only filled while it is still empty so a manual edit is
// preserved.
useEffect(() => {
if (prefillLastRoomDisabled()) return
let active = true
api
.listRooms()
.then((rooms) => {
const last = rooms?.[0]?.roomName
if (active && last) setRoom((current) => current || last)
})
.catch(() => {
// Prefilling is best-effort; leave the field empty on failure.
})
return () => {
active = false
}
}, [])
const create = async (event) => {
event.preventDefault()
setError('')
if (!title.trim() || !scheduledAt) {
setError('A title and a date/time are required.')
return
}
setSaving(true)
try {
await api.createMeeting({
title,
intent,
room,
// datetime-local is local time; toISOString() produces the RFC3339 UTC
// instant the backend expects.
scheduledAt: new Date(scheduledAt).toISOString(),
autoJoin,
hasNotification,
})
onCreated?.()
} catch (err) {
setError(err.message)
setSaving(false)
}
}
return (
<section className="card plan-meeting">
<div className="card-head">
<h2>Plan a meeting</h2>
<button className="btn-ghost" type="button" onClick={() => onCancel?.()}>
Cancel
</button>
</div>
<form onSubmit={create}>
<label>
Title
<input
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Weekly sync"
/>
</label>
<label>
What is it about
<textarea
className="meeting-intent-field"
rows={6}
value={intent}
onChange={(e) => setIntent(e.target.value)}
placeholder="Discuss the roadmap"
/>
</label>
<label>
Room
<input
value={room}
onChange={(e) => setRoom(e.target.value)}
placeholder="weekly-sync"
/>
</label>
<label>
When
<input
type="datetime-local"
value={scheduledAt}
onChange={(e) => setScheduledAt(e.target.value)}
/>
</label>
<label className="checkbox">
<input
type="checkbox"
checked={autoJoin}
onChange={(e) => setAutoJoin(e.target.checked)}
/>
I will join this meeting
</label>
<label className="checkbox">
<input
type="checkbox"
checked={hasNotification}
onChange={(e) => setHasNotification(e.target.checked)}
/>
Send a Telegram notification
</label>
<button className="btn-primary" type="submit" disabled={saving}>
{saving ? 'Creating…' : 'Create meeting'}
</button>
{error && <p className="error">{error}</p>}
<p className="hint">
The meeting is visible to every registered user, who can vote whether
they will join. Past meetings are removed 7 days after they occur.
</p>
</form>
</section>
)
}