-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #930 from rockingrohit9639/main
Assign custom fields to categories
- Loading branch information
Showing
17 changed files
with
345 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useState } from "react"; | ||
import { tw } from "~/utils/tw"; | ||
import DynamicSelect from "../dynamic-select/dynamic-select"; | ||
import { Button } from "../shared/button"; | ||
|
||
type CategoriesInputProps = { | ||
className?: string; | ||
style?: React.CSSProperties; | ||
disabled?: boolean; | ||
name: (index: number) => string; | ||
categories: string[]; | ||
}; | ||
|
||
export default function CategoriesInput({ | ||
className, | ||
style, | ||
disabled, | ||
name, | ||
categories: incomingCategories, | ||
}: CategoriesInputProps) { | ||
const [categories, setCategories] = useState<string[]>( | ||
incomingCategories.length === 0 ? [""] : incomingCategories | ||
); | ||
|
||
return ( | ||
<div className={tw("w-full", className)} style={style}> | ||
{categories.map((category, i) => ( | ||
<div key={i} className="mb-3 flex items-center gap-x-2"> | ||
<DynamicSelect | ||
disabled={disabled} | ||
fieldName={name(i)} | ||
defaultValue={category} | ||
model={{ name: "category", queryKey: "name" }} | ||
label="Category" | ||
initialDataKey="categories" | ||
countKey="totalCategories" | ||
placeholder="Select Category" | ||
className="flex-1" | ||
excludeItems={categories} | ||
onChange={(value) => { | ||
categories[i] = value; | ||
setCategories([...categories]); | ||
}} | ||
/> | ||
|
||
<Button | ||
icon="x" | ||
className="py-2" | ||
variant="outline" | ||
type="button" | ||
onClick={() => { | ||
categories.splice(i, 1); | ||
setCategories([...categories]); | ||
}} | ||
/> | ||
</div> | ||
))} | ||
|
||
<Button | ||
icon="plus" | ||
className="py-3" | ||
variant="link" | ||
type="button" | ||
onClick={() => { | ||
setCategories((prev) => [...prev, ""]); | ||
}} | ||
> | ||
Add another category | ||
</Button> | ||
</div> | ||
); | ||
} |
17 changes: 17 additions & 0 deletions
17
app/database/migrations/20240424093819_add_category_to_custom_field/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- CreateTable | ||
CREATE TABLE "_CategoryToCustomField" ( | ||
"A" TEXT NOT NULL, | ||
"B" TEXT NOT NULL | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "_CategoryToCustomField_AB_unique" ON "_CategoryToCustomField"("A", "B"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_CategoryToCustomField_B_index" ON "_CategoryToCustomField"("B"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_CategoryToCustomField" ADD CONSTRAINT "_CategoryToCustomField_A_fkey" FOREIGN KEY ("A") REFERENCES "Category"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_CategoryToCustomField" ADD CONSTRAINT "_CategoryToCustomField_B_fkey" FOREIGN KEY ("B") REFERENCES "CustomField"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
4 changes: 4 additions & 0 deletions
4
...base/migrations/20240429080434_add_rls_to_category_to_custom_field_relation/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-- This is an empty migration. | ||
|
||
-- Enable RLS | ||
ALTER TABLE "_CategoryToCustomField" ENABLE row level security; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.