👺 Fixed usage of context with sockets
This commit is contained in:
@@ -20,7 +20,7 @@ import {
|
|||||||
import { refineQuery } from "../shared/utils/filenameparser.utils";
|
import { refineQuery } from "../shared/utils/filenameparser.utils";
|
||||||
import sortBy from "array-sort-by";
|
import sortBy from "array-sort-by";
|
||||||
import { success } from "react-notification-system-redux";
|
import { success } from "react-notification-system-redux";
|
||||||
import { WebSocketContext } from "../context/socket/socket.context";
|
import { Socket } from "socket.io-client";
|
||||||
|
|
||||||
export async function walkFolder(path: string): Promise<Array<IFolderData>> {
|
export async function walkFolder(path: string): Promise<Array<IFolderData>> {
|
||||||
return axios
|
return axios
|
||||||
@@ -48,49 +48,50 @@ export async function walkFolder(path: string): Promise<Array<IFolderData>> {
|
|||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @return {Promise<string>} HTML of the page
|
* @return {Promise<string>} HTML of the page
|
||||||
*/
|
*/
|
||||||
export const fetchComicBookMetadata = (options) => async (dispatch) => {
|
export const fetchComicBookMetadata =
|
||||||
const socket = useContext(WebSocketContext);
|
(options, socketInstance: Socket) => async (dispatch) => {
|
||||||
const extractionOptions = {
|
const socket = socketInstance.socket;
|
||||||
extractTarget: "cover",
|
const extractionOptions = {
|
||||||
targetExtractionFolder: "./userdata/covers",
|
extractTarget: "cover",
|
||||||
extractionMode: "bulk",
|
targetExtractionFolder: "./userdata/covers",
|
||||||
paginationOptions: {
|
extractionMode: "bulk",
|
||||||
pageLimit: 25,
|
paginationOptions: {
|
||||||
page: 1,
|
pageLimit: 25,
|
||||||
},
|
page: 1,
|
||||||
};
|
|
||||||
const walkedFolders = await walkFolder("./comics");
|
|
||||||
dispatch(
|
|
||||||
success({
|
|
||||||
// uid: 'once-please', // you can specify your own uid if required
|
|
||||||
title: "Import Started",
|
|
||||||
message: `<span class="icon-text has-text-success"><i class="fas fa-plug"></i></span> Socket <span class="has-text-info">${socket.id}</span> connected. <strong>${walkedFolders.length}</strong> comics scanned.`,
|
|
||||||
dismissible: "click",
|
|
||||||
position: "tr",
|
|
||||||
autoDismiss: 0,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
await axios
|
|
||||||
.request({
|
|
||||||
url: "http://localhost:8050/api/getComicCovers",
|
|
||||||
method: "POST",
|
|
||||||
data: {
|
|
||||||
extractionOptions,
|
|
||||||
walkedFolders,
|
|
||||||
},
|
},
|
||||||
})
|
};
|
||||||
.then((response) => {
|
const walkedFolders = await walkFolder("./comics");
|
||||||
console.log("Response from import call", response);
|
dispatch(
|
||||||
});
|
success({
|
||||||
|
// uid: 'once-please', // you can specify your own uid if required
|
||||||
|
title: "Import Started",
|
||||||
|
message: `<span class="icon-text has-text-success"><i class="fas fa-plug"></i></span> Socket <span class="has-text-info">${socket.id}</span> connected. <strong>${walkedFolders.length}</strong> comics scanned.`,
|
||||||
|
dismissible: "click",
|
||||||
|
position: "tr",
|
||||||
|
autoDismiss: 0,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await axios
|
||||||
|
.request({
|
||||||
|
url: "http://localhost:8050/api/getComicCovers",
|
||||||
|
method: "POST",
|
||||||
|
data: {
|
||||||
|
extractionOptions,
|
||||||
|
walkedFolders,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((response) => {
|
||||||
|
console.log("Response from import call", response);
|
||||||
|
});
|
||||||
|
|
||||||
socket.on("coverExtracted", (data) => {
|
socket.on("coverExtracted", (data) => {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
dispatch({
|
dispatch({
|
||||||
type: IMS_COMICBOOK_METADATA_FETCHED,
|
type: IMS_COMICBOOK_METADATA_FETCHED,
|
||||||
data,
|
data,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
};
|
|
||||||
|
|
||||||
export const getComicBooks = (options) => async (dispatch) => {
|
export const getComicBooks = (options) => async (dispatch) => {
|
||||||
const { paginationOptions } = options;
|
const { paginationOptions } = options;
|
||||||
|
|||||||
@@ -177,17 +177,17 @@ export const AcquisitionPanel = (
|
|||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<a
|
<a
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
downloadDCPPResult(
|
downloadDCPPResult(
|
||||||
searchInstance.id,
|
searchInstance.id,
|
||||||
result.id,
|
result.id,
|
||||||
props.comicBookMetadata._id,
|
props.comicBookMetadata._id,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<i className="fas fa-file-download"></i>
|
<i className="fas fa-file-download"></i>
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -213,7 +213,7 @@ export const ComicDetail = ({}: ComicDetailProps): ReactElement => {
|
|||||||
<dl>
|
<dl>
|
||||||
<dt>Raw File Details</dt>
|
<dt>Raw File Details</dt>
|
||||||
<dd>{props.data.containedIn}</dd>
|
<dd>{props.data.containedIn}</dd>
|
||||||
<dd>{props.data.path}</dd>
|
<dd className="is-size-7">{props.data.path}</dd>
|
||||||
<dd>
|
<dd>
|
||||||
<div className="field is-grouped">
|
<div className="field is-grouped">
|
||||||
<div className="control">
|
<div className="control">
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { useDispatch, useSelector } from "react-redux";
|
|||||||
import { RootState } from "threetwo-ui-typings";
|
import { RootState } from "threetwo-ui-typings";
|
||||||
import { isNil, map } from "lodash";
|
import { isNil, map } from "lodash";
|
||||||
import prettyBytes from "pretty-bytes";
|
import prettyBytes from "pretty-bytes";
|
||||||
|
import dayjs from "dayjs";
|
||||||
import ellipsize from "ellipsize";
|
import ellipsize from "ellipsize";
|
||||||
|
|
||||||
interface IDownloadsPanelProps {
|
interface IDownloadsPanelProps {
|
||||||
@@ -73,7 +74,7 @@ export const DownloadsPanel = (
|
|||||||
<tr>
|
<tr>
|
||||||
<th>Filename</th>
|
<th>Filename</th>
|
||||||
<th>Size</th>
|
<th>Size</th>
|
||||||
<th>Time</th>
|
<th>Download Time</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -86,6 +87,11 @@ export const DownloadsPanel = (
|
|||||||
<span className="is-size-7">{bundle.target}</span>
|
<span className="is-size-7">{bundle.target}</span>
|
||||||
</td>
|
</td>
|
||||||
<td>{prettyBytes(bundle.size)}</td>
|
<td>{prettyBytes(bundle.size)}</td>
|
||||||
|
<td>
|
||||||
|
{dayjs
|
||||||
|
.unix(bundle.time_finished)
|
||||||
|
.format("h:mm on ddd, D MMM, YYYY")}
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
))}
|
))}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import React, { ReactElement, useCallback } from "react";
|
import React, { ReactElement, useCallback, useContext } from "react";
|
||||||
import { isEmpty, isNil, isUndefined } from "lodash";
|
import { isEmpty, isNil, isUndefined } from "lodash";
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
import { fetchComicBookMetadata } from "../actions/fileops.actions";
|
import { fetchComicBookMetadata } from "../actions/fileops.actions";
|
||||||
|
import { WebSocketContext } from "../context/socket/socket.context";
|
||||||
import { IFolderData } from "threetwo-ui-typings";
|
import { IFolderData } from "threetwo-ui-typings";
|
||||||
import { LazyLog, ScrollFollow } from "react-lazylog";
|
import { LazyLog, ScrollFollow } from "react-lazylog";
|
||||||
import DynamicList, { createCache } from "react-window-dynamic-list";
|
import DynamicList, { createCache } from "react-window-dynamic-list";
|
||||||
@@ -29,6 +30,7 @@ interface IProps {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export const Import = (props: IProps): ReactElement => {
|
export const Import = (props: IProps): ReactElement => {
|
||||||
|
const socket = useContext(WebSocketContext);
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const isSocketConnected = useSelector((state: RootState) => {
|
const isSocketConnected = useSelector((state: RootState) => {
|
||||||
console.log(state);
|
console.log(state);
|
||||||
@@ -43,7 +45,7 @@ export const Import = (props: IProps): ReactElement => {
|
|||||||
const initiateImport = useCallback(() => {
|
const initiateImport = useCallback(() => {
|
||||||
if (typeof props.path !== "undefined") {
|
if (typeof props.path !== "undefined") {
|
||||||
console.log("asdasd");
|
console.log("asdasd");
|
||||||
dispatch(fetchComicBookMetadata(props.path));
|
dispatch(fetchComicBookMetadata(props.path, socket));
|
||||||
}
|
}
|
||||||
}, [dispatch]);
|
}, [dispatch]);
|
||||||
const cache = createCache();
|
const cache = createCache();
|
||||||
|
|||||||
@@ -1,34 +1,28 @@
|
|||||||
import React, { createContext } from "react";
|
import React, { createContext, ReactElement } from "react";
|
||||||
import io, { Socket } from "socket.io-client";
|
import io, { Socket } from "socket.io-client";
|
||||||
import { SOCKET_BASE_URI } from "../../constants/endpoints";
|
import { SOCKET_BASE_URI } from "../../constants/endpoints";
|
||||||
import { useDispatch } from "react-redux";
|
import { useDispatch } from "react-redux";
|
||||||
import { RMQ_SOCKET_CONNECTED } from "../../constants/action-types";
|
import { RMQ_SOCKET_CONNECTED } from "../../constants/action-types";
|
||||||
import { isNil } from "lodash";
|
|
||||||
|
|
||||||
const WebSocketContext = createContext(null);
|
const WebSocketContext = createContext(null);
|
||||||
export const WebSocketProvider = ({ children }) => {
|
export const WebSocketProvider = ({ children }): ReactElement => {
|
||||||
let socket: Socket;
|
|
||||||
let ws;
|
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
const socket: Socket = io(SOCKET_BASE_URI);
|
||||||
|
|
||||||
if (!isNil(socket)) {
|
socket.on("connect", () => {
|
||||||
socket = io(SOCKET_BASE_URI);
|
dispatch({
|
||||||
|
type: RMQ_SOCKET_CONNECTED,
|
||||||
socket.on("connect", () => {
|
isSocketConnected: true,
|
||||||
dispatch({
|
socketId: socket.id,
|
||||||
type: RMQ_SOCKET_CONNECTED,
|
|
||||||
isSocketConnected: true,
|
|
||||||
socketId: socket.id,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
socket.on("disconnect", () => {
|
|
||||||
console.log(`disconnect`);
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
socket.on("disconnect", () => {
|
||||||
|
console.log(`disconnect`);
|
||||||
|
});
|
||||||
|
|
||||||
ws = {
|
const ws: any = {
|
||||||
socket: socket,
|
socket,
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<WebSocketContext.Provider value={ws}>{children}</WebSocketContext.Provider>
|
<WebSocketContext.Provider value={ws}>{children}</WebSocketContext.Provider>
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import * as React from "react";
|
import React from "react";
|
||||||
import { render } from "react-dom";
|
import { render } from "react-dom";
|
||||||
import { Provider } from "react-redux";
|
import { Provider } from "react-redux";
|
||||||
import { ConnectedRouter } from "connected-react-router";
|
import { ConnectedRouter } from "connected-react-router";
|
||||||
import WebSocketProvider, {
|
import WebSocketProvider from "./context/socket/socket.context";
|
||||||
WebSocketContext,
|
|
||||||
} from "./context/socket/socket.context";
|
|
||||||
import configureStore, { history } from "./store/index";
|
import configureStore, { history } from "./store/index";
|
||||||
import App from "./components/App";
|
import App from "./components/App";
|
||||||
const store = configureStore({});
|
const store = configureStore({});
|
||||||
|
|||||||
Reference in New Issue
Block a user