|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { User } from "@/backend/models/domain-models"; |
| 4 | +import { UserActionInput } from "@/backend/services/inputs/user.input"; |
| 5 | +import * as userActions from "@/backend/services/user.action"; |
| 6 | +import { Button } from "@/components/ui/button"; |
| 7 | +import { |
| 8 | + Form, |
| 9 | + FormControl, |
| 10 | + FormDescription, |
| 11 | + FormField, |
| 12 | + FormItem, |
| 13 | + FormLabel, |
| 14 | + FormMessage, |
| 15 | +} from "@/components/ui/form"; |
| 16 | +import { Input } from "@/components/ui/input"; |
| 17 | +import { useTranslation } from "@/i18n/use-translation"; |
| 18 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 19 | +import { useMutation } from "@tanstack/react-query"; |
| 20 | +import { Loader } from "lucide-react"; |
1 | 21 | import React from "react"; |
| 22 | +import { SubmitHandler, useForm } from "react-hook-form"; |
| 23 | +import { toast } from "sonner"; |
| 24 | +import z from "zod"; |
| 25 | + |
| 26 | +interface Props { |
| 27 | + user: User; |
| 28 | +} |
| 29 | + |
| 30 | +const SocialMediaForm: React.FC<Props> = ({ user }) => { |
| 31 | + const { _t } = useTranslation(); |
| 32 | + const mutation = useMutation({ |
| 33 | + mutationFn: ( |
| 34 | + payload: z.infer<typeof UserActionInput.updateMyProfileInput> |
| 35 | + ) => userActions.updateMyProfile(payload), |
| 36 | + onSuccess: () => { |
| 37 | + toast(_t("Social links updated successfully")); |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const form = useForm({ |
| 42 | + mode: "all", |
| 43 | + defaultValues: { |
| 44 | + social_links: { |
| 45 | + github: user.social_links.github, |
| 46 | + x: user.social_links.x, |
| 47 | + linkedin: user.social_links.linkedin, |
| 48 | + facebook: user.social_links.facebook, |
| 49 | + instagram: user.social_links.instagram, |
| 50 | + youtube: user.social_links.youtube, |
| 51 | + }, |
| 52 | + }, |
| 53 | + resolver: zodResolver(UserActionInput.updateMyProfileInput), |
| 54 | + }); |
| 55 | + |
| 56 | + const onSubmit: SubmitHandler< |
| 57 | + z.infer<typeof UserActionInput.updateMyProfileInput> |
| 58 | + > = (data) => { |
| 59 | + mutation.mutate({ |
| 60 | + social_links: { |
| 61 | + github: data.social_links?.github, |
| 62 | + x: data.social_links?.x, |
| 63 | + linkedin: data.social_links?.linkedin, |
| 64 | + facebook: data.social_links?.facebook, |
| 65 | + instagram: data.social_links?.instagram, |
| 66 | + youtube: data.social_links?.youtube, |
| 67 | + }, |
| 68 | + }); |
| 69 | + }; |
| 70 | + |
| 71 | + return ( |
| 72 | + <Form {...form}> |
| 73 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 74 | + <FormField |
| 75 | + control={form.control} |
| 76 | + name="social_links.github" |
| 77 | + render={({ field }) => ( |
| 78 | + <FormItem> |
| 79 | + <FormLabel>{_t("Github")}</FormLabel> |
| 80 | + <FormControl> |
| 81 | + <Input className="py-6" {...field} /> |
| 82 | + </FormControl> |
| 83 | + <FormDescription /> |
| 84 | + <FormMessage /> |
| 85 | + </FormItem> |
| 86 | + )} |
| 87 | + /> |
| 88 | + |
| 89 | + <FormField |
| 90 | + control={form.control} |
| 91 | + name="social_links.facebook" |
| 92 | + render={({ field }) => ( |
| 93 | + <FormItem> |
| 94 | + <FormLabel>{_t("Facebook")}</FormLabel> |
| 95 | + <FormControl> |
| 96 | + <Input className="py-6" {...field} /> |
| 97 | + </FormControl> |
| 98 | + <FormDescription /> |
| 99 | + <FormMessage /> |
| 100 | + </FormItem> |
| 101 | + )} |
| 102 | + /> |
| 103 | + |
| 104 | + <FormField |
| 105 | + control={form.control} |
| 106 | + name="social_links.instagram" |
| 107 | + render={({ field }) => ( |
| 108 | + <FormItem> |
| 109 | + <FormLabel>{_t("Instagram")}</FormLabel> |
| 110 | + <FormControl> |
| 111 | + <Input className="py-6" {...field} /> |
| 112 | + </FormControl> |
| 113 | + <FormDescription /> |
| 114 | + <FormMessage /> |
| 115 | + </FormItem> |
| 116 | + )} |
| 117 | + /> |
| 118 | + |
| 119 | + <FormField |
| 120 | + control={form.control} |
| 121 | + name="social_links.linkedin" |
| 122 | + render={({ field }) => ( |
| 123 | + <FormItem> |
| 124 | + <FormLabel>{_t("LinkedIn")}</FormLabel> |
| 125 | + <FormControl> |
| 126 | + <Input className="py-6" {...field} /> |
| 127 | + </FormControl> |
| 128 | + <FormDescription /> |
| 129 | + <FormMessage /> |
| 130 | + </FormItem> |
| 131 | + )} |
| 132 | + /> |
| 133 | + |
| 134 | + <FormField |
| 135 | + control={form.control} |
| 136 | + name="social_links.x" |
| 137 | + render={({ field }) => ( |
| 138 | + <FormItem> |
| 139 | + <FormLabel>{_t("X")}</FormLabel> |
| 140 | + <FormControl> |
| 141 | + <Input className="py-6" {...field} /> |
| 142 | + </FormControl> |
| 143 | + <FormDescription /> |
| 144 | + <FormMessage /> |
| 145 | + </FormItem> |
| 146 | + )} |
| 147 | + /> |
| 148 | + |
| 149 | + <FormField |
| 150 | + control={form.control} |
| 151 | + name="social_links.youtube" |
| 152 | + render={({ field }) => ( |
| 153 | + <FormItem> |
| 154 | + <FormLabel>{_t("Youtube")}</FormLabel> |
| 155 | + <FormControl> |
| 156 | + <Input className="py-6" {...field} /> |
| 157 | + </FormControl> |
| 158 | + <FormDescription /> |
| 159 | + <FormMessage /> |
| 160 | + </FormItem> |
| 161 | + )} |
| 162 | + /> |
2 | 163 |
|
3 | | -const SocialMediaForm = () => { |
4 | | - return <div>SocialMediaForm</div>; |
| 164 | + <Button |
| 165 | + type="submit" |
| 166 | + disabled={mutation.isPending || !form.formState.isValid} |
| 167 | + > |
| 168 | + {mutation.isPending && <Loader className="animate-spin" />} |
| 169 | + {_t("Save")} |
| 170 | + </Button> |
| 171 | + </form> |
| 172 | + </Form> |
| 173 | + ); |
5 | 174 | }; |
6 | 175 |
|
7 | 176 | export default SocialMediaForm; |
0 commit comments