Skip to content

Commit

Permalink
Merge pull request #1207 from Shelf-nu/1204-bug-import-allows-for-mul…
Browse files Browse the repository at this point in the history
…tiple-options-with-the-same-value-should-not-be-allowed

fix: dont allow duplicate options on custom fields
  • Loading branch information
DonKoko authored Jul 25, 2024
2 parents 85e8692 + fc12b9c commit 6eb7a50
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions app/components/forms/option-builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,30 @@ interface Props {

function OptionBuilder({ options, onAdd, onRemove, disabled }: Props) {
const [opt, setOpt] = useState("");
const [error, setError] = useState("");
return (
<div className="container flex-1 grow rounded border px-6 py-4 text-[14px] text-gray-600">
<div className="">
<Input
onChange={({ target }) => setOpt(target.value)}
label=""
value={opt}
defaultValue={opt}
placeholder="Type an option here and press enter"
disabled={disabled}
className="w-full"
error={error}
hideLabel
onKeyDown={(e) => {
if (e.key == "Enter") {
e.preventDefault();
if (opt) {
onAdd(opt);
setOpt("");
if (options.includes(opt)) {
setError("Option already exists");
} else {
onAdd(opt);
setOpt("");
setError("");
}
}
}
}}
Expand Down
4 changes: 2 additions & 2 deletions app/modules/custom-field/service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ export async function createCustomFieldsIfNotExists({

for (const [customFieldDefStr, def] of Object.entries(fieldToDefDraftMap)) {
if (def.type === "OPTION" && optionMap[customFieldDefStr]?.length) {
def.options = optionMap[customFieldDefStr];
const uniqueSet = new Set(optionMap[customFieldDefStr]);
def.options = Array.from(uniqueSet);
}
}

return await upsertCustomField(Object.values(fieldToDefDraftMap));
} catch (cause) {
throw new ShelfError({
Expand Down

0 comments on commit 6eb7a50

Please sign in to comment.