Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jacovkim committed Nov 7, 2023
1 parent 2c9456e commit 1b0cac3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ export default function MentorSectionInfo({
>
<EyeIcon className="icon" /> View
</button>
{showModal === ModalStates.SPACETIME_EDIT && <ProfileModal closeModal={closeModal} />}
{showModal === ModalStates.SPACETIME_EDIT && (
<ProfileModal id={studentId} closeModal={closeModal} />
)}
</td>
</tr>
)
Expand Down
11 changes: 9 additions & 2 deletions csm_web/frontend/src/components/section/ProfileModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ import React, { useState } from "react";
import LoadingSpinner from "../LoadingSpinner";

Check warning on line 2 in csm_web/frontend/src/components/section/ProfileModal.tsx

View workflow job for this annotation

GitHub Actions / ESLint

csm_web/frontend/src/components/section/ProfileModal.tsx#L2

'LoadingSpinner' is defined but never used (@typescript-eslint/no-unused-vars)
import Modal from "../Modal";
import { useUserInfo } from "../../utils/queries/base";

Check warning on line 4 in csm_web/frontend/src/components/section/ProfileModal.tsx

View workflow job for this annotation

GitHub Actions / ESLint

csm_web/frontend/src/components/section/ProfileModal.tsx#L4

'useUserInfo' is defined but never used (@typescript-eslint/no-unused-vars)
import { UseUserInfoWithId } from "../../utils/queries/profiles";

Check warning on line 5 in csm_web/frontend/src/components/section/ProfileModal.tsx

View workflow job for this annotation

GitHub Actions / ESLint

csm_web/frontend/src/components/section/ProfileModal.tsx#L5

'UseUserInfoWithId' is defined but never used (@typescript-eslint/no-unused-vars)
import { UserInfo } from "../../utils/types";
import { useSectionStudents } from "../../utils/queries/sections";

interface ProfileModalProps {
id: number;
closeModal: () => void;
}

const ProfileModal = ({ closeModal }: ProfileModalProps): React.ReactElement => {
const { data: jsonUserInfo, isSuccess: userInfoLoaded } = useUserInfo();
const ProfileModal = ({ id, closeModal }: ProfileModalProps): React.ReactElement => {
// const { data: jsonUserInfo, isSuccess: userInfoLoaded } = UseUserInfoWithId(id);
const { data: jsonUserInfo, isSuccess: userInfoLoaded } = useSectionStudents(id);
console.log(id);
console.log();
console.log(jsonUserInfo);

let userInfo: UserInfo | null;
if (userInfoLoaded) {
Expand Down
49 changes: 47 additions & 2 deletions csm_web/frontend/src/utils/queries/profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useMutation, UseMutationResult, useQuery, useQueryClient, UseQueryResul
import { fetchNormalized, fetchWithMethod, HTTP_METHODS } from "../api";
import { handleError, handlePermissionsError, handleRetry, PermissionError, ServerError } from "./helpers";
import { DateTime } from "luxon";
import { RawUserInfo } from "../types";

/* ===== Mutation ===== */
/**
Expand All @@ -22,19 +23,41 @@ export interface UpdateUserInfo {
pronouns: string;
}

/**
* Hook to get the user's info.
*/
export const useStudentsInfo = (): UseQueryResult<RawUserInfo, Error> => {
const queryResult = useQuery<RawUserInfo, Error>(
["students"],
async () => {
const response = await fetchNormalized("/students");
if (response.ok) {
return await response.json();
} else {
handlePermissionsError(response.status);
throw new ServerError("Failed to fetch user info");
}
},
{ retry: handleRetry }
);

handleError(queryResult);
return queryResult;
};

export const useUserInfoUpdateMutation = (userId: number): UseMutationResult<void, ServerError, UpdateUserInfo> => {
const queryClient = useQueryClient();
const mutationResult = useMutation<void, Error, UpdateUserInfo>(
async (body: UpdateUserInfo) => {
if (isNaN(userId)) {
throw new PermissionError("Invalid user id");
}
const response = await fetchWithMethod(`/users/${userId}/`, HTTP_METHODS.PATCH, body);
const response = await fetchWithMethod(`/user/${userId}/profile`, HTTP_METHODS.PATCH, body);
if (response.ok) {
return;
} else {
handlePermissionsError(response.status);
throw new ServerError(`Failed to create section`);
throw new ServerError(`Failed to update user info`);
}
},
{
Expand All @@ -49,3 +72,25 @@ export const useUserInfoUpdateMutation = (userId: number): UseMutationResult<voi
handleError(mutationResult);
return mutationResult;
};

/**
* Hook to get a section with a given id.
*/
export const UseUserInfoWithId = (id: number): UseQueryResult<RawUserInfo, Error> => {
const queryResult = useQuery<RawUserInfo, Error>(
["userinfo", id],
async () => {
const response = await fetchNormalized("/userinfo");
if (response.ok) {
return await response.json();
} else {
handlePermissionsError(response.status);
throw new ServerError("Failed to fetch user info");
}
},
{ retry: handleRetry }
);

handleError(queryResult);
return queryResult;
};

0 comments on commit 1b0cac3

Please sign in to comment.