create member form funcitonnal
This commit is contained in:
@@ -36,6 +36,7 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
|
fmt.Println(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
Status: core.Error,
|
Status: core.Error,
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ func HandleCreateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
}.Respond(w, http.StatusNotAcceptable)
|
}.Respond(w, http.StatusNotAcceptable)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println("body : ", body )
|
||||||
var user core.User
|
var user core.User
|
||||||
err = json.Unmarshal(body, &user)
|
err = json.Unmarshal(body, &user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -30,7 +31,7 @@ func HandleCreateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println(user)
|
log.Println("User : ", user)
|
||||||
|
|
||||||
res, err := user.Insert(context.Background())
|
res, err := user.Insert(context.Background())
|
||||||
log.Println(res)
|
log.Println(res)
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
|||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
||||||
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
|
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
||||||
|
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
||||||
|
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gorilla/csrf v1.7.2 h1:oTUjx0vyf2T+wkrx09Trsev1TE+/EbDAeHtSTbtC2eI=
|
github.com/gorilla/csrf v1.7.2 h1:oTUjx0vyf2T+wkrx09Trsev1TE+/EbDAeHtSTbtC2eI=
|
||||||
|
|||||||
@@ -21,25 +21,45 @@ import {
|
|||||||
CardTitle,
|
CardTitle,
|
||||||
} from "@/components/ui/card";
|
} from "@/components/ui/card";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
|
import useApiMutation from "@/hooks/use-api";
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectGroup,
|
||||||
|
SelectItem,
|
||||||
|
SelectLabel,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from "@/components/ui/select"
|
||||||
|
|
||||||
const formSchema = z.object({
|
const formSchema = z.object({
|
||||||
firstname: z.string().min(2, { message: "Prénom trop court" }),
|
firstname: z.string().min(2, { message: "Prénom trop court." }),
|
||||||
lastname: z.string().min(2, { message: "Nom trop court" }),
|
lastname: z.string().min(2, { message: "Nom trop court." }),
|
||||||
|
role: z.enum(["admin", "user"], { message: "Rôle invalide." }),
|
||||||
phone: z
|
phone: z
|
||||||
.string()
|
.string()
|
||||||
.min(10, { message: "Un numéro de téléphone à 10 chiffres" }),
|
.max(10, { message: "Un numéro de téléphone à 10 chiffres." })
|
||||||
email: z.string().email({ message: "Invalid email address" }),
|
.optional(),
|
||||||
message: z
|
email: z.string().email({ message: "Email invalide." })
|
||||||
.string()
|
|
||||||
.min(10, { message: "Message must be at least 10 characters long" }),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default function ContactFormPreview() {
|
export default function CreateMemberForm() {
|
||||||
|
const {
|
||||||
|
trigger
|
||||||
|
} = useApiMutation(
|
||||||
|
"/users/new",
|
||||||
|
{ onSuccess: () => console.log("Member created") },
|
||||||
|
"POST",
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
|
||||||
const form = useForm<z.infer<typeof formSchema>>({
|
const form = useForm<z.infer<typeof formSchema>>({
|
||||||
resolver: zodResolver(formSchema),
|
resolver: zodResolver(formSchema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
firstname: "",
|
firstname: "",
|
||||||
lastname: "",
|
lastname: "",
|
||||||
|
role: "user",
|
||||||
phone: "",
|
phone: "",
|
||||||
email: "",
|
email: "",
|
||||||
},
|
},
|
||||||
@@ -47,25 +67,24 @@ export default function ContactFormPreview() {
|
|||||||
|
|
||||||
async function onSubmit(values: z.infer<typeof formSchema>) {
|
async function onSubmit(values: z.infer<typeof formSchema>) {
|
||||||
try {
|
try {
|
||||||
console.log(values);
|
const res = await trigger(values)
|
||||||
await fetch("/api/users/new", {
|
if (!res) throw new Error("The server hasn't responded.");
|
||||||
method: "POST",
|
if (res.status === "Error") throw new Error(res.message);
|
||||||
body: JSON.stringify(values),
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Form submission error", error);
|
console.error("Form submission error", error);
|
||||||
}
|
}
|
||||||
console.log("subimted");
|
console.log("submited");
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex min-h-[60vh] h-full w-full items-center justify-center px-4">
|
<div className="flex min-h-[60vh] h-full w-full items-center justify-center px-4">
|
||||||
<Card className="mx-auto max-w-md">
|
<Card className="mx-auto max-w-md">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle className="text-2xl">Contact Us</CardTitle>
|
<CardTitle className="text-2xl">Création de membre</CardTitle>
|
||||||
<CardDescription>
|
<CardDescription>
|
||||||
Please fill out the form below and we will get back to
|
Remplissez les différents champs pour créer un membre.
|
||||||
you shortly.
|
Un code sera envoyé par mail à l'utilisateur.
|
||||||
|
Il pourra ensuite modifier le mot de passe de son compte avec ce code.
|
||||||
</CardDescription>
|
</CardDescription>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
@@ -90,6 +109,7 @@ export default function ContactFormPreview() {
|
|||||||
placeholder="John Doe"
|
placeholder="John Doe"
|
||||||
type="text"
|
type="text"
|
||||||
autoComplete="firstname"
|
autoComplete="firstname"
|
||||||
|
|
||||||
{...field}
|
{...field}
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
@@ -113,6 +133,7 @@ export default function ContactFormPreview() {
|
|||||||
placeholder="John Doe"
|
placeholder="John Doe"
|
||||||
type="text"
|
type="text"
|
||||||
autoComplete="lastname"
|
autoComplete="lastname"
|
||||||
|
|
||||||
{...field}
|
{...field}
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
@@ -136,6 +157,7 @@ export default function ContactFormPreview() {
|
|||||||
placeholder="johndoe@mail.com"
|
placeholder="johndoe@mail.com"
|
||||||
type="email"
|
type="email"
|
||||||
autoComplete="email"
|
autoComplete="email"
|
||||||
|
|
||||||
{...field}
|
{...field}
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
@@ -144,7 +166,36 @@ export default function ContactFormPreview() {
|
|||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Message Field */}
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="role"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem className="grid gap-2">
|
||||||
|
<FormLabel htmlFor="role">
|
||||||
|
Role
|
||||||
|
</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Select
|
||||||
|
value={field.value}
|
||||||
|
onValueChange={(value: string) => field.onChange(value)}>
|
||||||
|
<SelectTrigger className="w-[180px]">
|
||||||
|
<SelectValue placeholder="Select a role" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectGroup>
|
||||||
|
<SelectLabel>Role</SelectLabel>
|
||||||
|
<SelectItem value="admin">Administrateur</SelectItem>
|
||||||
|
<SelectItem value="user">Utilisateur</SelectItem>
|
||||||
|
</SelectGroup>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Phone Field */}
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="phone"
|
name="phone"
|
||||||
@@ -157,8 +208,9 @@ export default function ContactFormPreview() {
|
|||||||
<Input
|
<Input
|
||||||
id="phone"
|
id="phone"
|
||||||
placeholder="0648265441"
|
placeholder="0648265441"
|
||||||
type="number"
|
type="tel"
|
||||||
autoComplete="phone"
|
autoComplete="phone"
|
||||||
|
|
||||||
{...field}
|
{...field}
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
|
|||||||
Reference in New Issue
Block a user