|
| 1 | +import { Button } from "@/shared/ui/button" |
| 2 | +import { Input } from "@/shared/ui/input" |
| 3 | +import { H4, P } from "@/shared/ui/typography" |
| 4 | +import { |
| 5 | + Select, |
| 6 | + SelectTrigger, |
| 7 | + SelectValue, |
| 8 | + SelectContent, |
| 9 | + SelectItem, |
| 10 | +} from "@/shared/ui/select" |
| 11 | +import { format } from "prettier" |
| 12 | +import { ResponseJsonEditor } from "./ResponseJsonEditor" |
| 13 | +import { StatusSelect } from "./StatusSelect" |
| 14 | +import babel from "prettier/plugins/babel" |
| 15 | +import prettierPluginEstree from "prettier/plugins/estree" |
| 16 | +import { Separator } from "@/shared/ui/separator" |
| 17 | +import { Controller, useForm } from "react-hook-form" |
| 18 | +import { HttpMethods } from "msw" |
| 19 | +import { ROUTE_METHODS } from "@/constants" |
| 20 | + |
| 21 | +type CreateEditFormValues = { |
| 22 | + delay?: string |
| 23 | + description?: string |
| 24 | + method: HttpMethods |
| 25 | + url: string |
| 26 | + status: string |
| 27 | + response: string |
| 28 | +} |
| 29 | + |
| 30 | +export const CreateEditForm = () => { |
| 31 | + const { |
| 32 | + register, |
| 33 | + control, |
| 34 | + handleSubmit, |
| 35 | + setError, |
| 36 | + formState: { errors }, |
| 37 | + } = useForm<CreateEditFormValues>({ |
| 38 | + defaultValues: { |
| 39 | + method: HttpMethods.GET, |
| 40 | + url: "", |
| 41 | + status: "200", |
| 42 | + response: "{}", |
| 43 | + }, |
| 44 | + }) |
| 45 | + |
| 46 | + return ( |
| 47 | + <form |
| 48 | + onSubmit={handleSubmit((value) => { |
| 49 | + console.log(value) |
| 50 | + })} |
| 51 | + > |
| 52 | + <div className="flex py-[12px] px-[16px]"> |
| 53 | + <Controller |
| 54 | + control={control} |
| 55 | + name="method" |
| 56 | + render={({ field }) => ( |
| 57 | + <Select defaultValue={field.value} onValueChange={field.onChange}> |
| 58 | + <SelectTrigger className="w-[180px]"> |
| 59 | + <SelectValue placeholder="Theme" /> |
| 60 | + </SelectTrigger> |
| 61 | + <SelectContent> |
| 62 | + {ROUTE_METHODS.map((method) => ( |
| 63 | + <SelectItem key={method} value={method}> |
| 64 | + {method} |
| 65 | + </SelectItem> |
| 66 | + ))} |
| 67 | + </SelectContent> |
| 68 | + </Select> |
| 69 | + )} |
| 70 | + /> |
| 71 | + <Input |
| 72 | + placeholder="Route URL https://example.com/..." |
| 73 | + {...register("url")} |
| 74 | + /> |
| 75 | + <Button type="submit">Save</Button> |
| 76 | + </div> |
| 77 | + <Separator /> |
| 78 | + <div className="flex py-[12px] px-[16px]"> |
| 79 | + <StatusSelect defaultValue={"200"} /> |
| 80 | + <Input placeholder="Description" {...register("description")} /> |
| 81 | + <Input placeholder="delay" {...register("delay")} /> |
| 82 | + </div> |
| 83 | + <Separator /> |
| 84 | + <div className="flex justify-between w-full text-left py-[12px] px-[16px]"> |
| 85 | + <H4>Response Body</H4> |
| 86 | + {errors.response && ( |
| 87 | + <P className="text-red-500">{errors.response.message}</P> |
| 88 | + )} |
| 89 | + </div> |
| 90 | + <div className="py-[12px] px-[16px] text-left"> |
| 91 | + <Controller |
| 92 | + control={control} |
| 93 | + name="response" |
| 94 | + render={({ field }) => ( |
| 95 | + <ResponseJsonEditor |
| 96 | + value={field.value} |
| 97 | + onChange={(value) => { |
| 98 | + console.log(value) |
| 99 | + field.onChange(value) |
| 100 | + }} |
| 101 | + onSave={async () => { |
| 102 | + try { |
| 103 | + const formattedResponse = await format(field.value, { |
| 104 | + tabWidth: 2, |
| 105 | + printWidth: 100, |
| 106 | + parser: "json5", |
| 107 | + plugins: [babel, prettierPluginEstree], |
| 108 | + }) |
| 109 | + // LINK: https://github.com/prettier/prettier/issues/6360 |
| 110 | + field.onChange(formattedResponse.replace(/[\r\n]+$/, "")) |
| 111 | + } catch (error) { |
| 112 | + const formattedError = |
| 113 | + error instanceof Error ? error.message : "Unknown Error" |
| 114 | + setError("response", { |
| 115 | + message: formattedError, |
| 116 | + }) |
| 117 | + } |
| 118 | + }} |
| 119 | + /> |
| 120 | + )} |
| 121 | + /> |
| 122 | + </div> |
| 123 | + </form> |
| 124 | + ) |
| 125 | +} |
0 commit comments