-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add SAResponse type for Server Actions * Refactor JSON data component to use button variants * Add wallet form component and update user page * Update UI components and add separators
- Loading branch information
Showing
7 changed files
with
187 additions
and
19 deletions.
There are no files selected for viewing
14 changes: 9 additions & 5 deletions
14
app/app/(dashboard)/dashboard/cadet/[login]/_components/json-data/index.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
25 changes: 25 additions & 0 deletions
25
app/app/(dashboard)/dashboard/cadet/[login]/_components/wallet-form/actions.ts
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,25 @@ | ||
"use server"; | ||
|
||
import { transactions } from "@/lib/intra/users"; | ||
import { SAResponse } from "@/types/sa-response"; | ||
import { IAddAlt } from "./types"; | ||
import { revalidateTag } from "next/cache"; | ||
|
||
/** | ||
* Adds an alternative money | ||
* @param payload - The payload | ||
* @returns A promise that resolves to a SAResponse object with a boolean indicating the success of the operation. | ||
*/ | ||
export async function addAlt(payload: IAddAlt): Promise<SAResponse<boolean>> { | ||
try { | ||
const r = await transactions({ | ||
...payload, | ||
transactable_type: "Tuteur api", | ||
reason: "cadeau", | ||
}); | ||
revalidateTag(payload.login); | ||
return { data: true, error: null }; | ||
} catch (e) { | ||
return { data: null, error: "Failed to add Alt" }; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
app/app/(dashboard)/dashboard/cadet/[login]/_components/wallet-form/index.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,98 @@ | ||
"use client"; | ||
|
||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { useForm } from "react-hook-form"; | ||
import { z } from "zod"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { | ||
Form, | ||
FormControl, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from "@/components/ui/form"; | ||
import { Input } from "@/components/ui/input"; | ||
import { addAlt } from "./actions"; | ||
import { IWalletForm } from "./types"; | ||
import { toast } from "sonner"; | ||
import { useState } from "react"; | ||
const FormSchema = z.object({ | ||
value: z.coerce.number().int(), | ||
}); | ||
|
||
export function WalletForm(props: IWalletForm) { | ||
const [currentValue, setCurrentValue] = useState(props.currentValue); | ||
const [prevValue, setPrevValue] = useState(props.currentValue); | ||
const [isPending, setIsPending] = useState(false); | ||
const form = useForm<z.infer<typeof FormSchema>>({ | ||
resolver: zodResolver(FormSchema), | ||
defaultValues: { | ||
value: 0, | ||
}, | ||
}); | ||
|
||
async function onSubmit(data: z.infer<typeof FormSchema>) { | ||
if (data.value === 0) { | ||
toast.error("Why are you tring to add 0?"); | ||
return; | ||
} | ||
setIsPending(true); | ||
setPrevValue(currentValue); | ||
setCurrentValue(currentValue + data.value); | ||
const { data: respData, error } = await addAlt({ | ||
value: data.value, | ||
user_id: props.user_id, | ||
login: props.login, | ||
}); | ||
if (error) { | ||
setCurrentValue(prevValue); | ||
setIsPending(false); | ||
toast.error(error); | ||
} else if (respData) { | ||
setIsPending(false); | ||
toast.success("Alt added"); | ||
form.reset(); | ||
} | ||
} | ||
|
||
return ( | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="w-1/3 space-y-6"> | ||
<FormField | ||
control={form.control} | ||
name="value" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Add, or remove $Alt</FormLabel> | ||
<div className="flex"> | ||
<Input | ||
className="rounded-r-none min-w-[100px]" | ||
value={`$Alt ${currentValue}`} | ||
readOnly | ||
/> | ||
<FormControl> | ||
<Input | ||
className="rounded-none min-w-[100px]" | ||
type="number" | ||
step={100} | ||
{...field} | ||
/> | ||
</FormControl> | ||
<Button | ||
className="rounded-l-none" | ||
type="submit" | ||
disabled={isPending} | ||
> | ||
Add | ||
</Button> | ||
</div> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</form> | ||
</Form> | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
app/app/(dashboard)/dashboard/cadet/[login]/_components/wallet-form/types.ts
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,11 @@ | ||
export interface IAddAlt { | ||
value: number; | ||
user_id: number; | ||
login: string; | ||
} | ||
|
||
export interface IWalletForm { | ||
currentValue: number; | ||
user_id: number; | ||
login: string; | ||
} |
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,7 @@ | ||
/** | ||
* Represents a response object for Server Actions. | ||
* @template T - The type of the data. | ||
*/ | ||
export type SAResponse<T> = | ||
| { data: T; error: null } | ||
| { data: null; error: string }; |