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

Baseline Calender #480

Merged
merged 19 commits into from
Oct 26, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
done with appointments page
vaibhavgeek committed Oct 10, 2022
commit 21b8d90e79d3ce435998c119f534448364fb97be
Binary file modified examples/calendar/backend/db/temp.sqlite
Binary file not shown.
10 changes: 5 additions & 5 deletions examples/calendar/backend/services/time/controller.ts
Original file line number Diff line number Diff line change
@@ -34,17 +34,17 @@ export const create = async (req: Request, res: Response, next: NextFunction) =>

// Get all available times for a person
export const get = async (req: Request, res: Response, next: NextFunction) => {
if ((req as any).user.payload.id !== +req.params.userId) {
return res.status(401).send({ error: "You can can only access yourself" });
}

try {
const userId = (req as any).user.payload.id;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if userId is undefined?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added check to ensure it is not.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please check that the code was pushed?

const times = await Time.findAll({
where: {
userId: {
[Op.eq]: req.params.userId
user: {
[Op.eq]: userId
}
}
});
console.log("Times", times);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please either delete this, or use logger

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted it.

return res.status(200).send(times);
} catch (error) {
console.log(error);
106 changes: 91 additions & 15 deletions examples/calendar/frontend/src/Profile/Profile.tsx
Original file line number Diff line number Diff line change
@@ -15,8 +15,15 @@ interface Props {
}

interface Time {
startTime: number;
startTime: Date;
checked: boolean;
disabled: boolean;
}
interface SetBooking {
timeStarts: number[];
timeEnds: number[];
status: string;
userId: number;
}
interface State {
loading: boolean;
@@ -36,30 +43,81 @@ interface JwtDecoded {
}

export const Profile = ({ auth, onLoggedOut }: Props): JSX.Element => {

const [state, setState] = useState<State>({
const [selectedDate, setSelectedDate] = useState<Date>(new Date());

const getTimeAvailAPI = async (userId: string) => {
const fetchAvailTimes = (await axios.get(`${process.env.REACT_APP_BACKEND_URL}/time/${userId}`, {
headers: {
Authorization: `Bearer ${accessToken}`
}
})).data;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible error if axios call fails

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code.

console.log("fetch", fetchAvailTimes);
return fetchAvailTimes;
}
const setTimeAvailAPI = async (availableTimes:SetBooking ) => {
const setAvailTimes = (await axios.post(`${process.env.REACT_APP_BACKEND_URL}/time`, {
headers: {
Authorization: `Bearer ${accessToken}`
}, json: availableTimes
})).data;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible error if axios call fails

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code.

return setAvailTimes;
}
const timesInitial = () => {
const timeReturn = [];
var someDate = new Date(selectedDate);
someDate.setHours(8, 0, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a JSDoc as to what is happening here and at line 78

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gets the Time availability API. I feel the code is self explanatory here.

for (var i = 0; i < 60; i++)
{
const newTime = someDate.getTime() + i * 15 * 60 * 1000;
timeReturn.push({ startTime: new Date(newTime), checked: false, disabled: false });
}
return timeReturn;
}
const setTimesFalse = () => {
const timeReturn = [];
var someDate = new Date(selectedDate);
someDate.setHours(8, 0, 0);
for (var i = 0; i < 60; i++)
{
const newTime = someDate.getTime() + i * 15 * 60 * 1000;
timeReturn.push({ startTime: new Date(newTime), checked: false, disabled: false });
}
return timeReturn;
}

const [times, setTimes] = useState<Time[]>(timesInitial())
const [userV, setUser] = useState<State>({
loading: false,
user: undefined,
username: "",
times: [{startTime: 1888, checked: false}],
username: ""
});
const [checkedTimes, setCheckedTimes] = useState<String[]>([]);
useEffect(() => {
setTimes(timesInitial());
}, [selectedDate]);

useEffect(() => {
const { accessToken } = auth;
const {
payload: { id }
} = jwtDecode<JwtDecoded>(accessToken);

async function fetchUserAPI() {
const user = (await axios.get<User>(`${process.env.REACT_APP_BACKEND_URL}/users/user`, {
headers: {
Authorization: `Bearer ${accessToken}`
}
})).data;
setState({...state, user});
setUser({ ...userV, user });
const booked = await getTimeAvailAPI(id);
booked.forEach((booking: any) => {
let item = { startTime: booking.startTime, checked: false, disabled: false };
const tempTimes = times;
var foundIndex = tempTimes.findIndex(x => x === item);
tempTimes[foundIndex] = { startTime: booking.startTime, checked: false, disabled: true };
setTimes(tempTimes);
});
};
fetchUserAPI();

}, []);

const { accessToken } = auth;
@@ -68,26 +126,44 @@ export const Profile = ({ auth, onLoggedOut }: Props): JSX.Element => {
payload: { publicAddress }
} = jwtDecode<JwtDecoded>(accessToken);

const { loading, user, times } = state;
const { loading, user } = userV;

const username = user && user.username;

return (
<div className="Profile">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quite same content is in another file too. Can this be extracted out, and used in different files

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just two files, would have made sense if there were more than 2 instances.

<p>
Logged in as {publicAddress}
Logged in as {publicAddress}

</p>

<h1>Set your availability</h1>

<div style={{display: "grid", gridTemplateColumns: "3fr 9fr"}}>
<Calendar />
<Calendar value={selectedDate} onChange={setSelectedDate} />

<div style={{ "textAlign": "left" }}>
{times?.map((time: Time) => {
<>
{times?.map((time: Time, index:number) => {
return (
<ToggleButton className="mb-2" id="toggle-check" type="checkbox" variant="outline-primary" value="1">
{time.startTime}
<ToggleButton key={index} className="mb-2" disabled={time.disabled} id={`toggle-check-${index}`} type="checkbox"
variant="outline-primary" checked={checkedTimes[index] === time.startTime.getTime().toString()} value={index} onChange={
(e) => {
console.log("Checked times", checkedTimes[index]);
const tempTimes = [...checkedTimes];
//alert(e.target.value);
//const findIndex = tempTimes.findIndex(x => x.startTime.getTime().toString() == e.target.value);
//alert(findIndex);
tempTimes[index] = time.startTime.getTime().toString();
console.log("temptimes", tempTimes);
//alert(e.target.checked);
//e.target.checked = false;
setCheckedTimes(tempTimes);
}
}>
{time.startTime.getHours()}:{time.startTime.getMinutes()}
</ToggleButton>)
})}
</>
</div>
</div>
<button onClick={onLoggedOut}>Logout</button>