Skip to content

Commit

Permalink
feat: can create and join channels (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
name committed Dec 14, 2022
1 parent 249dcea commit a2c4a6f
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 16 deletions.
3 changes: 0 additions & 3 deletions frontend/components/ChannelList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ export default function ChannelList({ channels }) {
<ListItemButton
key={channel.id}
>
<ListItemAvatar>
<Avatar>P</Avatar>
</ListItemAvatar>
<ListItemText primary={channel.channelName} />
</ListItemButton>
);
Expand Down
24 changes: 21 additions & 3 deletions frontend/components/ChannelPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import axios from 'axios';
import ChannelList from './ChannelList';
import JoinChannelDialog from './JoinChannelDialog';
import CreateChannelDialog from './CreateChannelDialog';
import PasswordDialog from './PasswordDialog';

export default function ChannelPanel() {
const [joinDialogOpen, setJoinDialogOpen] = useState<boolean>(false);
const [createDialogOpen, setCreateDialogOpen] = useState<boolean>(false);
const [passwordDialogOpen, setPasswordDialogOpen] = useState<boolean>(false);
const [selectedChannel, setSelectedChannel] = useState(null);
const [channels, setChannels] = useState([]);

useEffect(() => getAndSetChannels(), []);
Expand All @@ -26,14 +29,23 @@ export default function ChannelPanel() {
})
}

function joinChannel(channelId: string) {
function joinChannelWithPassword(password: string) {
axios
.post(`/server/api/channel/${channelId}`).
.post(
`/server/api/channel/${selectedChannel.id}`,
{ password }
).
then((response) => getAndSetChannels())
.catch((error) => {
alert(error.response.data.message);
});
setPasswordDialogOpen(false);
}

function onSelectChannelToJoin(channel) {
setSelectedChannel(channel);
setJoinDialogOpen(false);
setPasswordDialogOpen(true);
}

function openCreateDialog() {
Expand All @@ -53,12 +65,18 @@ export default function ChannelPanel() {
<JoinChannelDialog
open={joinDialogOpen}
onClose={() => setJoinDialogOpen(false)}
onSelect={joinChannel}
onSelect={onSelectChannelToJoin}
onCreateNew={openCreateDialog}
/>
<PasswordDialog
open={passwordDialogOpen}
onClose={() => setPasswordDialogOpen(false)}
onSubmit={joinChannelWithPassword}
/>
<CreateChannelDialog
open={createDialogOpen}
onClose={() => setCreateDialogOpen(false)}
onSubmit={getAndSetChannels}
/>
</Box>
);
Expand Down
71 changes: 69 additions & 2 deletions frontend/components/CreateChannelDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,77 @@
import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import DialogTitle from '@mui/material/DialogTitle';
import CheckBox from '@mui/material/CheckBox';
import FormControlLabel from '@mui/material/FormControlLabel';
import { useState } from 'react';
import axios from 'axios';

export default function CreateChannelDialog({ open, onClose, onSubmit }) {
const [name, setName] = useState<string>('');
const [description, setDescription] = useState<string>('');
const [password, setPassword] = useState<string>('');
const [isPrivate, setIsPrivate] = useState<boolean>(false);

function submit() {
axios
.post(
"/server/api/channel",
{
channelName: name,
description,
password,
isPublic: !isPrivate
},
)
.then((response) => onSubmit())
.catch((error) => {
});
onClose();
}

export default function CreateChannelDialog({ open, onClose }) {
return (
<Dialog onClose={onClose} open={open}>
<Dialog open={open} onClose={onClose} fullWidth>
<DialogTitle>Create new channel</DialogTitle>
<DialogContent>
<TextField
autoFocus
margin="dense"
label="Channel name"
fullWidth
required
onChange={(event) => setName(event.target.value)}
/>
<TextField
margin="dense"
label="Channel description"
fullWidth
multiline
rows={8}
onChange={(event) => setDescription(event.target.value)}
/>
<TextField
margin="dense"
label="Channel password"
fullWidth
name="password"
onChange={(event) => setPassword(event.target.value)}
/>
<FormControlLabel
label="Set private"
control={<CheckBox onChange={() => {
console.log('CheckBox onChange');
setIsPrivate(!isPrivate);
}} />}
/>
</DialogContent>
<DialogActions>
<Button onClick={onClose}>Cancel</Button>
<Button onClick={submit}>Create</Button>
</DialogActions>
</Dialog>
);
}
6 changes: 3 additions & 3 deletions frontend/components/JoinChannelDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ export default function JoinChannelDialog({
.catch((error) => {
console.error(error);
});
}, []);
}, [open]);

return (
<Dialog onClose={onClose} open={open}>
<DialogTitle>Join new channel</DialogTitle>
<List sx={{ pt: 0 }}>
{channels.map((channel) => (
<ListItemButton onClick={() => onSelect(channel.id)} key={channel.id}>
<ListItemText primary={channel.name} />
<ListItemButton onClick={() => onSelect(channel)} key={channel.id}>
<ListItemText primary={channel.channelName} />
</ListItemButton>
))}
<ListItemButton onClick={onCreateNew}>
Expand Down
33 changes: 33 additions & 0 deletions frontend/components/PasswordDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';
import { useState } from 'react';

export default function PasswordDialog({ open, onClose, onSubmit }) {
const [password, setPassword] = useState('');

return (
<Dialog open={open} onClose={onClose}>
<DialogTitle>
Enter password
</DialogTitle>
<DialogContent>
<TextField
autoFocus
margin="dense"
label="password"
fullWidth
variant="standard"
onChange={(event) => setPassword(event.target.value)}
/>
</DialogContent>
<DialogActions>
<Button onClick={onClose}>Cancel</Button>
<Button onClick={() => onSubmit(password)}>Enter</Button>
</DialogActions>
</Dialog>
);
}
2 changes: 1 addition & 1 deletion frontend/pages/dm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios from "axios";
import { user_data } from "./login";
import { dmSocket } from "../sockets/sockets";
import Link from "next/link";
import Layout from "../components/layout";
import Layout from "../components/Layout";

export default function Dm() {
const router = useRouter();
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/dm/[room_id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useEffect, useState } from "react";
import { dmSocket } from "../../sockets/sockets";
import { useSocketAuthorization } from "../../lib/socket";
import { getLoginUser } from "../../lib/login";
import Layout from "../../components/layout";
import Layout from "../../components/Layout";

export default function Dm() {
useSocketAuthorization();
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/game/[room_id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
draw_countDown2,
} from "./sketch";
import { getLoginUser } from "../../lib/login";
import Layout from "../../components/layout";
import Layout from "../../components/Layout";

// Will only import `react-p5` on client-side
const Sketch = dynamic(() => import("react-p5").then((mod) => mod.default), {
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/matching.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRouter } from "next/router";
import { user_data } from "./login";
import { socket, useSocketAuthorization } from "../lib/socket";
import { useEffect } from "react";
import Layout from "../components/layout";
import Layout from "../components/Layout";

export default function matching() {
useSocketAuthorization();
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/profile/[user_name].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios from "axios";
import { user_data } from "../login";
import { socket, useSocketAuthorization } from "../../lib/socket";
import { logout, getLoginUser } from "../../lib/login";
import Layout from "../../components/layout";
import Layout from "../../components/Layout";
import { Button } from "@mui/material";
import {
InviteModal,
Expand Down

0 comments on commit a2c4a6f

Please sign in to comment.