index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { createStore, applyMiddleware } from 'redux'
  2. import thunk from 'redux-thunk';
  3. import promiseReducer from './reducers/promise-reducer'
  4. import { combineReducers } from "redux";
  5. import authReducer from './reducers/auth-reducer'
  6. import {history} from '../history'
  7. import chatsReducer from './reducers/chat-reducer';
  8. import {actionAuthLogout} from './actions/action-logout'
  9. import {actionCreateMessage} from './actions/action-create-msg'
  10. import {actionMsgOne} from './actions/actionMsgOn'
  11. import { actionAboutMe } from './actions/action-aboutMe';
  12. import { OpenChatPage } from '../pages/OpenChatPage';
  13. const store = createStore(
  14. combineReducers({
  15. auth: authReducer,
  16. promise: promiseReducer,
  17. chat: chatsReducer
  18. }),
  19. applyMiddleware(thunk)
  20. );
  21. export const socket = window.io("ws://chat.ed.asmer.org.ua");
  22. socket.on("jwt_ok", (data) => console.log(data));
  23. socket.on("jwt_fail", (error) => {
  24. console.log(error);
  25. });
  26. socket.on("msg", (msg) => {
  27. console.log("пришло смс");
  28. console.log(msg)
  29. store.dispatch(actionMsgOne(msg))
  30. });
  31. socket.on("chat", (chat) => {
  32. console.log("нас добавили в чат");
  33. const state = store.getState();
  34. socket.emit("jwt", state.auth.token);
  35. });
  36. socket.on("chat_left", (chat) => {
  37. console.log("нас выкинули из чата");
  38. });
  39. store.subscribe(() => {
  40. const authToken = store.getState().auth?.token;
  41. if (!authToken) {
  42. history.push('/')
  43. }
  44. console.log(store.getState())})
  45. export default store