diff --git a/src/client/components/ComicDetail/ActionMenu/Menu.tsx b/src/client/components/ComicDetail/ActionMenu/Menu.tsx index 8993346..8f5543a 100644 --- a/src/client/components/ComicDetail/ActionMenu/Menu.tsx +++ b/src/client/components/ComicDetail/ActionMenu/Menu.tsx @@ -1,157 +1,13 @@ -import { filter, isEmpty, isNil, isUndefined } from "lodash"; -import React, { ReactElement, useCallback, useState } from "react"; -import Select, { components } from "react-select"; -import { fetchComicVineMatches } from "../../../actions/fileops.actions"; -import { refineQuery } from "filename-parser"; -import { COMICVINE_SERVICE_URI } from "../../../constants/endpoints"; -import axios from "axios"; +import React, { ReactElement } from "react"; +import Select from "react-select"; export const Menu = (props): ReactElement => { - const { data } = props; - const { setSlidingPanelContentId, setVisible } = props.handlers; - const [comicVineMatches, setComicVineMatches] = useState([]); - - const fetchComicVineMatches = async ( - searchPayload, - issueSearchQuery, - seriesSearchQuery, - ) => { - try { - await axios - .request({ - url: `${COMICVINE_SERVICE_URI}/volumeBasedSearch`, - method: "POST", - data: { - format: "json", - // hack - query: issueSearchQuery.inferredIssueDetails.name - .replace(/[^a-zA-Z0-9 ]/g, "") - .trim(), - limit: "100", - page: 1, - resources: "volume", - scorerConfiguration: { - searchParams: issueSearchQuery.inferredIssueDetails, - }, - rawFileDetails: searchPayload.rawFileDetails, - }, - transformResponse: (r) => { - const matches = JSON.parse(r); - return matches; - // return sortBy(matches, (match) => -match.score); - }, - }) - .then((response) => { - let matches: any = []; - if ( - !isNil(response.data.results) && - response.data.results.length === 1 - ) { - matches = response.data.results; - } else { - matches = response.data.map((match) => match); - } - setComicVineMatches(matches); - }); - } catch (err) { - console.log(err); - } - }; - const openDrawerWithCVMatches = () => { - let seriesSearchQuery: IComicVineSearchQuery = {} as IComicVineSearchQuery; - let issueSearchQuery: IComicVineSearchQuery = {} as IComicVineSearchQuery; - - if (!isUndefined(data.rawFileDetails)) { - issueSearchQuery = refineQuery(data.rawFileDetails.name); - } else if (!isEmpty(data.sourcedMetadata)) { - issueSearchQuery = refineQuery(data.sourcedMetadata.comicvine.name); - } - fetchComicVineMatches(data, issueSearchQuery, seriesSearchQuery); - setSlidingPanelContentId("CVMatches"); - setVisible(true); - }; - - const openEditMetadataPanel = useCallback(() => { - setSlidingPanelContentId("editComicBookMetadata"); - setVisible(true); - }, []); - // Actions menu options and handler - const CVMatchLabel = ( - -
- -
-
Match on ComicVine
-
- ); - const editLabel = ( - -
- -
-
Edit Metadata
-
- ); - const deleteLabel = ( - -
- -
-
Delete Comic
-
- ); - const Placeholder = (props) => { - return ; - }; - const actionOptions = [ - { value: "match-on-comic-vine", label: CVMatchLabel }, - { value: "edit-metdata", label: editLabel }, - { value: "delete-comic", label: deleteLabel }, - ]; - - const filteredActionOptions = filter(actionOptions, (item) => { - if (isUndefined(data.rawFileDetails)) { - return item.value !== "match-on-comic-vine"; - } - return item; - }); - const handleActionSelection = (action) => { - switch (action.value) { - case "match-on-comic-vine": - openDrawerWithCVMatches(); - break; - case "edit-metdata": - openEditMetadataPanel(); - break; - default: - console.log("No valid action selected."); - break; - } - }; - const customStyles = { - menu: (base) => ({ - ...base, - backgroundColor: "rgb(156, 163, 175)", - }), - placeholder: (base) => ({ - ...base, - color: "black", - }), - option: (base, { data, isDisabled, isFocused, isSelected }) => ({ - ...base, - backgroundColor: isFocused ? "gray" : "rgb(156, 163, 175)", - }), - singleValue: (base) => ({ - ...base, - paddingTop: "0.4rem", - }), - control: (base) => ({ - ...base, - backgroundColor: "rgb(156, 163, 175)", - color: "black", - border: "1px solid rgb(156, 163, 175)", - }), - }; + const { + filteredActionOptions, + customStyles, + handleActionSelection, + Placeholder, + } = props.configuration; return (