|
1 |
| -import React from "react"; |
| 1 | +import React, { useEffect, useState } from "react"; |
2 | 2 | import MarkdownReader from "./md-components";
|
| 3 | +import { CheckIcon, Cross1Icon, Pencil1Icon } from "@radix-ui/react-icons"; |
| 4 | +import { Button } from "./ui/button"; |
| 5 | +import { Input } from "./ui/input"; |
3 | 6 |
|
4 | 7 | const ListSuggestion = ({
|
5 | 8 | suggestions,
|
6 | 9 | commitChanges,
|
| 10 | + submitForm, |
7 | 11 | }: {
|
8 | 12 | suggestions: string;
|
9 | 13 | commitChanges: string;
|
| 14 | + submitForm: ( |
| 15 | + e: React.FormEvent<HTMLFormElement>, |
| 16 | + message: string | null |
| 17 | + ) => void; |
10 | 18 | }) => {
|
| 19 | + const [isEditMode, setIsEditMode] = useState<boolean>(false); |
| 20 | + const [updatedMessage, setUpdatedMessage] = useState(commitChanges); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + setIsEditMode(false); |
| 24 | + }, [commitChanges, suggestions]); |
| 25 | + |
| 26 | + const toggelEditMode = () => { |
| 27 | + setIsEditMode((pre) => !pre); |
| 28 | + }; |
| 29 | + |
11 | 30 | return (
|
12 |
| - <div className="mx-4 md:mx-10"> |
13 |
| - <h1 className="text-xl font-bold">{commitChanges}</h1> |
| 31 | + <div className="mx-4 md:mx-10 pt-1"> |
| 32 | + {isEditMode ? ( |
| 33 | + <form |
| 34 | + onSubmit={(e) => submitForm(e, updatedMessage)} |
| 35 | + className="flex gap-2 w-full items-center justify-between " |
| 36 | + > |
| 37 | + <Input |
| 38 | + className="w-full" |
| 39 | + value={updatedMessage} |
| 40 | + autoComplete="off" |
| 41 | + onChange={(e) => { |
| 42 | + setUpdatedMessage(e.target.value); |
| 43 | + }} |
| 44 | + autoFocus |
| 45 | + required |
| 46 | + ></Input> |
| 47 | + <Button |
| 48 | + variant={"outline"} |
| 49 | + size={"icon"} |
| 50 | + type="submit" |
| 51 | + className={ |
| 52 | + "bg-primary-foreground text-muted-foreground border hover:bg-primary-foreground" |
| 53 | + } |
| 54 | + disabled={ |
| 55 | + updatedMessage === commitChanges || updatedMessage.trim() === "" |
| 56 | + } |
| 57 | + > |
| 58 | + <CheckIcon /> |
| 59 | + </Button> |
| 60 | + <Button |
| 61 | + variant={"outline"} |
| 62 | + size={"icon"} |
| 63 | + type="button" |
| 64 | + className={ |
| 65 | + "bg-primary-foreground text-muted-foreground border hover:bg-primary-foreground" |
| 66 | + } |
| 67 | + onClick={toggelEditMode} |
| 68 | + > |
| 69 | + <Cross1Icon /> |
| 70 | + </Button> |
| 71 | + </form> |
| 72 | + ) : ( |
| 73 | + <div className="flex w-full items-center justify-between "> |
| 74 | + <h1 className="text-xl font-bold">{commitChanges}</h1> |
| 75 | + <Button |
| 76 | + variant={"outline"} |
| 77 | + size={"icon"} |
| 78 | + className={ |
| 79 | + "bg-primary-foreground text-muted-foreground border hover:bg-primary-foreground" |
| 80 | + } |
| 81 | + onClick={toggelEditMode} |
| 82 | + > |
| 83 | + <Pencil1Icon /> |
| 84 | + </Button> |
| 85 | + </div> |
| 86 | + )} |
14 | 87 | <MarkdownReader markdown={suggestions} />
|
15 | 88 | </div>
|
16 | 89 | );
|
|
0 commit comments