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

Context update after actions #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/component/Calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import '../styles/calender.css'

function MyCalendar() {
const localizer = dayjsLocalizer(dayjs);
const { teams, members } = useContext(TeamDataContext);
const { teams, members, refreshTeamData } = useContext(TeamDataContext);
const [events, setEvents] = useState([]);
const [selectedEvent, setSelectedEvent] = useState(null);

Expand Down Expand Up @@ -43,6 +43,7 @@ function MyCalendar() {
});

setEvents(formattedEvents);
refreshTeamData()
} catch (error) {
console.error("Error fetching events:", error);
}
Expand Down
20 changes: 16 additions & 4 deletions src/component/Context.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ const TeamDataProvider = ({ children }) => {
const [teams, setTeams] = useState([]);
const [members, setMembers] = useState([]);
const [timeOff, setTimeOff] = useState([]);
const [dataIsOld, setDataIsOld] = useState(false);

useEffect(() => {
const fetchData = async () => {
const refreshTeamData = () => {
setDataIsOld(true)
}

const fetchData = async () => {
try {
const [teamsResponse, membersResponse, timeOffResponse] = await Promise.all([
fetch(apiPath('/teams')),
Expand All @@ -28,12 +32,20 @@ const TeamDataProvider = ({ children }) => {
console.error('Error fetching data:', error);
}
};


useEffect(() => {
fetchData();
}, []);

useEffect(() => {
if(dataIsOld) {
fetchData();
setDataIsOld(false)
}
}, [dataIsOld]);

return (
<TeamDataContext.Provider value={{ teams, members, timeOff }}>
<TeamDataContext.Provider value={{ teams, members, timeOff, setMembers, refreshTeamData }}>
{children}
</TeamDataContext.Provider>
);
Expand Down
30 changes: 18 additions & 12 deletions src/component/CreateMember.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { useState, useEffect, useContext } from "react";
import { useParams } from "react-router-dom";
import { apiPath } from '../api';
import { TeamDataContext } from '../component/Context';
import '../styles/createMember.css'
import { apiPath } from "../api";
import { TeamDataContext } from "../component/Context";
import "../styles/createMember.css";

function CreateMember() {
const { code } = useParams();
const contextData = useContext(TeamDataContext);
const teams = contextData.teams;
const { code } = useParams();
const { teams, refreshTeamData } = useContext(TeamDataContext);

const [teamId, setTeamId] = useState(null);
const [memberData, setMemberData] = useState({
first_name: "",
last_name: "",
email: "",
color: "#000000",
allowed_dayoff: 0,
allowed_dayoff: 0,
});

useEffect(() => {
const team = teams.find(team => team.team_code === code);
const team = teams.find((team) => team.team_code === code);
if (team) {
setTeamId(team.id);
} else {
Expand All @@ -31,21 +30,28 @@ function CreateMember() {
e.preventDefault();

try {
const response = await fetch(apiPath('/members'), {
const response = await fetch(apiPath("/members"), {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...memberData,
team_id: teamId,
assigned_dayoff: 0,
assigned_dayoff: 0,
}),
});

if (response.ok) {
alert("Member created successfully!");
setMemberData({ first_name: "", last_name: "", email: "", color: "#000000", allowed_dayoff: 0 });
refreshTeamData();
setMemberData({
first_name: "",
last_name: "",
email: "",
color: "#000000",
allowed_dayoff: 0,
});
} else {
const data = await response.json();
alert(data.error || "Failed to create member. Please try again.");
Expand All @@ -58,7 +64,7 @@ function CreateMember() {

const handleChange = (e) => {
const { name, value } = e.target;
setMemberData(prevState => ({
setMemberData((prevState) => ({
...prevState,
[name]: value,
}));
Expand Down
5 changes: 3 additions & 2 deletions src/component/DeleteMember.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useParams } from 'react-router-dom';
import '../styles/deleteMember.css';

const DeleteMember = () => {
const { members, teams } = useContext(TeamDataContext);
const { members, teams, setMembers } = useContext(TeamDataContext);
const [selectedMember, setSelectedMember] = useState('');
const [filteredMembers, setFilteredMembers] = useState([]);
const { code } = useParams();
Expand Down Expand Up @@ -48,9 +48,10 @@ const DeleteMember = () => {
});

if (response.ok) {
const updatedMembers = filteredMembers.filter(member => member.id !== selectedMember);
const updatedMembers = filteredMembers.filter(member => member.id !== Number(selectedMember));
setFilteredMembers(updatedMembers);
alert('Member deleted successfully');
setMembers(updatedMembers)
} else {
alert('Failed to delete member. Please try again.');
}
Expand Down