Skip to content

Commit

Permalink
dont allow duplicate options on custom fields:
Browse files Browse the repository at this point in the history
- make sure when importing the array of options is unique
- handle error in UI when trying to add 2 options with the same name
  • Loading branch information
DonKoko committed Jul 25, 2024
1 parent 85e8692 commit fc12b9c
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 fc12b9c

Please sign in to comment.