Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selectedByDefault not working with functional components #14

Open
naimgarzuzi opened this issue Oct 17, 2021 · 6 comments
Open

selectedByDefault not working with functional components #14

naimgarzuzi opened this issue Oct 17, 2021 · 6 comments

Comments

@naimgarzuzi
Copy link

naimgarzuzi commented Oct 17, 2021

Hi,

I'm facing issue with selectedByDefault when using functional components.
I'm fetching the data using useContext hook, and setting days prop with useEffect, The isSelected dosen't affected in picker.

The isReserved works fine.

@roggervalf
Copy link
Owner

hi @naimgarzuzi sorry for the delay, could you please share a piece of code to reproduce this

@naimgarzuzi
Copy link
Author

naimgarzuzi commented Jan 5, 2022

Hi @roggervalf ,
This is my code, I'm new in React so there might be some simple code.
But basicly the code is working fine, but the isSelected not worked for me.
Also the code file attached as TXT file

Thanks in advance.
`import { useEffect, useState, useContext } from 'react';
import { AppointmentPicker } from 'react-appointment-picker';

import { DatePickerCalendar } from 'react-nice-dates'
import 'react-nice-dates/build/style.css'
import './Appointments.css'
import { Container, Row, Col } from 'react-bootstrap'
import { getDay } from 'date-fns';
import AppointmentContext from '../Context/AppointmentContext';

function AppointmentsComp() {

const [lodaing, setLoading] = useState(false);
const [date, setDate] = useState(new Date(new Date().setHours(8, 0, 0, 0)));
const [days, setDays] = useState([[]]);

const modifiers = {
disabled: date => getDay(date) === 0 || getDay(date) === 6, // Disables Saturdays
}

const postContext = useContext(AppointmentContext)
const { error, appointments, getAppointments, removeAppointment, addAppointment } = postContext

useEffect(() => {
if (date != null) {
console.log("getting appointments")
setDays([
[
{
"id": 1,
"number": 1
},
{
"id": 2,
"number": 2,
"isReserved": true
},
{
"id": 3,
"number": 3,
"isSelected": true
},
{
"id": 4,
"number": 4
},
{
"id": 5,
"number": 5
},
{
"id": 6,
"number": 6
},
{
"id": 7,
"number": 7
},
{
"id": 8,
"number": 8
},
{
"id": 9,
"number": 9
},
{
"id": 10,
"number": 10
},
{
"id": 11,
"number": 11
},
{
"id": 12,
"number": 12
},
{
"id": 13,
"number": 13
}
]
])
}
}, [date])

// useEffect(() => {
// console.log(appointments)
// if (appointments.length > 0) {
// setDays(appointments)
// }
// }, [appointments])

async function addAppointmentCallbackContinuousCase({
addedAppointment: { day, number, time, id },
addCb,
removedAppointment: params,
removeCb
}) {
setLoading(true)
if (removeCb) {
await removeAppointment({ params });
console.log(
Removed appointment ${params.number}, day ${params.day}, time ${params.time}, id ${params.id}
);
removeCb(params.day, params.number);
}

await addAppointment({ id, number, day, time });
console.log(error)
if (!error) {
  console.log(
    `Added appointment ${number}, day ${day}, time ${time}, id ${id}`
  );
  addCb(day, number, time, id);
}
setLoading(false)

};

async function removeAppointmentCallbackContinuousCase(
{ day, number, time, id },
removeCb
) {
setLoading(true)
let params = { id, number, day, time }
await removeAppointment({ params });
console.log(
Removed appointment ${number}, day ${day}, time ${time}, id ${id}
);
removeCb(day, number);
setLoading(false)

};

return (










);
}

export default AppointmentsComp;
Appointments.txt
`

@roggervalf
Copy link
Owner

@naimgarzuzi, could you please set it in https://codesandbox.io/, would be easier to take a look

@naimgarzuzi
Copy link
Author

naimgarzuzi commented Jan 9, 2022

@naimgarzuzi, could you please set it in https://codesandbox.io/, would be easier to take a look

@roggervalf , you have edit permission
https://codesandbox.io/s/naughty-frog-sqw1y?file=/src/App.js

@roggervalf
Copy link
Owner

Nice, I was playing around and it works when dates is not set by setDays, if you just declare the dates variables and just pass it into the react-appointment-picker, it should work

@naimgarzuzi
Copy link
Author

I know that changing variable must be with use state, So I declared the setDays for changing the value of the days

Could you please change it in the sandbox to make it work?
I'll take a look after that to see how to do it without changing the state.

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants