0

I am working on a React JS with Inertia JS combined application. I'm in the progress to finish my edit feature. so I need to make some checkboxes to be checked default based on a set of values in an array.

I loop the checkbox to let the user choose the list of permissions and some of them should be already checked based on the data from the array.

I'm confused to fix the problem, sometimes I success to checked them but all of the checkbox checked also can't uncheck the checkbox.

this is my code

export default function EditPermission (props){
const {role} = usePage().props
const { data, setData, put, processing, errors, reset } = useForm({
    name: role.name || '',
    permissions: [] // store the permissions data on role

})

const mapPermissionsOnRole = () => {
    for(let i=0; i < role.permissions.length; i++){
       data.permissions.push(role.permissions[i]['name'])
    }
} //map the already exist permissions and push to data.permissions

const handleChecked = (e) => {
    let value = e.target.name;
    if (e.target.checked) {
        setData("permissions", [...data.permissions, value]);

    } else {
        setData(
            "permissionss",
            data.permissions.filter((item) => {
                return item !== value
            })
        );
    }
};

const submit = (e) => {
    e.preventDefault();
    put(route('role.update',permission.id));
};



return (
    <AuthenticatedLayout auth={props.auth} errors={props.errors}>
    <div className="w-full lg:max-w-6xl mt-6 px-6 py-4 bg-white shadow-md overflow-hidden sm:rounded-lg">        

        <form onSubmit={submit}>
            <div>
                    {props.permissions && props.permissions.length > 0 ? role.permissions.map((permissions, i) => {
                        return(
                            <div>
                                <Checkbox checked={props.permissions[i].name.includes(data.permissions)} name={permissions.name} value={data.permissions} handleChange={handleChecked} />{permissions.name}
                            </div>
                        )
                            
                            
                        }):<p>Permissions Coming Soon!</p>
                    }

                <PrimaryButton className="ml-4" processing={processing}>
                    Update
                </PrimaryButton>
            </div>

        </form>
    </div>

    </AuthenticatedLayout>
    
)

}

2
  • checked={props.permissions[i].name.includes(data.permissions)} || two things for above code: 1- is data.permissions an array?, if so then is props.permissions[i].name also an array? 2- we are using data from props to update the checked prop (shouldn't we use data from useForm here)? Commented Dec 9, 2022 at 10:11
  • ok , quest 1 , data.permissions is an array which contain only permissions on role specific role and props.permissions[i].name i use this for show all the permissions i have , question 2 i use data from props only for show all the permissions data @ShyamMittal Commented Dec 9, 2022 at 11:11

1 Answer 1

1

you need to call const mapPermissionsOnRole. try using the useEffect to call the data and push it to the array of data.permissions.

A quick tip is to use the id better than the name of the permission for comparison or use a new attribute on the database that the data is is_checked or not.

Sign up to request clarification or add additional context in comments.

4 Comments

useEffect(() => { let ignore = false; if (!ignore) mapPermissionsOnRole() return () => { ignore = true; } },[] ); do you mean this ?
so better to compare the id from the database?
I mean that mapPermissionsOnRole is set but not called on the function, what is the output that you get from this overall on the page?
you can use the id not the name as an index to compare if it there or not this is better than the name because the name is not a unique attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.