diff --git a/App.tsx b/App.tsx index 86a21ae..bc59c49 100644 --- a/App.tsx +++ b/App.tsx @@ -98,8 +98,8 @@ export default function App(): ReactElement { export type RootStackParamList = { Home: undefined; - Room: { id: number }; - CreateRoom: undefined; + Room: { roomid: string; endpoint: string }; + CreateRoom: { endpoint: string }; RoomList: undefined; EditDeck: undefined; Preferences: undefined; diff --git a/src/screens/create-room/index.tsx b/src/screens/create-room/index.tsx index b3c4cb9..432716c 100644 --- a/src/screens/create-room/index.tsx +++ b/src/screens/create-room/index.tsx @@ -1,44 +1,54 @@ -import React, { ReactElement } from "react"; -import { - StyleSheet, - View, - Text, - Button, - ScrollView, - TextInput, -} from "react-native"; +import React, { ReactElement, useEffect, useRef } from "react"; +import { StyleSheet, View, Text } from "react-native"; +import { Button, Input } from "react-native-elements"; import { StackNavigationProp } from "@react-navigation/stack"; +import { RouteProp } from "@react-navigation/native"; import { RootStackParamList } from "../../../App"; -import { gql, useMutation } from "@apollo/client"; import { Formik } from "formik"; import * as Yup from "yup"; -const CREATE_ROOM = gql` - mutation CreateRoom($name: String!, $player: String!) { - createRoom(name: $name, player: $player) { - id - name - players - } - } -`; - export default function CreateRoomScreen({ + route, navigation, }: { + route: CreateRoomScreenRouteProp; navigation: CreateRoomScreenNavigationProp; }): ReactElement { - const [createRoom] = useMutation(CREATE_ROOM, { - onCompleted: (data) => { - console.log(data.createRoom.id); - navigation.navigate("Room", { id: data.createRoom.id }); - }, - }); + // const [createRoom] = useMutation(CREATE_ROOM, { + // onCompleted: (data) => { + // console.log(data.createRoom.id); + // navigation.navigate("Room", { id: data.createRoom.id }); + // }, + // }); + const { endpoint } = route.params; + const websocket = useRef(null); + + useEffect(() => { + websocket.current = new WebSocket(`ws://${endpoint}/ws`); + websocket.current.onmessage = (event) => { + if (event.data.startsWith("{")) { + const json = JSON.parse(event.data); + if (json.status === "ok") { + navigation.navigate("Room", { + roomid: json.data.id, + endpoint: endpoint, + }); + } + } + }; + return () => { + if (websocket.current != null) { + websocket.current.close(); + } + }; + }, []); const onSubmit = async (values: { name: string }) => { // データ送信 console.log(values); - createRoom({ variables: { name: values.name, player: "piypiyo" } }); + if (websocket.current != null) { + websocket.current.send(`/create ${values.name}`); + } }; const schema = Yup.object().shape({ @@ -48,51 +58,50 @@ export default function CreateRoomScreen({ .required("ルーム名を入力してください"), }); return ( - - - onSubmit(values)} - > - {({ - handleSubmit, - handleChange, - handleBlur, - isValid, - isSubmitting, - values, - errors, - touched, - }) => ( - <> - - {errors.name && touched.name ? ( - {errors.name} - ) : null} - - -