-
Notifications
You must be signed in to change notification settings - Fork 0
DBP-125: 可以在 admin 頁面輕鬆加點數 #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
The head ref may contain hidden characters: "pan93412/dbp-125-\u53EF\u4EE5\u5728-admin-\u9801\u9762\u8F15\u9B06\u52A0\u9EDE\u6578"
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
app/(admin)/(activity-management)/points/_components/create.tsx
This file contains hidden or 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,129 @@ | ||
| "use client"; | ||
|
|
||
| import { buttonVariants } from "@/components/ui/button"; | ||
| import { ConfirmationDialog } from "@/components/ui/confirmation-dialog"; | ||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
| DialogDescription, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| DialogTrigger, | ||
| } from "@/components/ui/dialog"; | ||
| import { graphql } from "@/gql"; | ||
| import { useDialogCloseConfirmation } from "@/hooks/use-dialog-close-confirmation"; | ||
| import { useMutation } from "@apollo/client/react"; | ||
| import { useState } from "react"; | ||
| import { toast } from "sonner"; | ||
| import { POINTS_TABLE_QUERY } from "./data-table"; | ||
| import { UpdatePointsForm } from "./update-form"; | ||
|
|
||
| const CREATE_POINT_MUTATION = graphql(` | ||
| mutation CreatePoint($input: CreatePointInput!) { | ||
| createPoint(input: $input) { | ||
| id | ||
| } | ||
| } | ||
| `); | ||
|
|
||
| export function CreatePointTrigger() { | ||
| const [open, setOpen] = useState(false); | ||
| const [isFormDirty, setIsFormDirty] = useState(false); | ||
|
|
||
| const { | ||
| showConfirmation, | ||
| handleDialogOpenChange, | ||
| handleConfirmClose, | ||
| handleCancelClose, | ||
| } = useDialogCloseConfirmation({ | ||
| isDirty: isFormDirty, | ||
| setOpen, | ||
| onConfirmedClose: () => { | ||
| setIsFormDirty(false); | ||
| }, | ||
| }); | ||
|
|
||
| const handleFormStateChange = (isDirty: boolean) => { | ||
| setIsFormDirty(isDirty); | ||
| }; | ||
|
|
||
| const handleCompleted = () => { | ||
| setIsFormDirty(false); | ||
| setOpen(false); | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Dialog open={open} onOpenChange={handleDialogOpenChange}> | ||
| <DialogTrigger className={buttonVariants()}>給予點數</DialogTrigger> | ||
| <CreatePointDialogContent | ||
| onCompleted={handleCompleted} | ||
| onFormStateChange={handleFormStateChange} | ||
| /> | ||
| </Dialog> | ||
|
|
||
| <ConfirmationDialog | ||
| open={showConfirmation} | ||
| onOpenChange={() => {}} | ||
| onConfirm={handleConfirmClose} | ||
| onCancel={handleCancelClose} | ||
| /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| function CreatePointDialogContent({ | ||
| onCompleted, | ||
| onFormStateChange, | ||
| }: { | ||
| onCompleted: () => void; | ||
| onFormStateChange: (isDirty: boolean) => void; | ||
| }) { | ||
| const [createPoint] = useMutation(CREATE_POINT_MUTATION, { | ||
| refetchQueries: [POINTS_TABLE_QUERY], | ||
|
|
||
| onError(error) { | ||
| toast.error("給予點數失敗", { | ||
| description: error.message, | ||
| }); | ||
| }, | ||
|
|
||
| onCompleted() { | ||
| toast.success("給予點數成功"); | ||
| onCompleted(); | ||
| }, | ||
| }); | ||
|
|
||
| const onSubmit = (formData: { userID: string; points: number; description?: string }) => { | ||
| createPoint({ | ||
| variables: { | ||
| input: { | ||
| userID: formData.userID, | ||
| points: formData.points, | ||
| description: formData.description, | ||
| }, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <DialogContent className="max-h-[85vh] max-w-3xl overflow-y-auto"> | ||
| <DialogHeader> | ||
| <DialogTitle>給予點數</DialogTitle> | ||
| <DialogDescription> | ||
| 給一個使用者手動發放點數。 | ||
| </DialogDescription> | ||
| </DialogHeader> | ||
| <UpdatePointsForm | ||
| defaultValues={{ | ||
| userID: "", | ||
| points: 0, | ||
| description: "", | ||
| }} | ||
| onSubmit={onSubmit} | ||
| action="create" | ||
| onFormStateChange={onFormStateChange} | ||
| /> | ||
| </DialogContent> | ||
| ); | ||
| } |
This file contains hidden or 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
33 changes: 0 additions & 33 deletions
33
app/(admin)/(activity-management)/points/_components/query.ts
This file was deleted.
Oops, something went wrong.
151 changes: 151 additions & 0 deletions
151
app/(admin)/(activity-management)/points/_components/update-form.tsx
This file contains hidden or 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,151 @@ | ||
| import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { Spinner } from "@/components/ui/spinner"; | ||
| import { Textarea } from "@/components/ui/textarea"; | ||
| import { UpdateFormBody } from "@/components/update-modal/form-body"; | ||
| import type { UpdateFormBaseProps } from "@/components/update-modal/types"; | ||
| import { graphql } from "@/gql"; | ||
| import { skipToken, useQuery } from "@apollo/client/react"; | ||
| import { zodResolver } from "@hookform/resolvers/zod"; | ||
| import { useDebouncedValue } from "foxact/use-debounced-value"; | ||
| import { useForm, useWatch } from "react-hook-form"; | ||
| import { z } from "zod"; | ||
|
|
||
| export const formSchema = z.object({ | ||
| userID: z.string(), | ||
| points: z.number(), | ||
| description: z.string().optional(), | ||
| }); | ||
|
|
||
| export type UpdatePointsFormData = z.infer<typeof formSchema>; | ||
|
|
||
| export interface UpdatePointsFormProps extends Omit<UpdateFormBaseProps<z.infer<typeof formSchema>>, "onSubmit"> { | ||
| onSubmit: (newValues: { | ||
| userID: string; | ||
| points: number; | ||
| description?: string; | ||
| }) => void; | ||
| } | ||
|
|
||
| const UPDATE_POINTS_FORM_USER_INFO_QUERY = graphql(` | ||
| query UpdatePointsFormUserInfo($id: ID!) { | ||
| user(id: $id) { | ||
| id | ||
| name | ||
| } | ||
| } | ||
| `); | ||
|
|
||
| export function UpdatePointsForm({ | ||
| defaultValues, | ||
| onSubmit, | ||
| action, | ||
| onFormStateChange, | ||
| }: UpdatePointsFormProps) { | ||
| const form = useForm<z.infer<typeof formSchema>>({ | ||
| resolver: zodResolver(formSchema), | ||
| defaultValues: { | ||
| userID: "", | ||
| points: 0, | ||
| description: "", | ||
| ...defaultValues, | ||
| } as z.infer<typeof formSchema>, | ||
| }); | ||
|
|
||
| const userID = useWatch({ control: form.control, name: "userID" }); | ||
| const userIDDebounced = useDebouncedValue(userID, 200); | ||
|
|
||
| const { data: userInfoData, loading } = useQuery( | ||
| UPDATE_POINTS_FORM_USER_INFO_QUERY, | ||
| userIDDebounced | ||
| ? { | ||
| variables: { | ||
| id: userIDDebounced, | ||
| }, | ||
pan93412 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| errorPolicy: "ignore", | ||
| } | ||
| : skipToken, | ||
| ); | ||
|
|
||
| const handleSubmit = (data: z.infer<typeof formSchema>) => { | ||
| onSubmit({ | ||
| userID: data.userID, | ||
| points: data.points, | ||
| description: data.description, | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <UpdateFormBody | ||
| form={form} | ||
| onSubmit={handleSubmit} | ||
| action={action} | ||
| onFormStateChange={onFormStateChange} | ||
| > | ||
| <FormField | ||
| control={form.control} | ||
| name="userID" | ||
| render={({ field }) => ( | ||
| <FormItem> | ||
| <FormLabel>使用者 ID</FormLabel> | ||
| <FormControl> | ||
| <Input {...field} placeholder="請輸入使用者 ID" /> | ||
| </FormControl> | ||
| <FormDescription> | ||
| <div> | ||
| 選擇要發放點數的使用者。可以到使用者管理頁面確認對應代號。 | ||
| </div> | ||
|
|
||
| <div className="flex items-center gap-4"> | ||
| {loading ? <Spinner className="mr-4 inline-block size-4" /> : null} | ||
| {userInfoData?.user | ||
| ? `您正要發放給:${userInfoData.user.name} (${userInfoData.user.email})` | ||
| : "您輸入的使用者 ID 不存在。"} | ||
| </div> | ||
| </FormDescription> | ||
| <FormMessage /> | ||
| </FormItem> | ||
| )} | ||
| /> | ||
|
|
||
| <FormField | ||
| control={form.control} | ||
| name="points" | ||
| render={() => ( | ||
| <FormItem> | ||
| <FormLabel>點數</FormLabel> | ||
| <FormControl> | ||
| <Input | ||
| {...form.register("points", { valueAsNumber: true })} | ||
| type="number" | ||
| placeholder="請輸入要發放的點數" | ||
| /> | ||
| </FormControl> | ||
| <FormDescription>要發放給使用者的點數數量。</FormDescription> | ||
| <FormMessage /> | ||
| </FormItem> | ||
| )} | ||
| /> | ||
|
|
||
| <FormField | ||
| control={form.control} | ||
| name="description" | ||
| render={({ field }) => ( | ||
| <FormItem> | ||
| <FormLabel>備註(可選)</FormLabel> | ||
| <FormControl> | ||
| <Textarea | ||
| {...field} | ||
| placeholder="請輸入發放點數的原因或備註" | ||
| className="min-h-[80px]" | ||
| /> | ||
| </FormControl> | ||
| <FormDescription>發放點數的原因說明。</FormDescription> | ||
| <FormMessage /> | ||
| </FormItem> | ||
| )} | ||
| /> | ||
| </UpdateFormBody> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.