Merge pull request #345 from robintown/unregistered-join-existing

Fix joining an existing room from UnregisteredView
This commit is contained in:
Robin 2022-05-20 07:52:47 -04:00 committed by GitHub
commit 7d44a1e979
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -43,6 +43,10 @@ export function UnauthenticatedView() {
useInteractiveRegistration(); useInteractiveRegistration();
const { execute, reset, recaptchaId } = useRecaptcha(recaptchaKey); const { execute, reset, recaptchaId } = useRecaptcha(recaptchaKey);
const { modalState, modalProps } = useModalTriggerState();
const [onFinished, setOnFinished] = useState();
const history = useHistory();
const onSubmit = useCallback( const onSubmit = useCallback(
(e) => { (e) => {
e.preventDefault(); e.preventDefault();
@ -63,40 +67,42 @@ export function UnauthenticatedView() {
recaptchaResponse, recaptchaResponse,
true true
); );
const roomIdOrAlias = await createRoom(client, roomName, ptt);
let roomIdOrAlias;
try {
roomIdOrAlias = await createRoom(client, roomName, ptt);
} catch (error) {
if (error.errcode === "M_ROOM_IN_USE") {
setOnFinished(() => () => {
setClient(client, session);
const aliasLocalpart = roomAliasFromRoomName(roomName);
const [, serverName] = client.getUserId().split(":");
history.push(`/room/#${aliasLocalpart}:${serverName}`);
});
setLoading(false);
modalState.open();
return;
} else {
throw error;
}
}
// Only consider the registration successful if we managed to create the room, too // Only consider the registration successful if we managed to create the room, too
setClient(client, session); setClient(client, session);
history.push(`/room/${roomIdOrAlias}`);
if (roomIdOrAlias) {
history.push(`/room/${roomIdOrAlias}`);
}
} }
submit().catch((error) => { submit().catch((error) => {
if (error.errcode === "M_ROOM_IN_USE") { console.error(error);
setExistingRoomId(roomAliasFromRoomName(roomName)); setLoading(false);
setLoading(false); setError(error);
setError(undefined); reset();
modalState.open();
} else {
console.error(error);
setLoading(false);
setError(error);
reset();
}
}); });
}, },
[register, reset, execute] [register, reset, execute, history]
); );
const { modalState, modalProps } = useModalTriggerState();
const [existingRoomId, setExistingRoomId] = useState();
const history = useHistory();
const onJoinExistingRoom = useCallback(() => {
history.push(`/${existingRoomId}`);
}, [history, existingRoomId]);
return ( return (
<> <>
<Header> <Header>
@ -176,7 +182,7 @@ export function UnauthenticatedView() {
</footer> </footer>
</div> </div>
{modalState.isOpen && ( {modalState.isOpen && (
<JoinExistingCallModal onJoin={onJoinExistingRoom} {...modalProps} /> <JoinExistingCallModal onJoin={onFinished} {...modalProps} />
)} )}
</> </>
); );