Archived
fetch calls and websocket
This commit is contained in:
@@ -3,7 +3,8 @@ import UserLite from "./UserLite";
|
||||
interface UserFull {
|
||||
userInfo: UserLite;
|
||||
manager: UserLite;
|
||||
directReports: UserLite[];
|
||||
managerDirectReports: UserLite[];
|
||||
userDirectReports: UserLite[];
|
||||
}
|
||||
|
||||
export default UserFull;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
interface UserStatus {
|
||||
uuid: string;
|
||||
userId: string;
|
||||
inMeeting: boolean;
|
||||
meetingID: string | null;
|
||||
meetingId: string | null;
|
||||
}
|
||||
|
||||
export default UserStatus;
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
} from "../../../../redux/slices/favoritesSlice";
|
||||
import RemoveIcon from "@mui/icons-material/Remove";
|
||||
import { MeetingStatus } from "../../../../utils";
|
||||
import { selectMe } from "../../../../redux/slices/usersSlice";
|
||||
|
||||
interface Props {
|
||||
contactInfo: UserLite;
|
||||
@@ -28,9 +29,10 @@ const UpperBody: React.FC<Props> = (props) => {
|
||||
const userStatus: UserStatus = useAppSelector((state) =>
|
||||
selectUserStatus(state, props.contactInfo.uuid)
|
||||
);
|
||||
const me = useAppSelector(selectMe);
|
||||
const favoritesUuids = useAppSelector(selectFavorites);
|
||||
const detailedMeeting = useAppSelector((state) =>
|
||||
selectMeeting(state, userStatus.meetingID)
|
||||
selectMeeting(state, userStatus.meetingId)
|
||||
);
|
||||
const meetingStatus: MeetingStatus =
|
||||
userStatus && userStatus.inMeeting
|
||||
@@ -63,7 +65,11 @@ const UpperBody: React.FC<Props> = (props) => {
|
||||
variant="outlined"
|
||||
color="success"
|
||||
startIcon={<AddIcon />}
|
||||
onClick={() => dispatch(addFavorite(props.contactInfo.uuid))}
|
||||
onClick={() =>
|
||||
dispatch(
|
||||
addFavorite({ userId: me, toBeAdded: props.contactInfo.uuid })
|
||||
)
|
||||
}
|
||||
>
|
||||
favorites
|
||||
</Button>
|
||||
@@ -72,7 +78,14 @@ const UpperBody: React.FC<Props> = (props) => {
|
||||
variant="outlined"
|
||||
color="secondary"
|
||||
startIcon={<RemoveIcon />}
|
||||
onClick={() => dispatch(removeFavorite(props.contactInfo.uuid))}
|
||||
onClick={() =>
|
||||
dispatch(
|
||||
removeFavorite({
|
||||
userId: me,
|
||||
toBeRemoved: props.contactInfo.uuid,
|
||||
})
|
||||
)
|
||||
}
|
||||
>
|
||||
favorites
|
||||
</Button>
|
||||
|
||||
@@ -25,9 +25,9 @@ const Home: React.FC = () => {
|
||||
return (
|
||||
<Container className="main-home">
|
||||
<Grid container>
|
||||
<Grid item sm={8}>
|
||||
{/* <Grid item sm={8}>
|
||||
<MeetingsPanel />
|
||||
</Grid>
|
||||
</Grid> */}
|
||||
<Grid item sm={4}>
|
||||
<ShortCuts users={mockUsers} />
|
||||
</Grid>
|
||||
|
||||
@@ -12,7 +12,8 @@ import { store } from "../../redux/store";
|
||||
import { fetchFavorites } from "../../redux/slices/favoritesSlice";
|
||||
import { fetchMeetings } from "../../redux/slices/meetingsAndUserStatusSlice";
|
||||
import { fetchUsers } from "../../redux/slices/usersSlice";
|
||||
import axios from "../../api/axios"
|
||||
import axios from "../../api/axios";
|
||||
import { socketActions } from "../../redux/slices/meetingsAndUserStatusSlice";
|
||||
|
||||
interface LocationState {
|
||||
from: { pathname: string };
|
||||
@@ -40,14 +41,14 @@ const Login: React.FC = () => {
|
||||
// setErrMsg('');
|
||||
// }, [user, pwd])
|
||||
|
||||
const handleLogin = async(e: React.SyntheticEvent) => {
|
||||
const handleLogin = async (e: React.SyntheticEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
try {
|
||||
if (email === "" && password === "") {
|
||||
setAuth["email"] = email;
|
||||
setAuth["isLoggedIn"] = true;
|
||||
|
||||
|
||||
store.dispatch(fetchMeetings(""));
|
||||
store.dispatch(fetchUsers(""));
|
||||
store.dispatch(fetchFavorites(""));
|
||||
@@ -55,12 +56,12 @@ const Login: React.FC = () => {
|
||||
}
|
||||
|
||||
const response = await axios.post(
|
||||
'/login',
|
||||
"/login",
|
||||
JSON.stringify({ email: email, password: password })
|
||||
// { headers: { 'Content-Type': 'application/json' } }
|
||||
)
|
||||
);
|
||||
// const response = { data: { userid: "123456" }}; // delete later
|
||||
|
||||
|
||||
const logedInUserId = response?.data?.userId;
|
||||
|
||||
console.log(email);
|
||||
@@ -68,18 +69,16 @@ const Login: React.FC = () => {
|
||||
console.log(response?.data);
|
||||
|
||||
if (logedInUserId != undefined) {
|
||||
setAuth["email"] = email;
|
||||
setAuth["isLoggedIn"] = true;
|
||||
|
||||
setAuth.email = email;
|
||||
setAuth.isLoggedIn = true;
|
||||
|
||||
store.dispatch(socketActions.startConnecting());
|
||||
store.dispatch(fetchMeetings(logedInUserId));
|
||||
store.dispatch(fetchUsers(logedInUserId));
|
||||
store.dispatch(fetchFavorites(logedInUserId));
|
||||
navigate(from, { replace: true });
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
} catch (error) {}
|
||||
setEmail(email);
|
||||
setPassword("");
|
||||
};
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
import { open as openMeetingDetails } from "../../redux/slices/meetingDetailsOpenSlice";
|
||||
import { selectMeeting } from "../../redux/slices/meetingsAndUserStatusSlice";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { selectMe } from "../../redux/slices/usersSlice";
|
||||
|
||||
interface Props {
|
||||
user: UserLite;
|
||||
@@ -21,9 +22,10 @@ interface Props {
|
||||
const SettingsButton: React.FC<Props> = ({ user, status }: Props) => {
|
||||
const navigate = useNavigate();
|
||||
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
|
||||
const me = useAppSelector(selectMe);
|
||||
const favoritesUuids = useAppSelector(selectFavorites);
|
||||
const meeting = useAppSelector((state) =>
|
||||
selectMeeting(state, status.meetingID)
|
||||
selectMeeting(state, status.meetingId)
|
||||
);
|
||||
const dispatch = useAppDispatch();
|
||||
const open = Boolean(anchorEl);
|
||||
@@ -56,7 +58,7 @@ const SettingsButton: React.FC<Props> = ({ user, status }: Props) => {
|
||||
<MenuItem
|
||||
onClick={() => {
|
||||
handleClose();
|
||||
dispatch(removeFavorite(user.uuid));
|
||||
dispatch(removeFavorite({ userId: me, toBeRemoved: user.uuid }));
|
||||
}}
|
||||
>
|
||||
Remove from Favorites
|
||||
@@ -65,7 +67,7 @@ const SettingsButton: React.FC<Props> = ({ user, status }: Props) => {
|
||||
<MenuItem
|
||||
onClick={() => {
|
||||
handleClose();
|
||||
dispatch(addFavorite(user.uuid));
|
||||
dispatch(addFavorite({ userId: me, toBeAdded: user.uuid }));
|
||||
}}
|
||||
>
|
||||
Add to Favorites
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Middleware } from "redux";
|
||||
import { io, Socket } from "socket.io-client";
|
||||
import DetailedMeeting from "../../api-bodies/DetailedMeeting";
|
||||
import { socketActions } from "../slices/meetingsAndUserStatusSlice";
|
||||
|
||||
const socketMiddleware: Middleware = (store) => {
|
||||
let socket: Socket;
|
||||
|
||||
return (next) => (action) => {
|
||||
const isConnectionEstablished =
|
||||
socket && store.getState().meetingsAndUserStatuses.isConnected;
|
||||
|
||||
if (socketActions.startConnecting.match(action)) {
|
||||
console.log("startConnecting called");
|
||||
socket = io("wss://uo5wdcbn6l.execute-api.us-west-2.amazonaws.com/Prod/");
|
||||
|
||||
socket.on("Connect", () => {
|
||||
console.log("connected!!!");
|
||||
store.dispatch(socketActions.connectionEstablished());
|
||||
});
|
||||
|
||||
socket.on("MeetingCreated", (meeting: DetailedMeeting) => {
|
||||
store.dispatch(socketActions.meetingCreated(meeting));
|
||||
});
|
||||
|
||||
socket.on(
|
||||
"UserStatusChange",
|
||||
(statusChange: {
|
||||
userId: string;
|
||||
inMeeting: boolean;
|
||||
meetingId: string;
|
||||
}) => {
|
||||
store.dispatch(socketActions.userStatusChanged(statusChange));
|
||||
}
|
||||
);
|
||||
}
|
||||
next(action);
|
||||
};
|
||||
};
|
||||
|
||||
export default socketMiddleware;
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
|
||||
import { RootState } from "../store";
|
||||
import { favorites } from "../../api-bodies/MockData";
|
||||
import axios from "../../api/axios";
|
||||
|
||||
interface FavoritesState {
|
||||
favorites: string[];
|
||||
@@ -32,24 +32,25 @@ export const favoritesSlice = createSlice({
|
||||
export const fetchFavorites = createAsyncThunk(
|
||||
"favorites/fetchFavorites",
|
||||
async (uuid: string) => {
|
||||
// const response = await client.post("/fakeApi/posts", initialPost);
|
||||
// !!!
|
||||
console.log(uuid);
|
||||
return favorites;
|
||||
const response = await axios.get(`/users/${uuid}/favourites`);
|
||||
return response.data.userIds;
|
||||
}
|
||||
);
|
||||
export const addFavorite = createAsyncThunk(
|
||||
"favorites/addFavorite",
|
||||
async (uuid: string) => {
|
||||
// const response = await client.post("/fakeApi/posts", initialPost);
|
||||
return uuid;
|
||||
async (ids: { userId: string; toBeAdded: string }) => {
|
||||
await axios.post(
|
||||
`/users/${ids.userId}/favourites`,
|
||||
JSON.stringify({ userId: ids.toBeAdded })
|
||||
);
|
||||
return ids.toBeAdded;
|
||||
}
|
||||
);
|
||||
export const removeFavorite = createAsyncThunk(
|
||||
"favorites/removeFavorite",
|
||||
async (uuid: string) => {
|
||||
// const response = await client.post("/fakeApi/posts", initialPost);
|
||||
return uuid;
|
||||
async (ids: { userId: string; toBeRemoved: string }) => {
|
||||
await axios.delete(`/users/${ids.userId}/favourites/${ids.toBeRemoved}`);
|
||||
return ids.toBeRemoved;
|
||||
}
|
||||
);
|
||||
export const selectFavorites = (state: RootState) => state.favorites.favorites;
|
||||
|
||||
@@ -1,23 +1,41 @@
|
||||
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
|
||||
import DetailedMeeting from "../../api-bodies/DetailedMeeting";
|
||||
import { RootState } from "../store";
|
||||
import { meetings } from "../../api-bodies/MockData";
|
||||
import UserStatus from "../../api-bodies/UserStatus";
|
||||
import axios from "../../api/axios";
|
||||
|
||||
interface MeetingsAndUserStatusState {
|
||||
meetings: DetailedMeeting[];
|
||||
userStatuses: Record<string, UserStatus>;
|
||||
isConnecting: boolean;
|
||||
isConnected: boolean;
|
||||
}
|
||||
|
||||
const initialState: MeetingsAndUserStatusState = {
|
||||
meetings: [],
|
||||
userStatuses: {},
|
||||
isConnecting: false,
|
||||
isConnected: false,
|
||||
};
|
||||
|
||||
export const meetingsAndUserStatusSlice = createSlice({
|
||||
name: "meetingsAndUserStatus",
|
||||
initialState,
|
||||
reducers: {},
|
||||
reducers: {
|
||||
startConnecting: (state) => {
|
||||
state.isConnecting = true;
|
||||
},
|
||||
connectionEstablished: (state) => {
|
||||
state.isConnecting = true;
|
||||
state.isConnected = true;
|
||||
},
|
||||
meetingCreated: (state, action) => {
|
||||
state.meetings.push(action.payload.meeting);
|
||||
},
|
||||
userStatusChanged: (state, action) => {
|
||||
state.userStatuses[action.payload.uuid] = action.payload;
|
||||
},
|
||||
},
|
||||
extraReducers(builder) {
|
||||
builder.addCase(fetchMeetings.fulfilled, (state, action) => {
|
||||
state.meetings = action.payload.meetings;
|
||||
@@ -29,16 +47,17 @@ export const meetingsAndUserStatusSlice = createSlice({
|
||||
export const fetchMeetings = createAsyncThunk(
|
||||
"meetingsAndUserStatus/fetchMeetings",
|
||||
async (uuid: string) => {
|
||||
// const response = await client.post("/fakeApi/posts", initialPost);
|
||||
// !!!
|
||||
console.log(uuid);
|
||||
const response = await axios.get(
|
||||
`/users/${uuid}/meetings?start=1900-01-01&end=2100-01-01`
|
||||
);
|
||||
const meetings: DetailedMeeting[] = response.data.meetings;
|
||||
const userStatuses: Record<string, UserStatus> = {};
|
||||
meetings.forEach((meeting) => {
|
||||
meeting.liveParticipantIds.forEach((uuid) => {
|
||||
userStatuses[uuid] = {
|
||||
uuid: uuid,
|
||||
userId: uuid,
|
||||
inMeeting: true,
|
||||
meetingID: meeting.meetingId,
|
||||
meetingId: meeting.meetingId,
|
||||
};
|
||||
});
|
||||
});
|
||||
@@ -51,8 +70,8 @@ export const selectMeetings = (state: RootState) =>
|
||||
export const selectMeeting = (state: RootState, meetingID: string | null) => {
|
||||
return meetingID !== null
|
||||
? state.meetingsAndUserStatuses.meetings.find(
|
||||
(meeting) => meeting.meetingId === meetingID
|
||||
)
|
||||
(meeting) => meeting.meetingId === meetingID
|
||||
)
|
||||
: null;
|
||||
};
|
||||
export const selectUserUpcomingMeetings = (state: RootState, uuid: string) => {
|
||||
@@ -69,9 +88,9 @@ export const selectUserStatus = (
|
||||
return userStatus;
|
||||
} else {
|
||||
return {
|
||||
uuid: uuid,
|
||||
userId: uuid,
|
||||
inMeeting: false,
|
||||
meetingID: null,
|
||||
meetingId: null,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -86,12 +105,13 @@ export const selectUserStatuses = (
|
||||
userStatuses.push(userStatus);
|
||||
} else {
|
||||
userStatuses.push({
|
||||
uuid: uuid,
|
||||
userId: uuid,
|
||||
inMeeting: false,
|
||||
meetingID: null,
|
||||
meetingId: null,
|
||||
});
|
||||
}
|
||||
});
|
||||
return userStatuses;
|
||||
};
|
||||
export default meetingsAndUserStatusSlice.reducer;
|
||||
export const socketActions = meetingsAndUserStatusSlice.actions;
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
|
||||
import { RootState } from "../store";
|
||||
import { userLites, team } from "../../api-bodies/MockData";
|
||||
import UserLite from "../../api-bodies/UserLite";
|
||||
import axios from "../../api/axios";
|
||||
import UserFull from "../../api-bodies/UserFull";
|
||||
|
||||
interface TeamState {
|
||||
user: string | null;
|
||||
manager: string | null;
|
||||
user: string;
|
||||
manager: string;
|
||||
sameManager: string[];
|
||||
directReports: string[];
|
||||
}
|
||||
@@ -17,7 +18,7 @@ interface UsersState {
|
||||
|
||||
const initialState: UsersState = {
|
||||
users: {},
|
||||
team: { user: null, manager: null, sameManager: [], directReports: [] },
|
||||
team: { user: "", manager: "", sameManager: [], directReports: [] },
|
||||
};
|
||||
|
||||
export const usersSlice = createSlice({
|
||||
@@ -35,19 +36,37 @@ export const usersSlice = createSlice({
|
||||
export const fetchUsers = createAsyncThunk(
|
||||
"users/fetchUsers",
|
||||
async (uuid: string) => {
|
||||
// fetch userfull
|
||||
// fetch managerfull
|
||||
// now you have yourself, co-workers, manager, and direct reports (your team basically!)
|
||||
// call setTeam reducer in teamSlice
|
||||
console.log(uuid);
|
||||
const users: Record<string, UserLite> = {};
|
||||
userLites.forEach((userLite) => {
|
||||
const team: TeamState = {
|
||||
user: "",
|
||||
manager: "",
|
||||
sameManager: [],
|
||||
directReports: [],
|
||||
};
|
||||
// fetch userfull
|
||||
const userResp = await axios.get(`/users/${uuid}`);
|
||||
const user: UserFull = userResp.data.user;
|
||||
|
||||
users[user.userInfo.uuid] = user.userInfo;
|
||||
users[user.manager.uuid] = user.manager;
|
||||
|
||||
team.user = user.userInfo.uuid;
|
||||
team.manager = user.manager.uuid;
|
||||
|
||||
user.userDirectReports.forEach((userLite) => {
|
||||
users[userLite.uuid] = userLite;
|
||||
team.directReports.push(userLite.uuid);
|
||||
});
|
||||
user.managerDirectReports.forEach((userLite) => {
|
||||
users[userLite.uuid] = userLite;
|
||||
team.sameManager.push(userLite.uuid);
|
||||
});
|
||||
console.log(users);
|
||||
return { users: users, team: team };
|
||||
}
|
||||
);
|
||||
|
||||
export const selectMe = (state: RootState) => state.users.team.user;
|
||||
export const selectUser = (
|
||||
state: RootState,
|
||||
uuid: string | undefined
|
||||
@@ -56,7 +75,9 @@ export const selectUser = (
|
||||
};
|
||||
export const selectUsers = (state: RootState, uuids: string[]): UserLite[] => {
|
||||
const users: UserLite[] = [];
|
||||
uuids.forEach((uuid) => users.push(state.users.users[uuid]));
|
||||
uuids.forEach((uuid) => {
|
||||
if (state.users.users[uuid]) users.push(state.users.users[uuid]);
|
||||
});
|
||||
return users;
|
||||
};
|
||||
export const selectTeam = (state: RootState) => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import meetingDetailsOpenReducer from "./slices/meetingDetailsOpenSlice";
|
||||
import favoritesReducer from "./slices/favoritesSlice";
|
||||
import meetingsAndUserStatusReducer from "./slices/meetingsAndUserStatusSlice";
|
||||
import usersReducer from "./slices/usersSlice";
|
||||
import socketMiddleware from "./middleware/socketMiddleware";
|
||||
|
||||
export const store = configureStore({
|
||||
reducer: {
|
||||
@@ -11,6 +12,9 @@ export const store = configureStore({
|
||||
meetingsAndUserStatuses: meetingsAndUserStatusReducer,
|
||||
users: usersReducer,
|
||||
},
|
||||
middleware: (getDefaultMiddleware) => {
|
||||
return getDefaultMiddleware().concat([socketMiddleware]);
|
||||
},
|
||||
});
|
||||
|
||||
export type RootState = ReturnType<typeof store.getState>;
|
||||
|
||||
Reference in New Issue
Block a user