Condition before inserting an item and show message in reactjs

Viewed 93

I'm developing an application in React.JS

I need to put a condition before inserting.

The code:

const App = () => {
    const [item, setItem] = useState([])
    const [category, setCategory] = useState([])

    const categories = category.map(elem => ({
        value: elem.id,
        label: elem.cat,
    }));

    const [category_select, setCategorySelect] = React.useState(null);

    function handleChangeCategory(value) {
        setCategorySelect(value);
    }

    useEffect(() => {
        getItems()
    }, [])

    useEffect(() => {
        getCategories()
    }, [])

    const getItems = async () => {
        const response = await axios.get(REQUEST_1)
        setItem(response.data)
    }

    const getCategories = async () => {
        const response = await axios.get(REQUEST_2)
        setCategory(response.data)
    }

    const addItems = () => {
        axios.post(`${REQUEST_1}`, {cat: category_select.value});
    };

    const body = () => {
        return item && item.map((elem,j) => {
            return (
                <tr key={elem.id}>
                    <td><span>{elem.cat}</span></td>
                </tr>
            )
        })
    }

    return (
        <>
            <div>
                <div>
                    <div>
                        <div>
                            <NoSsr>
                                <Select
                                    classes={classes}
                                    styles={selectStyles}
                                    inputId="category"
                                    TextFieldProps={{
                                        label: 'Category',
                                        InputLabelProps: {
                                        htmlFor: 'category',
                                        shrink: true,
                                        },
                                        placeholder: 'Category...',
                                    }}
                                    options={categories}
                                    components={components}
                                    value={category}
                                    onChange={handleChangeCategory}
                                />
                            </NoSsr>
                            <span>Select</span>
                        </div>
                        <div>
                            <label>&nbsp;</label>
                            <span onClick={addItems}></span>
                        </div>
                    </div>
                    <div>
                        <div>
                            <div>
                                <table>
                                    <thead>
                                        <tr>
                                            <th>
                                                <span>Category</span>
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {body()}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </>
    )
}

export default App;

The idea is to check if the item to be inserted already exists, and if it is already inserted, show a message through a popup.

How do I add this condition in the const addItems = () => {...} and the popup? Do I have to put that condition here?

How can I do it, suggestions?

1 Answers

You should check if the selected category already exists in addItems method.

You can use React packages like reactjs-popup to display a popup.

const [isShow, setIsShow] = useState(false)

const addItems = () => {
    // check if selected category already exists
    if (!category.some(elem => elem.cat === category_select.value)) { 
        axios.post(`${REQUEST_1}`, {cat: category_select.value});
    }
    else {
        // update popup modal open state and display popup
        setIsShow(true);
    }
};

...
return (
...
{ isShow && <h1>Error</h1> }
...)
Related