I'm currently working on incorporating an edit feature into the create form by utilizing server actions in the latest version of Next.js, version 14

The issue arises when the create form's type (id) parameter is null, which does not align with prisma's (edit info).


export function AboutForm({ id }: { id: number }) {
    const router = useRouter();
    const [err, setErr] = useState("");
    const [isPending, startTransition] = useTransition();
    const pending = useFormStatus();
    const formRef = useRef<HTMLFormElement>(null);
    const formRef2 = useRef<HTMLFormElement>(null);

    const formInfo = useForm<InputInfo>({
        resolver: zodResolver(infoSchema),
    });

    const formExperience = useForm<InputExperience>({
        resolver: zodResolver(experienceSchema),
    });

    const onsubmitInfo = async (data: InputInfo) => {
        startTransition(async () => {
            if (id) await editInfo(id) //try to edit the form here if id exists
            await createInfo(data);
            router.refresh();
        });
    };

    const onSubmitExperience = async (data: InputExperience) => {
        startTransition(async () => {
            const result = await createExperience(data);
            if (!result.success) setErr("Unexpected error. Unable to create");
            router.refresh();
        });
    };


    return (
        <section className="mx-auto px-10 md:max-w-md pt-10 md:pt-0 bg-re">

            <Form {...formInfo}>
                <form
                    onSubmit={formInfo.handleSubmit(onsubmitInfo)}
                    className="space-y-8"
                    ref={formRef}
                >
                    <FormField
                        control={formInfo.control}
                        name="resume"
                        render={({ field }) => (
                            <FormItem>
                                <FormControl>
                                    <Input placeholder="resume" {...field} />
                                </FormControl>
                                <FormMessage />
                            </FormItem>
                        )}
                    />
//problems arise here:
export const EditForm = async({ id }: { id: number }) => {
    let [isPending, startTransition] = useTransition();
    const info = await editInfo(id)

    return (
        <AboutForm id={info}/>   //id 
       
    );
};

Type '{ info: { id: number; resume: string; description: string; createAt: Date; } | null; success: boolean; }' is not assignable to type 'number'.ts(2322)

AboutForm.tsx(38, 37): The expected type comes from property 'id' which is declared here on type 'IntrinsicAttributes & { id: number; }'

I attempted utilizing dynamic import from an online lesson but it didn't work. My ultimate objective is to consolidate all components in one place. The purpose of editing is to trigger the modal to edit the form using the same form I used for creating. Reference image of the app: https://i.stack.imgur.com/PP2sH.png

Answer №1

When utilizing your AboutForm, it requires an id to be passed as a parameter. The info parameter you provide is an object obtained from the editInfo function. To ensure proper functionality, consider adjusting either the return value of the editInfo function or modifying the component call from <AboutForm id={info}/> to

<AboutForm id={info.info.id}/>
.

To prevent potential issues with null values in the info object, incorporate a check such as:

return (
{ info && info.info ?
        <AboutForm id={info} />
: <>Include a meaningful message here ;)</>
}
    );

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to resize a PDF to match the width and height of a canvas using

I need help with a project involving rendering previews of PDF documents in a UI similar to Google Docs. {documents.map((doc) => { return ( <div key={doc.id} className=" ...

Server hosted by Moralis

I am encountering an issue with setting up a Moralis self-hosted server. I have inherited a project that relies on a previous developer wrapping the entire application around the Moralis provider. However, the ability to create a Moralis server on their we ...

An issue arises with Autocomplete when attempting an ajax request and an error is

I'm struggling to implement jQuery Autocomplete on a text box, but it's not functioning properly. Below is my script for retrieving autocomplete list from the database. However, I encounter an error that displays an alert with an error message. ...

Tips for ensuring a controller function only runs once in AngularJS

I have a controller that is being referenced in some HTML. The HTML changes on certain events, causing the controller function code to execute multiple times. The issue lies in a portion of the code that should only be executed once. Shown below is the ...

Interactive menu that changes based on user input

I am attempting to create individual menus that display the names of each person in my app, but all the menus end up showing the same name. The buttons correctly display different user names, but the menu content does not change. Here is a simplified versi ...

Display a compilation either in the backend or the frontend

I'm fairly new to NodeJS and I could really use some advice. I currently have a webpage that displays a list of users, which is retrieved from a collection using Mongoose. I am aware of two different ways to display this list: 1) One option is to que ...

Is there a built-in event in Jquery UI tabs for when the tabs are collapsed?

Which event in jquery ui can I utilize to detect when a panel collapse has finished on the browser? I require this information to perform calculations based on the screen layout after the collapse has occurred. If I use the select or show event callbacks, ...

Attempting to achieve a carousel animation using jQuery

Upon loading the page, only one character out of four is displayed. Two arrows are provided - one on the left and one on the right. Clicking on the left arrow causes the current character to fade out and the previous character to fade in. Clicking on the r ...

Aligning rows of checkboxes in React Material UI

Any suggestions on how I can achieve the following: I am looking to have two checkboxes, one for Android and one for IOS placed next to each other in a row. I have tried using Material UI FormGroup, Formlabel, and Checkbox components within a React form. ...

Tips for launching a React Native application with a Node.js backend on an Android device?

Currently, I'm facing an issue while trying to run my React Native app on my local Android Device. The app utilizes Node.js APIs to retrieve data from a MySQL database. However, the problem arises when my Android device is unable to access the Node.js ...

The images are not displaying in the react-native-flatlist-slider component

App.js import { StyleSheet, Text, View } from 'react-native' import React from 'react' import { FlatListSlider } from 'react-native-flatlist-slider'; const images = [ { image: 'https://cdn-icons-png.flaticon. ...

The action creator is failing to dispatch any actions

After spending hours trying to solve this issue, I've decided to seek help here... The problem I'm encountering is that even though I can successfully call my action creator, I am unable to dispatch my action. This is how my Action Creator look ...

Next.js threw a wrench in my plans when the HTML syntax was completely disrupted upon saving the index.js

I have encountered an issue in my VSCode environment while working on a next.js project. Whenever I attempt to save the index.js file, the HTML syntax crashes. I am at a loss on how to resolve this issue, so any assistance would be greatly appreciated. Tha ...

Having difficulty animating the height transition of the NextJS Image component

On my webpage, I have a headerbar that changes size (from 120px to 70px) when the user scrolls down. I successfully implemented this transition using Tailwind classes and a height transition. Now, I am trying to make the site logo resize along with the hea ...

Retrieve a dynamic hex color variable for Tailwind CSS using Next.js

I need to dynamically change the colors based on a hex code retrieved from a database. In my variables.js file: const primaryColor = "#000000"; const secondaryColor = "#ff0000"; const notCheckedColor = "#9CA3AF"; export { primaryColor, secondaryColor, no ...

Tips for optimizing the placement of div elements for top advertisements on Amazon's website

I'm looking to overlay some divs on top of Amazon.com ads, similar to the image above. This is part of a personal project where I need to position these divs precisely over the ads. To achieve this effect, I've been using getBoundingClientRect t ...

When making an Ajax post request, the loading indicator may not appear

I've implemented the jQuery code below for an autocomplete input field, but I'd like to have a spinner display while waiting for the response from the server. In my code, I'm using search: to show the loading div and open: to hide it. The s ...

Bootstrap4 does not support the <button> element

How can I achieve a 'collapse icon' floated to the left followed by Copyright © using Bootstrap 4? I've looked at similar questions on this topic, but none seem to provide a direct answer. Other questions contain different code with ob ...

Tips for adding a gradient to your design instead of a plain solid color

I stumbled upon a snippet on CSS Tricks Attempting to replace the green color with a gradient value, but unfortunately, the value is not being applied. I have tried using both the fill property and gradient color, but neither has been successful. Here is ...

Retrieving User Keypad Input with Twilio Phone Calling in a Node.js Application (excluding web server)

const userInput = message.content.split(' ') const phoneNumber = userInput[1] const messageToSay = userInput.slice(2).join(' ') if (phoneNumber) { // Dial phoneNumber and deliver messageToSay, then gather ke ...