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

v3 - fix(SoMeInputs): remove useEffects and use proper form patches #530

Merged
merged 2 commits into from
Sep 3, 2024
Merged
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
69 changes: 37 additions & 32 deletions studio/components/SoMeInputs.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from "react";
import { ObjectInputProps, PatchEvent, set } from "sanity";
import React from "react";
import { ObjectInputProps, set } from "sanity";
import { Box, TextInput, Select, Stack, Label } from "@sanity/ui";

export const SoMePlatforms: { [key: string]: string } = {
Expand All @@ -14,62 +14,67 @@ export const SoMePlatforms: { [key: string]: string } = {
tikTok: "Tiktok",
};

const detectPlatformFromUrl = (url: string): string | null => {
for (const key in SoMePlatforms) {
if (url.includes(key)) {
return SoMePlatforms[key];
}
}
return null;
};

const SoMeInputs: React.FC<ObjectInputProps<Record<string, any>>> = ({
value = {},
onChange,
}) => {
const [url, setUrl] = useState(value.url || "");
const [platform, setPlatform] = useState(value.platform || "");

useEffect(() => {
let detectedPlatform = "";
for (const key in SoMePlatforms) {
if (url.includes(key)) {
detectedPlatform = SoMePlatforms[key];
break;
}
}
setPlatform(detectedPlatform);
if (onChange) {
onChange(
PatchEvent.from([set({ ...value, url, platform: detectedPlatform })]),
);
}
}, [url, onChange]);

const handleUrlChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (!onChange) return;
const newUrl = event.target.value;
setUrl(newUrl);
onChange(set(newUrl, ["url"]));
const detectedPlatform = detectPlatformFromUrl(newUrl);
if (detectedPlatform !== null) {
onChange(set(detectedPlatform, ["platform"]));
}
};

const handlePlatformChange = (
event: React.ChangeEvent<HTMLSelectElement>,
) => {
if (!onChange) return;
const newPlatform = event.target.value;
setPlatform(newPlatform);
if (onChange) {
onChange(
PatchEvent.from([set({ ...value, url, platform: newPlatform })]),
);
}
onChange(set(newPlatform, ["platform"]));
};

const urlInputName = "social-url-input";
const platformInputName = "social-platform-input";

return (
<Stack space={6}>
<Box>
<Stack space={3}>
<Label>URL</Label>
<Label htmlFor={urlInputName} as={"label"}>
URL
</Label>
<TextInput
value={url}
id={urlInputName}
name={urlInputName}
value={value.url}
onChange={handleUrlChange}
placeholder="Enter URL"
/>
</Stack>
</Box>
<Box>
<Stack space={3}>
<Label>Platform</Label>
<Select value={platform} onChange={handlePlatformChange}>
<Label htmlFor={platformInputName} as={"label"}>
Platform
</Label>
<Select
mathiazom marked this conversation as resolved.
Show resolved Hide resolved
id={platformInputName}
name={platformInputName}
value={value.platform}
onChange={handlePlatformChange}
>
<option value="">Select Platform</option>
{Object.values(SoMePlatforms).map((platform) => (
<option key={platform} value={platform}>
Expand Down