-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71b5f9c
commit b0478e3
Showing
15 changed files
with
407 additions
and
37 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
132 changes: 132 additions & 0 deletions
132
web/src/components/modals/admin/CategoryCreateModal.tsx
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,132 @@ | ||
import { useCategoryApi } from "@/api/category"; | ||
import MDIcon from "@/components/ui/MDIcon"; | ||
import { showSuccessNotification } from "@/utils/notification"; | ||
import { | ||
Box, | ||
Button, | ||
Card, | ||
Divider, | ||
Flex, | ||
Modal, | ||
ModalProps, | ||
Stack, | ||
TextInput, | ||
Text, | ||
ColorInput, | ||
} from "@mantine/core"; | ||
import { useForm, zodResolver } from "@mantine/form"; | ||
import { useEffect } from "react"; | ||
import { z } from "zod"; | ||
|
||
interface CategoryCreateModalProps extends ModalProps { | ||
setRefresh: () => void; | ||
} | ||
|
||
export default function CategoryCreateModal(props: CategoryCreateModalProps) { | ||
const { setRefresh, ...modalProps } = props; | ||
const categoryApi = useCategoryApi(); | ||
|
||
const form = useForm({ | ||
mode: "controlled", | ||
initialValues: { | ||
name: "", | ||
color: "", | ||
icon: "", | ||
}, | ||
validate: zodResolver( | ||
z.object({ | ||
name: z.string(), | ||
}) | ||
), | ||
}); | ||
|
||
function createCategory() { | ||
categoryApi | ||
.createCategory({ | ||
name: form.getValues().name, | ||
icon: form.getValues().icon, | ||
color: form.getValues().color, | ||
}) | ||
.then((_) => { | ||
showSuccessNotification({ | ||
message: `分类 ${form.getValues().name} 创建成功`, | ||
}); | ||
setRefresh(); | ||
modalProps.onClose(); | ||
}); | ||
} | ||
|
||
useEffect(() => { | ||
form.reset(); | ||
}, [modalProps.opened]); | ||
|
||
return ( | ||
<> | ||
<Modal.Root {...modalProps}> | ||
<Modal.Overlay /> | ||
<Modal.Content | ||
sx={{ | ||
flex: "none", | ||
backgroundColor: "transparent", | ||
}} | ||
> | ||
<Card | ||
shadow="md" | ||
padding={"lg"} | ||
radius={"md"} | ||
withBorder | ||
w={"40rem"} | ||
> | ||
<Flex gap={10} align={"center"}> | ||
<MDIcon>collections_bookmark</MDIcon> | ||
<Text fw={600}>创建分类</Text> | ||
</Flex> | ||
<Divider my={10} /> | ||
<Box p={10}> | ||
<form | ||
onSubmit={form.onSubmit((_) => | ||
createCategory() | ||
)} | ||
> | ||
<Stack gap={10}> | ||
<TextInput | ||
label="分类名" | ||
withAsterisk | ||
key={form.key("name")} | ||
{...form.getInputProps("name")} | ||
/> | ||
<ColorInput | ||
label="颜色" | ||
key={form.key("color")} | ||
{...form.getInputProps("color")} | ||
/> | ||
<TextInput | ||
label="图标" | ||
withAsterisk | ||
leftSection={ | ||
<MDIcon> | ||
{form.getValues().icon} | ||
</MDIcon> | ||
} | ||
key={form.key("icon")} | ||
{...form.getInputProps("icon")} | ||
/> | ||
</Stack> | ||
<Flex mt={20} justify={"end"}> | ||
<Button | ||
type="submit" | ||
leftSection={ | ||
<MDIcon c={"white"}>check</MDIcon> | ||
} | ||
> | ||
创建 | ||
</Button> | ||
</Flex> | ||
</form> | ||
</Box> | ||
</Card> | ||
</Modal.Content> | ||
</Modal.Root> | ||
</> | ||
); | ||
} |
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,157 @@ | ||
import { useCategoryApi } from "@/api/category"; | ||
import MDIcon from "@/components/ui/MDIcon"; | ||
import { Category } from "@/types/category"; | ||
import { showSuccessNotification } from "@/utils/notification"; | ||
import { | ||
Box, | ||
Button, | ||
Card, | ||
Divider, | ||
Flex, | ||
Modal, | ||
ModalProps, | ||
Stack, | ||
TextInput, | ||
Text, | ||
ColorInput, | ||
} from "@mantine/core"; | ||
import { useForm, zodResolver } from "@mantine/form"; | ||
import { useEffect, useState } from "react"; | ||
import { z } from "zod"; | ||
|
||
interface CategoryEditModalProps extends ModalProps { | ||
setRefresh: () => void; | ||
categoryID: number; | ||
} | ||
|
||
export default function CategoryEditModal(props: CategoryEditModalProps) { | ||
const { setRefresh, categoryID, ...modalProps } = props; | ||
const categoryApi = useCategoryApi(); | ||
|
||
const [category, setCategory] = useState<Category>(); | ||
|
||
const form = useForm({ | ||
mode: "controlled", | ||
initialValues: { | ||
name: "", | ||
color: "", | ||
icon: "", | ||
}, | ||
validate: zodResolver( | ||
z.object({ | ||
name: z.string(), | ||
}) | ||
), | ||
}); | ||
|
||
function getCategory() { | ||
categoryApi.getCategories().then((res) => { | ||
const r = res.data; | ||
setCategory(r?.data?.find((c: Category) => c.id === categoryID)); | ||
}); | ||
} | ||
|
||
function updateCategory() { | ||
categoryApi | ||
.updateCategory({ | ||
id: categoryID, | ||
name: form.getValues().name, | ||
icon: form.getValues().icon, | ||
color: form.getValues().color, | ||
}) | ||
.then((_) => { | ||
showSuccessNotification({ | ||
message: `分类 ${form.getValues().name} 更新成功`, | ||
}); | ||
setRefresh(); | ||
modalProps.onClose(); | ||
}); | ||
} | ||
|
||
useEffect(() => { | ||
form.reset(); | ||
if (modalProps.opened) { | ||
getCategory(); | ||
} | ||
}, [modalProps.opened]); | ||
|
||
useEffect(() => { | ||
if (category) { | ||
form.setValues({ | ||
name: category.name, | ||
color: category.color, | ||
icon: category.icon, | ||
}); | ||
} | ||
}, [category]); | ||
|
||
return ( | ||
<> | ||
<Modal.Root {...modalProps}> | ||
<Modal.Overlay /> | ||
<Modal.Content | ||
sx={{ | ||
flex: "none", | ||
backgroundColor: "transparent", | ||
}} | ||
> | ||
<Card | ||
shadow="md" | ||
padding={"lg"} | ||
radius={"md"} | ||
withBorder | ||
w={"40rem"} | ||
> | ||
<Flex gap={10} align={"center"}> | ||
<MDIcon>collections_bookmark</MDIcon> | ||
<Text fw={600}>更新分类</Text> | ||
</Flex> | ||
<Divider my={10} /> | ||
<Box p={10}> | ||
<form | ||
onSubmit={form.onSubmit((_) => | ||
updateCategory() | ||
)} | ||
> | ||
<Stack gap={10}> | ||
<TextInput | ||
label="分类名" | ||
withAsterisk | ||
key={form.key("name")} | ||
{...form.getInputProps("name")} | ||
/> | ||
<ColorInput | ||
label="颜色" | ||
key={form.key("color")} | ||
{...form.getInputProps("color")} | ||
/> | ||
<TextInput | ||
label="图标" | ||
withAsterisk | ||
leftSection={ | ||
<MDIcon> | ||
{form.getValues().icon} | ||
</MDIcon> | ||
} | ||
key={form.key("icon")} | ||
{...form.getInputProps("icon")} | ||
/> | ||
</Stack> | ||
<Flex mt={20} justify={"end"}> | ||
<Button | ||
type="submit" | ||
leftSection={ | ||
<MDIcon c={"white"}>check</MDIcon> | ||
} | ||
> | ||
保存 | ||
</Button> | ||
</Flex> | ||
</form> | ||
</Box> | ||
</Card> | ||
</Modal.Content> | ||
</Modal.Root> | ||
</> | ||
); | ||
} |
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 |
---|---|---|
|
@@ -8,7 +8,6 @@ import { | |
Modal, | ||
ModalProps, | ||
TextInput, | ||
ThemeIcon, | ||
Text, | ||
Stack, | ||
Group, | ||
|
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 |
---|---|---|
|
@@ -3,10 +3,8 @@ import { | |
Flex, | ||
Modal, | ||
ModalProps, | ||
ThemeIcon, | ||
Text, | ||
Divider, | ||
TextInput, | ||
Stack, | ||
Button, | ||
Box, | ||
|
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 |
---|---|---|
|
@@ -15,7 +15,6 @@ import { | |
Modal, | ||
ModalProps, | ||
Stack, | ||
ThemeIcon, | ||
Text, | ||
Avatar, | ||
} from "@mantine/core"; | ||
|
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.