✨(frontend) create room in db when creating a new room
With unregistered rooms being now forbidden, we need to call create a room in our Django db to get an access token. Clicking on the 'Create room' will create a new entry using a Post request. The output serialized already contains an access token to the LiveKit server, thus we won't need to run the useQuery hook when navigating to the room. /!\ this modification now prevent authenticated users to create rooms by simply navigating to it. It'll be fixed in the upcoming commits.
This commit is contained in:
committed by
aleb_the_flash
parent
b261f2ee5b
commit
fb0ee9e8f6
@@ -8,10 +8,20 @@ import { Centered } from '@/layout/Centered'
|
|||||||
import { generateRoomId } from '@/features/rooms'
|
import { generateRoomId } from '@/features/rooms'
|
||||||
import { authUrl, useUser, UserAware } from '@/features/auth'
|
import { authUrl, useUser, UserAware } from '@/features/auth'
|
||||||
import { JoinMeetingDialog } from '../components/JoinMeetingDialog'
|
import { JoinMeetingDialog } from '../components/JoinMeetingDialog'
|
||||||
|
import { useCreateRoom } from '@/features/rooms'
|
||||||
|
|
||||||
export const Home = () => {
|
export const Home = () => {
|
||||||
const { t } = useTranslation('home')
|
const { t } = useTranslation('home')
|
||||||
const { isLoggedIn } = useUser()
|
const { isLoggedIn } = useUser()
|
||||||
|
|
||||||
|
const { mutateAsync: createRoom } = useCreateRoom({
|
||||||
|
onSuccess: (data) => {
|
||||||
|
navigateTo('room', data.slug, {
|
||||||
|
state: { create: true, initialRoomData: data },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<UserAware>
|
<UserAware>
|
||||||
<Screen>
|
<Screen>
|
||||||
@@ -32,10 +42,10 @@ export const Home = () => {
|
|||||||
variant="primary"
|
variant="primary"
|
||||||
onPress={
|
onPress={
|
||||||
isLoggedIn
|
isLoggedIn
|
||||||
? () =>
|
? async () => {
|
||||||
navigateTo('room', generateRoomId(), {
|
const slug = generateRoomId()
|
||||||
state: { create: true },
|
await createRoom({slug})
|
||||||
})
|
}
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
href={isLoggedIn ? undefined : authUrl()}
|
href={isLoggedIn ? undefined : authUrl()}
|
||||||
|
|||||||
@@ -8,4 +8,7 @@ export type ApiRoom = {
|
|||||||
room: string
|
room: string
|
||||||
token: string
|
token: string
|
||||||
}
|
}
|
||||||
|
configuration?: {
|
||||||
|
[key: string]: string | number | boolean;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
25
src/frontend/src/features/rooms/api/createRoom.ts
Normal file
25
src/frontend/src/features/rooms/api/createRoom.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { useMutation, UseMutationOptions } from "@tanstack/react-query";
|
||||||
|
import { fetchApi } from '@/api/fetchApi';
|
||||||
|
import { ApiError } from "@/api/ApiError";
|
||||||
|
import { ApiRoom } from "./ApiRoom";
|
||||||
|
|
||||||
|
export interface CreateRoomParams {
|
||||||
|
slug: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const createRoom = ({slug}: CreateRoomParams): Promise<ApiRoom> => {
|
||||||
|
return fetchApi(`rooms/`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({
|
||||||
|
name: slug,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export function useCreateRoom(options?: UseMutationOptions<ApiRoom, ApiError, CreateRoomParams>) {
|
||||||
|
return useMutation<ApiRoom, ApiError, CreateRoomParams>({
|
||||||
|
mutationFn: createRoom,
|
||||||
|
onSuccess: options?.onSuccess,
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -11,24 +11,30 @@ import { navigateTo } from '@/navigation/navigateTo'
|
|||||||
import { Screen } from '@/layout/Screen'
|
import { Screen } from '@/layout/Screen'
|
||||||
import { QueryAware } from '@/components/QueryAware'
|
import { QueryAware } from '@/components/QueryAware'
|
||||||
import { fetchRoom } from '../api/fetchRoom'
|
import { fetchRoom } from '../api/fetchRoom'
|
||||||
|
import { ApiRoom } from '../api/ApiRoom'
|
||||||
import { InviteDialog } from './InviteDialog'
|
import { InviteDialog } from './InviteDialog'
|
||||||
|
|
||||||
export const Conference = ({
|
export const Conference = ({
|
||||||
roomId,
|
roomId,
|
||||||
userConfig,
|
userConfig,
|
||||||
|
initialRoomData,
|
||||||
mode = 'join',
|
mode = 'join',
|
||||||
}: {
|
}: {
|
||||||
roomId: string
|
roomId: string
|
||||||
userConfig: LocalUserChoices
|
userConfig: LocalUserChoices
|
||||||
mode?: 'join' | 'create'
|
mode?: 'join' | 'create'
|
||||||
|
initialRoomData?: ApiRoom
|
||||||
}) => {
|
}) => {
|
||||||
const { status, data } = useQuery({
|
const { status, data } = useQuery({
|
||||||
queryKey: [keys.room, roomId, userConfig.username],
|
queryKey: [keys.room, roomId, userConfig.username],
|
||||||
|
enabled: !initialRoomData,
|
||||||
|
initialData: initialRoomData,
|
||||||
queryFn: () =>
|
queryFn: () =>
|
||||||
fetchRoom({
|
fetchRoom({
|
||||||
roomId: roomId as string,
|
roomId: roomId as string,
|
||||||
username: userConfig.username,
|
username: userConfig.username,
|
||||||
}),
|
}),
|
||||||
|
retry: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
const roomOptions = useMemo((): RoomOptions => {
|
const roomOptions = useMemo((): RoomOptions => {
|
||||||
|
|||||||
@@ -2,3 +2,4 @@ export { Room as RoomRoute } from './routes/Room'
|
|||||||
export { FeedbackRoute } from './routes/Feedback'
|
export { FeedbackRoute } from './routes/Feedback'
|
||||||
export { roomIdPattern, isRoomValid } from './utils/isRoomValid'
|
export { roomIdPattern, isRoomValid } from './utils/isRoomValid'
|
||||||
export { generateRoomId } from './utils/generateRoomId'
|
export { generateRoomId } from './utils/generateRoomId'
|
||||||
|
export { useCreateRoom } from './api/createRoom'
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ export const Room = () => {
|
|||||||
const [userConfig, setUserConfig] = useState<LocalUserChoices | null>(null)
|
const [userConfig, setUserConfig] = useState<LocalUserChoices | null>(null)
|
||||||
|
|
||||||
const { roomId } = useParams()
|
const { roomId } = useParams()
|
||||||
|
const initialRoomData = history.state?.initialRoomData
|
||||||
const mode = isLoggedIn && history.state?.create ? 'create' : 'join'
|
const mode = isLoggedIn && history.state?.create ? 'create' : 'join'
|
||||||
const skipJoinScreen = isLoggedIn && mode === 'create'
|
const skipJoinScreen = isLoggedIn && mode === 'create'
|
||||||
|
|
||||||
@@ -33,6 +34,7 @@ export const Room = () => {
|
|||||||
return (
|
return (
|
||||||
<UserAware>
|
<UserAware>
|
||||||
<Conference
|
<Conference
|
||||||
|
initialRoomData={initialRoomData}
|
||||||
roomId={roomId}
|
roomId={roomId}
|
||||||
mode={mode}
|
mode={mode}
|
||||||
userConfig={{
|
userConfig={{
|
||||||
|
|||||||
Reference in New Issue
Block a user