Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrape dialog #644

Merged
merged 21 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
EditableTextUtils,
} from "src/utils";
import { useToast } from "src/hooks";
import { PerformerScrapeDialog } from "./PerformerScrapeDialog";

interface IPerformerDetails {
performer: Partial<GQL.PerformerDataFragment>;
Expand Down Expand Up @@ -90,6 +91,10 @@ export const PerformerDetailsPanel: React.FC<IPerformerDetails> = ({
const Scrapers = useListPerformerScrapers();
const [queryableScrapers, setQueryableScrapers] = useState<GQL.Scraper[]>([]);

const [scrapedPerformer, setScrapedPerformer] = useState<
GQL.ScrapedPerformer | undefined
>();

const imageEncoding = ImageUtils.usePasteImage(onImageLoad, isEditing);

function updatePerformerEditState(
Expand Down Expand Up @@ -142,15 +147,63 @@ export const PerformerDetailsPanel: React.FC<IPerformerDetails> = ({
function updatePerformerEditStateFromScraper(
state: Partial<GQL.ScrapedPerformerDataFragment>
) {
updatePerformerEditState(state);
if (state.name) {
setName(state.name);
}

// gender is a string in the scraper data
setGender(translateScrapedGender(state.gender ?? undefined));
if (state.aliases) {
setAliases(state.aliases ?? undefined);
}
if (state.birthdate) {
setBirthdate(state.birthdate ?? undefined);
}
if (state.ethnicity) {
setEthnicity(state.ethnicity ?? undefined);
}
if (state.country) {
setCountry(state.country ?? undefined);
}
if (state.eye_color) {
setEyeColor(state.eye_color ?? undefined);
}
if (state.height) {
setHeight(state.height ?? undefined);
}
if (state.measurements) {
setMeasurements(state.measurements ?? undefined);
}
if (state.fake_tits) {
setFakeTits(state.fake_tits ?? undefined);
}
if (state.career_length) {
setCareerLength(state.career_length ?? undefined);
}
if (state.tattoos) {
setTattoos(state.tattoos ?? undefined);
}
if (state.piercings) {
setPiercings(state.piercings ?? undefined);
}
if (state.url) {
setUrl(state.url ?? undefined);
}
if (state.twitter) {
setTwitter(state.twitter ?? undefined);
}
if (state.instagram) {
setInstagram(state.instagram ?? undefined);
}
if (state.gender) {
// gender is a string in the scraper data
setGender(translateScrapedGender(state.gender ?? undefined));
}

// image is a base64 string
// #404: don't overwrite image if it has been modified by the user
// overwrite if not new since it came from a dialog
// otherwise follow existing behaviour
if (
image === undefined &&
(!isNew || image === undefined) &&
(state as GQL.ScrapedPerformerDataFragment).image !== undefined
) {
const imageStr = (state as GQL.ScrapedPerformerDataFragment).image;
Expand Down Expand Up @@ -261,7 +314,13 @@ export const PerformerDetailsPanel: React.FC<IPerformerDetails> = ({
getQueryScraperPerformerInput()
);
if (!result?.data?.scrapePerformer) return;
updatePerformerEditStateFromScraper(result.data.scrapePerformer);

// if this is a new performer, just dump the data
if (isNew) {
updatePerformerEditStateFromScraper(result.data.scrapePerformer);
} else {
setScrapedPerformer(result.data.scrapePerformer);
}
} catch (e) {
Toast.error(e);
} finally {
Expand All @@ -278,11 +337,12 @@ export const PerformerDetailsPanel: React.FC<IPerformerDetails> = ({
return;
}

// leave URL as is if not set explicitly
if (!result.data.scrapePerformerURL.url) {
result.data.scrapePerformerURL.url = url;
// if this is a new performer, just dump the data
if (isNew) {
updatePerformerEditStateFromScraper(result.data.scrapePerformerURL);
} else {
setScrapedPerformer(result.data.scrapePerformerURL);
}
updatePerformerEditStateFromScraper(result.data.scrapePerformerURL);
} catch (e) {
Toast.error(e);
} finally {
Expand Down Expand Up @@ -337,7 +397,9 @@ export const PerformerDetailsPanel: React.FC<IPerformerDetails> = ({

return (
<OverlayTrigger trigger="click" placement="top" overlay={popover}>
<Button variant="secondary">Scrape with...</Button>
<Button variant="secondary" className="mr-2">
Scrape with...
</Button>
</OverlayTrigger>
);
}
Expand Down Expand Up @@ -386,6 +448,49 @@ export const PerformerDetailsPanel: React.FC<IPerformerDetails> = ({
);
}

function maybeRenderScrapeDialog() {
if (!scrapedPerformer) {
return;
}

const currentPerformer: Partial<GQL.PerformerDataFragment> = {
name,
aliases,
birthdate,
ethnicity,
country,
eye_color: eyeColor,
height,
measurements,
fake_tits: fakeTits,
career_length: careerLength,
tattoos,
piercings,
url,
twitter,
instagram,
gender: stringToGender(gender),
image_path: image ?? performer.image_path,
};

return (
<PerformerScrapeDialog
performer={currentPerformer}
scraped={scrapedPerformer}
onClose={(p) => {
onScrapeDialogClosed(p);
}}
/>
);
}

function onScrapeDialogClosed(p?: GQL.ScrapedPerformerDataFragment) {
if (p) {
updatePerformerEditStateFromScraper(p);
}
setScrapedPerformer(undefined);
}

function renderURLField() {
return (
<tr>
Expand All @@ -411,15 +516,15 @@ export const PerformerDetailsPanel: React.FC<IPerformerDetails> = ({
return (
<div className="row">
<Button
className="edit-button"
className="mr-2"
variant="primary"
onClick={() => onSave?.(getPerformerInput())}
>
Save
</Button>
{!isNew ? (
<Button
className="edit-button"
className="mr-2"
variant="danger"
onClick={() => setIsDeleteAlertOpen(true)}
>
Expand Down Expand Up @@ -503,6 +608,7 @@ export const PerformerDetailsPanel: React.FC<IPerformerDetails> = ({
<>
{renderDeleteAlert()}
{renderScraperDialog()}
{maybeRenderScrapeDialog()}

<Table id="performer-details" className="w-100">
<tbody>
Expand Down
Loading