Skip to content

Commit 9444815

Browse files
authored
♻️ Abstraction of specific AddModal component out of the Navbar (fastapi#1246)
1 parent 5b5045d commit 9444815

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

frontend/src/components/Common/Navbar.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
import { ComponentType, ElementType } from 'react';
2+
13
import { Button, Flex, Icon, useDisclosure } from "@chakra-ui/react"
24
import { FaPlus } from "react-icons/fa"
35

4-
import AddUser from "../Admin/AddUser"
5-
import AddItem from "../Items/AddItem"
6-
76
interface NavbarProps {
87
type: string
8+
addModalAs: ComponentType | ElementType
99
}
1010

11-
const Navbar = ({ type }: NavbarProps) => {
12-
const addUserModal = useDisclosure()
13-
const addItemModal = useDisclosure()
11+
const Navbar = ({ type, addModalAs }: NavbarProps) => {
12+
const addModal = useDisclosure()
1413

14+
const AddModal = addModalAs
1515
return (
1616
<>
1717
<Flex py={8} gap={4}>
@@ -26,12 +26,11 @@ const Navbar = ({ type }: NavbarProps) => {
2626
variant="primary"
2727
gap={1}
2828
fontSize={{ base: "sm", md: "inherit" }}
29-
onClick={type === "User" ? addUserModal.onOpen : addItemModal.onOpen}
29+
onClick={addModal.onOpen}
3030
>
3131
<Icon as={FaPlus} /> Add {type}
3232
</Button>
33-
<AddUser isOpen={addUserModal.isOpen} onClose={addUserModal.onClose} />
34-
<AddItem isOpen={addItemModal.isOpen} onClose={addItemModal.onClose} />
33+
<AddModal isOpen={addModal.isOpen} onClose={addModal.onClose} />
3534
</Flex>
3635
</>
3736
)

frontend/src/routes/_layout/admin.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Suspense } from "react"
2020
import { type UserPublic, UsersService } from "../../client"
2121
import ActionsMenu from "../../components/Common/ActionsMenu"
2222
import Navbar from "../../components/Common/Navbar"
23+
import AddUser from "../../components/Admin/AddUser"
2324

2425
export const Route = createFileRoute("/_layout/admin")({
2526
component: Admin,
@@ -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
@@ -20,6 +20,7 @@ import { useEffect } from "react"
2020
import { ItemsService } from "../../client"
2121
import ActionsMenu from "../../components/Common/ActionsMenu"
2222
import Navbar from "../../components/Common/Navbar"
23+
import AddItem from "../../components/Items/AddItem"
2324

2425
const itemsSearchSchema = z.object({
2526
page: z.number().catch(1),
@@ -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)