Skip to content

Commit

Permalink
refactor: drinker interface
Browse files Browse the repository at this point in the history
id -> glassId (string -> number)
  • Loading branch information
Charmull committed Nov 11, 2022
1 parent ae1f913 commit 88517cd
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/auth/AuthPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const AuthPage = () => {
["post/login"],
async () => {
const { data } = await customAxios.post(
"/dispenser/login",
"/login",
{},
{
headers: { dispenserToken: `${dispenserToken}` },
Expand Down
5 changes: 4 additions & 1 deletion src/drink/components/DrinkersStates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const DrinksStates = ({ drinkers }: { drinkers: Drinker[] }) => {
<span className="text-2xl">현재 상태</span>
{drinkers.map((registeredUser: Drinker) => {
return (
<DrinkerState key={registeredUser.id} {...registeredUser} />
<DrinkerState
key={registeredUser.glassId}
{...registeredUser}
/>
);
})}
</div>
Expand Down
35 changes: 18 additions & 17 deletions src/main/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ const MainPage = () => {

const startClickHandler = () => {
// console.log(ws);
if (ws.current?.readyState === 1 && ws.current != null) {
console.log("send!!");
ws.current?.send(`startDispenser:${"dispenser01"}`);
console.log("sent!!");
// if (ws.current?.readyState === 1 && ws.current != null) {
// console.log("send!!");
// ws.current?.send(`startDispenser:${"dispenser01"}`);
// console.log("sent!!");

// ws.current.onmessage = (msg: MessageEvent) => {
// console.log(msg);
// switch (msg.data) {
// case "술자리가 시작되었습니다!":
// // native 연결
// console.log("페이지 변화");
// navigate("/main");
// break;
// case "dispenser01에 술잔이 등록되었습니다":
// break;
// }
// };
}
// // ws.current.onmessage = (msg: MessageEvent) => {
// // console.log(msg);
// // switch (msg.data) {
// // case "술자리가 시작되었습니다!":
// // // native 연결
// // console.log("페이지 변화");
// // navigate("/main");
// // break;
// // case "dispenser01에 술잔이 등록되었습니다":
// // break;
// // }
// // };
// }
navigate("/main");
};

return (
Expand Down
5 changes: 3 additions & 2 deletions src/main/components/RegisteredUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { BiEditAlt } from "react-icons/bi";
import MainEdit from "../../mainEdit/MainEdit";

const RegisteredUser = ({
id,
glassId,
totalCapacity,
drinkerName,
detail,
}: {
id: string;
glassId: number;
totalCapacity: number;
drinkerName: string;
detail: string;
}) => {
const id = String(glassId);
return (
<div className="card shadow-md">
<div className="card-body flex flex-row p-3">
Expand Down
2 changes: 1 addition & 1 deletion src/main/components/RegisteredUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const RegisteredUsers = ({ drinkers }: { drinkers: Drinker[] }) => {
{drinkers.map((registeredUser: Drinker) => {
return (
<RegisteredUser
key={registeredUser.id}
key={registeredUser.glassId}
{...registeredUser}
/>
);
Expand Down
17 changes: 13 additions & 4 deletions src/main/hooks/useMainViewModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ const useMainViewModel = () => {
// socket
// const socket = new WebSocket("ws://192.168.0.42:8080/ws/socket");
// const socketUrl = "ws://192.168.0.42:8080/ws/socket/glass";
const socketUrl = `${process.env.REACT_APP_SOCKET_ROUTE}/ws/socket/glass`;
const socketUrl = `${process.env.REACT_APP_SOCKET_ROUTE}`;
const ws = useRef<WebSocket | null>(null);
const [socketConnected, setSocketConnected] = useState(false);

const navigate = useNavigate();

// 현재 연결된 drinkers에 대한 정보 가져오기
const { status, data, error, refetch, isFetching } = useQuery(
["get/getglass"],
["get/glass"],
async () => {
const { data } = await customAxios.get("/glass/getglass", {
const { data } = await customAxios.get("/glass", {
headers: authHeader,
});

Expand All @@ -40,19 +40,27 @@ const useMainViewModel = () => {
refetchOnWindowFocus: false,
}
);

useEffect(() => {
ws.current = new WebSocket(socketUrl);
ws.current.onopen = () => {
console.log("open!!");
setSocketConnected(true);
ws.current?.send(
JSON.stringify({
eventType: "drinkerLogin",
dispenserId: "dispenser01",
})
);
};
ws.current.onerror = (message) => {
console.log("error!!");
console.log(message);
};
ws.current.onclose = () => {
ws.current.onclose = (msg) => {
console.log("close!!");
setSocketConnected(false);
console.log(msg);
};
ws.current.onmessage = (msg: MessageEvent) => {
console.log(msg);
Expand All @@ -77,6 +85,7 @@ const useMainViewModel = () => {
return () => {
ws.current?.close();
setSocketConnected(false);
console.log("닫힘");
};
}, []);
// useEffect(() => {
Expand Down
5 changes: 2 additions & 3 deletions src/main/interfaces/drinker.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
interface Drinker {
id: string;
glassId: number;
totalCapacity: number;
currentDrink: number;
drinkerName: string;
detail: string;
// date로 수정할건지...
lastDrinkTimeStamp: string;
lastDrinkTimeStamp: number;
}

export default Drinker;
2 changes: 1 addition & 1 deletion src/mainEdit/hooks/useMainEditModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const useMainEditModel = ({
["patch/updateglass"],
async () => {
const { data: updateData } = await customAxios.patch(
`/glass/updateglass/${id}`,
`/glass/${id}`,
{
totalCapacity: inputCapacity,
drinkerName: inputName,
Expand Down

0 comments on commit 88517cd

Please sign in to comment.