Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 19 additions & 5 deletions frontend/src/components/urlInput/urlInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ interface URLInputProps {
lens: Lens<URLItem[]>;
type: ValidSiteTypeEnum;
errors?: ErrorsType;
pendingURLError?: string;
onPendingURLChange?: (pendingURL: string) => void;
}

const URLInput: FC<URLInputProps> = ({ lens, type, errors }) => {
const URLInput: FC<URLInputProps> = ({
lens,
type,
errors,
pendingURLError,
onPendingURLChange,
}) => {
const interop = lens.interop();
const {
fields: urls,
Expand All @@ -55,6 +63,11 @@ const URLInput: FC<URLInputProps> = ({ lens, type, errors }) => {
s.valid_types.includes(type),
);

const setPendingURL = (url: string) => {
setNewURL(url);
onPendingURLChange?.(url.trim());
};

const handleAdd = () => {
if (!newURL || !selectedSite) return;
const cleanedURL = cleanURL(selectedSite?.regex, newURL);
Expand All @@ -69,7 +82,7 @@ const URLInput: FC<URLInputProps> = ({ lens, type, errors }) => {
if (selectRef.current) selectRef.current.value = "";
if (inputRef.current) inputRef.current.value = "";
setSelectedSite(undefined);
setNewURL("");
setPendingURL("");
};

const handleInput = (url: string) => {
Expand All @@ -89,6 +102,7 @@ const URLInput: FC<URLInputProps> = ({ lens, type, errors }) => {
const updatedURL = cleanURL(site.regex, url);
if (updatedURL) {
inputRef.current.value = updatedURL;
setPendingURL(updatedURL);
return true;
}
}
Expand All @@ -99,7 +113,6 @@ const URLInput: FC<URLInputProps> = ({ lens, type, errors }) => {
const match = handleInput(e.clipboardData.getData("text/plain"));
if (match) {
e.preventDefault();
setNewURL(e.currentTarget.value);
}
};

Expand Down Expand Up @@ -159,14 +172,15 @@ const URLInput: FC<URLInputProps> = ({ lens, type, errors }) => {
ref={inputRef}
onBlur={(e) => handleInput(e.currentTarget.value)}
placeholder="URL"
onChange={(e) => setNewURL(e.currentTarget.value)}
onChange={(e) => setPendingURL(e.currentTarget.value)}
onPaste={handlePaste}
className="w-50"
className={`w-50 ${pendingURLError ? "is-invalid" : ""}`}
/>
<Button onClick={handleAdd} disabled={!newURL || !selectedSite}>
Add
</Button>
</InputGroup>
{pendingURLError && <div className="text-danger">{pendingURLError}</div>}
</div>
);
};
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export { default as useAuth } from "./useAuth";
export { useBeforeUnload } from "./useBeforeUnload";
export { useCurrentUser } from "./useCurrentUser";
export { default as useEditFilter } from "./useEditFilter";
export { default as usePagination } from "./usePagination";
export { usePendingURLField } from "./usePendingURLField";
export { useQueryParams } from "./useQueryParams";
export { useToast } from "./useToast";
export { useUser } from "./useUser";
20 changes: 20 additions & 0 deletions frontend/src/hooks/usePendingURLField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useCallback } from "react";
import type {
FieldValues,
Path,
PathValue,
UseFormSetValue,
} from "react-hook-form";

export const usePendingURLField = <T extends FieldValues>(
setValue: UseFormSetValue<T>,
name: Path<T>,
) =>
useCallback(
(pendingURL: string) => {
setValue(name, pendingURL as PathValue<T, Path<T>>, {
shouldValidate: true,
});
},
[name, setValue],
);
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
type PerformerEditOptionsInput,
ValidSiteTypeEnum,
} from "src/graphql";
import { useBeforeUnload } from "src/hooks/useBeforeUnload";
import { useBeforeUnload, usePendingURLField } from "src/hooks";
import DiffPerformer from "./diff";
import ExistingPerformerAlert from "./ExistingPerformerAlert";
import { type PerformerFormData, PerformerSchema } from "./schema";
Expand Down Expand Up @@ -183,10 +183,12 @@ const PerformerForm: FC<PerformerProps> = ({
piercings: initial?.piercings ?? performer?.piercings ?? [],
images: initial?.images ?? performer?.images ?? [],
urls: initial?.urls ?? performer?.urls ?? [],
pendingUrl: "",
},
});

const lens = useLens({ control });
const onPendingURLChange = usePendingURLField(setValue, "pendingUrl");

const [activeTab, setActiveTab] = useState("personal");
const [updateAliases, setUpdateAliases] = useState<boolean>(
Expand Down Expand Up @@ -300,6 +302,7 @@ const PerformerForm: FC<PerformerProps> = ({
error: errors.urls?.find?.((u) => u?.url?.message)?.url?.message,
tab: "links",
},
{ error: errors.pendingUrl?.message, tab: "links" },
].filter((e) => e.error) as { error: string; tab: string }[];

return (
Expand Down Expand Up @@ -668,6 +671,8 @@ const PerformerForm: FC<PerformerProps> = ({
lens={lens.focus("urls").defined()}
type={ValidSiteTypeEnum.PERFORMER}
errors={errors.urls}
pendingURLError={errors.pendingUrl?.message}
onPendingURLChange={onPendingURLChange}
/>

<NavButtons onNext={() => setActiveTab("images")} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,24 @@ describe("PerformerForm", () => {
expect(callback).not.toHaveBeenCalled();
});

it("blocks submit when a URL is entered but not added", async () => {
const callback = vi.fn();
const { user } = renderEdit(callback);
await user.click(screen.getByRole("tab", { name: "Links" }));
const urlInput = (await waitFor(() => {
const el = document.querySelector('.URLInput input[placeholder="URL"]');
if (!el) throw new Error("URLInput not ready");
return el;
})) as HTMLInputElement;
await user.type(urlInput, "https://example.org/pending");
await submit(user);
const matches = await screen.findAllByText(
"Click Add to include the entered URL before submitting",
);
expect(matches.length).toBeGreaterThan(0);
expect(callback).not.toHaveBeenCalled();
});

it("hides breast type when gender is MALE", async () => {
const { user } = renderEdit();
expect(screen.getByLabelText("Breast type")).toBeInTheDocument();
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/pages/performers/performerForm/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ export const PerformerSchema = yup.object({
}),
)
.ensure(),
pendingUrl: yup
.string()
.test(
"no-pending-url",
"Click Add to include the entered URL before submitting",
(value) => !value,
),
note: yup.string().required("Edit note is required"),
});

Expand Down
8 changes: 7 additions & 1 deletion frontend/src/pages/scenes/sceneForm/SceneForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
type SceneEditDetailsInput,
ValidSiteTypeEnum,
} from "src/graphql";
import { useBeforeUnload } from "src/hooks/useBeforeUnload";
import { useBeforeUnload, usePendingURLField } from "src/hooks";
import { formatDuration, parseDuration, performerHref } from "src/utils";
import DiffScene from "./diff";
import ExistingSceneAlert from "./ExistingSceneAlert";
Expand Down Expand Up @@ -66,6 +66,7 @@ const SceneForm: FC<SceneProps> = ({
control,
handleSubmit,
watch,
setValue,
formState: { errors },
} = useForm({
resolver: yupResolver(SceneSchema),
Expand All @@ -83,6 +84,7 @@ const SceneForm: FC<SceneProps> = ({
images: initial?.images ?? scene?.images ?? [],
studio: initial?.studio ?? scene?.studio ?? undefined,
tags: initial?.tags ?? scene?.tags ?? [],
pendingUrl: "",
performers: (initial?.performers ?? scene?.performers ?? []).map((p) => ({
performerId: p.performer.id,
name: p.performer.name,
Expand All @@ -106,6 +108,7 @@ const SceneForm: FC<SceneProps> = ({
});

const lens = useLens({ control });
const onPendingURLChange = usePendingURLField(setValue, "pendingUrl");

const fieldData = watch();
const [oldSceneChanges, newSceneChanges] = useMemo(
Expand Down Expand Up @@ -318,6 +321,7 @@ const SceneForm: FC<SceneProps> = ({
error: errors.urls?.find?.((u) => u?.url?.message)?.url?.message,
tab: "links",
},
{ error: errors.pendingUrl?.message, tab: "links" },
].filter((e) => e.error) as { error: string; tab: string }[];

return (
Expand Down Expand Up @@ -500,6 +504,8 @@ const SceneForm: FC<SceneProps> = ({
lens={lens.focus("urls").defined()}
type={ValidSiteTypeEnum.SCENE}
errors={errors.urls}
pendingURLError={errors.pendingUrl?.message}
onPendingURLChange={onPendingURLChange}
/>

<NavButtons onNext={() => setActiveTab("images")} />
Expand Down
18 changes: 18 additions & 0 deletions frontend/src/pages/scenes/sceneForm/__tests__/SceneForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,24 @@ describe("SceneForm", () => {
expect(callback).not.toHaveBeenCalled();
});

it("blocks submit when a URL is entered but not added", async () => {
const callback = vi.fn();
const { user } = renderEdit(callback);
await user.click(screen.getByRole("tab", { name: "Links" }));
const urlInput = (await waitFor(() => {
const el = document.querySelector('.URLInput input[placeholder="URL"]');
if (!el) throw new Error("URLInput not ready");
return el;
})) as HTMLInputElement;
await user.type(urlInput, "https://pending.example");
await submit(user);
const matches = await screen.findAllByText(
"Click Add to include the entered URL before submitting",
);
expect(matches.length).toBeGreaterThan(0);
expect(callback).not.toHaveBeenCalled();
});

it("disables submit when saving=true", async () => {
const { user } = renderForm(
<SceneForm scene={baseScene} callback={vi.fn()} saving={true} />,
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/pages/scenes/sceneForm/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ export const SceneSchema = yup.object({
}),
)
.ensure(),
pendingUrl: yup
.string()
.test(
"no-pending-url",
"Click Add to include the entered URL before submitting",
(value) => !value,
),
note: yup.string().required("Edit note is required"),
});

Expand Down
Loading