Use redux hooks on the websocket component
This commit is contained in:
parent
d9fae3e23a
commit
af2641c317
@ -1,25 +1,14 @@
|
|||||||
import { useEffect, useState, useCallback } from "react";
|
import { useEffect, useState, useCallback } from "react";
|
||||||
import PropTypes from "prop-types";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import { connect } from "react-redux";
|
|
||||||
import { setFetchedTorrents } from "../actions/torrents";
|
import { setFetchedTorrents } from "../actions/torrents";
|
||||||
import { newMovieEvent } from "../actions/movies";
|
import { newMovieEvent } from "../actions/movies";
|
||||||
import { newEpisodeEvent } from "../actions/shows";
|
import { newEpisodeEvent } from "../actions/shows";
|
||||||
|
|
||||||
const mapStateToProps = (state) => ({
|
const WsHandler = () => {
|
||||||
isLogged: state.userStore.get("isLogged"),
|
const dispatch = useDispatch();
|
||||||
});
|
|
||||||
const mapDispatchToProps = {
|
const isLogged = useSelector((state) => state.userStore.get("isLogged"));
|
||||||
setFetchedTorrents,
|
|
||||||
newMovieEvent,
|
|
||||||
newEpisodeEvent,
|
|
||||||
};
|
|
||||||
|
|
||||||
const WsHandler = ({
|
|
||||||
isLogged,
|
|
||||||
setFetchedTorrents,
|
|
||||||
newMovieEvent,
|
|
||||||
newEpisodeEvent,
|
|
||||||
}) => {
|
|
||||||
const [ws, setWs] = useState(null);
|
const [ws, setWs] = useState(null);
|
||||||
|
|
||||||
const stop = useCallback(() => {
|
const stop = useCallback(() => {
|
||||||
@ -73,13 +62,13 @@ const WsHandler = ({
|
|||||||
|
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case "torrents":
|
case "torrents":
|
||||||
setFetchedTorrents(data.message);
|
dispatch(setFetchedTorrents(data.message));
|
||||||
break;
|
break;
|
||||||
case "newVideo":
|
case "newVideo":
|
||||||
if (data.message.type === "movie") {
|
if (data.message.type === "movie") {
|
||||||
newMovieEvent(data.message.data);
|
dispatch(newMovieEvent(data.message.data));
|
||||||
} else if (data.message.type === "episode") {
|
} else if (data.message.type === "episode") {
|
||||||
newEpisodeEvent(data.message.data);
|
dispatch(newEpisodeEvent(data.message.data));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -90,7 +79,7 @@ const WsHandler = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
setWs(socket);
|
setWs(socket);
|
||||||
}, [ws, isLogged, newMovieEvent, setFetchedTorrents, newEpisodeEvent, stop]);
|
}, [ws, isLogged, dispatch, stop]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const intervalID = setInterval(() => {
|
const intervalID = setInterval(() => {
|
||||||
@ -118,11 +107,5 @@ const WsHandler = ({
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
WsHandler.propTypes = {
|
|
||||||
isLogged: PropTypes.bool.isRequired,
|
|
||||||
setFetchedTorrents: PropTypes.func,
|
|
||||||
newMovieEvent: PropTypes.func,
|
|
||||||
newEpisodeEvent: PropTypes.func,
|
|
||||||
};
|
|
||||||
|
|
||||||
export default connect(mapStateToProps, mapDispatchToProps)(WsHandler);
|
export default WsHandler;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user