Skip to content

Commit 9be6bd5

Browse files
committed
refactor: abstract specific AddModal out of Navbar
This follows the fixes from fastapi/full-stack-fastapi-template#1246
1 parent dbd3136 commit 9be6bd5

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

frontend/src/components/Common/Navbar.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { Button, Flex, Icon, useDisclosure } from "@chakra-ui/react"
2+
import type { ComponentType, ElementType } from "react"
23
import { FaPlus } from "react-icons/fa"
34

4-
import AddUser from "../Admin/AddUser"
5-
import AddItem from "../Items/AddItem"
6-
75
interface NavbarProps {
86
type: string
7+
addModalAs: ComponentType | ElementType
98
}
109

11-
const Navbar = ({ type }: NavbarProps) => {
12-
const addUserModal = useDisclosure()
13-
const addItemModal = useDisclosure()
14-
10+
const Navbar = ({ type, addModalAs }: NavbarProps) => {
11+
const addModal = useDisclosure()
12+
const AddModal = addModalAs
1513
return (
1614
<>
1715
<Flex py={8} gap={4}>
@@ -26,12 +24,11 @@ const Navbar = ({ type }: NavbarProps) => {
2624
variant="primary"
2725
gap={1}
2826
fontSize={{ base: "sm", md: "inherit" }}
29-
onClick={type === "User" ? addUserModal.onOpen : addItemModal.onOpen}
27+
onClick={addModal.onOpen}
3028
>
3129
<Icon as={FaPlus} /> Add {type}
3230
</Button>
33-
<AddUser isOpen={addUserModal.isOpen} onClose={addUserModal.onClose} />
34-
<AddItem isOpen={addItemModal.isOpen} onClose={addItemModal.onClose} />
31+
<AddModal isOpen={addModal.isOpen} onClose={addModal.onClose} />
3532
</Flex>
3633
</>
3734
)

frontend/src/routes/_layout/admin.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { useQueryClient, useSuspenseQuery } from "@tanstack/react-query"
1717
import { createFileRoute } from "@tanstack/react-router"
1818

19+
import AddUser from "@/components/Admin/AddUser"
1920
import { Suspense } from "react"
2021
import { type UserPublic, UsersService } from "../../client"
2122
import ActionsMenu from "../../components/Common/ActionsMenu"
@@ -93,7 +94,7 @@ function Admin() {
9394
<Heading size="lg" textAlign={{ base: "center", md: "left" }} pt={12}>
9495
User Management
9596
</Heading>
96-
<Navbar type={"User"} />
97+
<Navbar type={"User"} addModalAs={AddUser} />
9798
<TableContainer>
9899
<Table fontSize="md" size={{ base: "sm", md: "md" }}>
99100
<Thead>

frontend/src/routes/_layout/items.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useQuery, useQueryClient } from "@tanstack/react-query"
1616
import { createFileRoute, useNavigate } from "@tanstack/react-router"
1717
import { z } from "zod"
1818

19+
import AddItem from "@/components/Items/AddItem"
1920
import { useEffect } from "react"
2021
import { ItemsService } from "../../client"
2122
import ActionsMenu from "../../components/Common/ActionsMenu"
@@ -135,7 +136,7 @@ function Items() {
135136
Items Management
136137
</Heading>
137138

138-
<Navbar type={"Item"} />
139+
<Navbar type={"Item"} addModalAs={AddItem} />
139140
<ItemsTable />
140141
</Container>
141142
)

0 commit comments

Comments
 (0)