Skip to content

Commit

Permalink
Added a window for adding a new channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
AxeRicin committed Dec 9, 2023
1 parent 595f65e commit aeb76ee
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text=auto
* text eol=lf
10 changes: 8 additions & 2 deletions frontend/src/Components/Channels.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { useSelector } from 'react-redux';
import { useSelector, useDispatch } from 'react-redux';
import { PlusSquare } from 'react-bootstrap-icons';
import ChannelBtn from './ChannelBtn';
import { openModal } from '../slices/modalSlice';

const Channels = () => {
const channelsInfo = useSelector((state) => state.channelsInfo);
const dispatch = useDispatch();
const { channels } = channelsInfo;

const handleAddChannel = () => {
dispatch(openModal({ type: 'addChannel' }));
};

return (
<div className="col-4 col-md-2 border-end px-0 bg-light flex-column h-100 d-flex">
<div className="d-flex mt-1 justify-content-between mb-2 ps-4 pe-2 p-4">
<b>Каналы</b>
<button className="p-0 text-primary btn btn-group-vertical" type="button">
<button onClick={handleAddChannel} className="p-0 text-primary btn btn-group-vertical" type="button">
<PlusSquare width="20" height="20" />
<span className="visually-hidden">+</span>
</button>
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/Components/Layout.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {
Link, Outlet, useLocation, Navigate,
} from 'react-router-dom';
import { useSelector } from 'react-redux';
import getRoutes from '../routes';
import useAuth from '../hook/useAuth';
import Modal from './Modal';
import getModal from './Modals/index';

const Layout = () => {
const location = useLocation();
const { userToken } = useAuth();
const { type } = useSelector(((state) => state.modal));
if (location.pathname === getRoutes.main() && !userToken) {
return <Navigate to="/login" state={{ from: location }} />;
}
Expand All @@ -19,7 +21,7 @@ const Layout = () => {
</div>
</nav>
<Outlet />
<Modal />
{getModal(type)}
</>
);
};
Expand Down
1 change: 0 additions & 1 deletion frontend/src/Components/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const LoginForm = () => {
try {
setAuthFailed(false);
const response = (await axios.post('/api/v1/login', values));
console.log(response);
const { data } = response;
signIn(data, () => navigate(fromPage, { replace: true }));
} catch (err) {
Expand Down
27 changes: 0 additions & 27 deletions frontend/src/Components/Modal.js

This file was deleted.

63 changes: 63 additions & 0 deletions frontend/src/Components/Modals/AddChannel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Modal, Form, Button } from 'react-bootstrap';
import { useDispatch, useSelector } from 'react-redux';
import { useFormik } from 'formik';
import {
useContext, useEffect, useRef, useState,
} from 'react';
import cn from 'classnames';
import { modalClose } from '../../slices/modalSlice';
import { ApiContext } from '../../hoc/ApiProvider';

const ModalAddChannel = () => {
const { isOpened } = useSelector((state) => state.modal);
const { sendNewChannel } = useContext(ApiContext);
const dispatch = useDispatch();
const inputRef = useRef();
const [inputInvalid] = useState(false);
// setInputInvalid
const handlerСlosure = () => dispatch(modalClose());

const formik = useFormik({
initialValues: {
name: '',
},
onSubmit: async (values) => {
sendNewChannel(values.name);
},
});

useEffect(() => {
inputRef.current.select();
}, []);

return (
<Modal dialogClassName="modal-dialog-centered" show={isOpened} onHide={handlerСlosure}>
<Modal.Header closeButton>
<Modal.Title>Добавить канал</Modal.Title>
</Modal.Header>
<Modal.Body>
<Form onSubmit={formik.handleSubmit}>
<Form.Group controlId="name">
<Form.Control
ref={inputRef}
className={cn({
'mb-2': true,
'is-invalid': inputInvalid,
})}
name="name"
value={formik.values.name}
onChange={formik.handleChange}
/>
<Form.Label className="visually-hidden">Имя канала</Form.Label>
<div className="d-flex justify-content-end">
<Button className="me-2" variant="secondary">Отменить</Button>
<Button variant="primary" type="submit">Отправить</Button>
</div>
</Form.Group>
</Form>
</Modal.Body>
</Modal>
);
};

export default ModalAddChannel;
11 changes: 11 additions & 0 deletions frontend/src/Components/Modals/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import AddChannel from './AddChannel.js';
// import RemoveChannel from './RemoveChannel.jsx';
// import RenameChannel from './RenameChannel.jsx';

const modals = {
addChannel: <AddChannel />,
// removeChannel: RemoveChannel,
// renameChannel: RenameChannel,
};

export default (modalType) => modals[modalType];
Binary file added frontend/src/assets/favicon.ico
Binary file not shown.
7 changes: 7 additions & 0 deletions frontend/src/hoc/ApiProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ const ApiProvider = ({ children }) => {
});
};

const sendNewChannel = (nameNewChannel) => {
socket.timeout(msTimeout).emit('newChannel', { nameNewChannel }, (err, response) => {
console.log(err, response);
});
};

const value = useMemo(() => ({
sendMessage,
takeMessage,
sendNewChannel,
}), []);

return (<ApiContext.Provider value={value}>{children}</ApiContext.Provider>);
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/slices/modalSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ export const modalSlice = createSlice({
initialState,
reducers: {
modalClose: (state) => {
state.isOpened = null;
state.isOpened = false;
state.extra = null;
state.type = null;
},
openModal: (state, { payload }) => {
const { type, extra } = payload;
console.log(type);
state.type = type;
state.extra = extra ?? null;
state.isOpened = true;
},
},
});

export const { modalClose } = modalSlice.actions;
export const { modalClose, openModal } = modalSlice.actions;

export default modalSlice.reducer;

0 comments on commit aeb76ee

Please sign in to comment.