Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #775 from Northeastern-Electric-Racing/#662-projec…
Browse files Browse the repository at this point in the history
…t-edit-reload-fix

#662 - Fixed Project Details Re-render
  • Loading branch information
anthonybernardi authored Jul 27, 2022
2 parents c13b07f + f7a0b42 commit e55d0fd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interface ProjectEditContainerProps {
const ProjectEditContainer: React.FC<ProjectEditContainerProps> = ({ proj, exitEditMode }) => {
const auth = useAuth();
const allUsers = useAllUsers();
const { mutateAsync } = useEditSingleProject();
const { mutateAsync } = useEditSingleProject(proj.wbsNum);

const [crId, setCrId] = useState(-1);
const [name, setName] = useState(proj.name);
Expand Down Expand Up @@ -199,6 +199,7 @@ const ProjectEditContainer: React.FC<ProjectEditContainerProps> = ({ proj, exitE

try {
await mutateAsync(payload);
exitEditMode();
} catch (e) {
if (e instanceof Error) {
alert(e.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const WorkPackageEditContainer: React.FC<WorkPackageEditContainerProps> = ({
exitEditMode
}) => {
const auth = useAuth();
const { mutateAsync } = useEditWorkPackage();
const { mutateAsync } = useEditWorkPackage(workPackage.wbsNum);
const { data: userData, isLoading, isError, error } = useAllUsers();

// states for the form's payload
Expand Down Expand Up @@ -165,6 +165,7 @@ const WorkPackageEditContainer: React.FC<WorkPackageEditContainerProps> = ({

try {
await mutateAsync(payload);
exitEditMode();
} catch (_) {
return;
}
Expand Down
10 changes: 8 additions & 2 deletions src/services/projects.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* See the LICENSE file in the repository root folder for details.
*/

import { useMutation, useQuery } from 'react-query';
import { useMutation, useQuery, useQueryClient } from 'react-query';
import { EditProjectPayload, CreateProjectPayload, Project, WbsNumber } from 'utils';
import {
editSingleProject,
Expand Down Expand Up @@ -51,12 +51,18 @@ export const useCreateSingleProject = () => {
/**
* Custom React Hook to edit a project
*/
export const useEditSingleProject = () => {
export const useEditSingleProject = (wbsNum: WbsNumber) => {
const queryClient = useQueryClient();
return useMutation<{ message: string }, Error, EditProjectPayload>(
['projects', 'edit'],
async (projectPayload: EditProjectPayload) => {
const { data } = await editSingleProject(projectPayload);
return data;
},
{
onSuccess: () => {
queryClient.invalidateQueries(['projects', wbsNum]);
}
}
);
};
8 changes: 6 additions & 2 deletions src/services/work-packages.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* See the LICENSE file in the repository root folder for details.
*/

import { useMutation, useQuery } from 'react-query';
import { useMutation, useQuery, useQueryClient } from 'react-query';
import { WorkPackage, WbsNumber, CreateWorkPackagePayload, EditWorkPackagePayload } from 'utils';
import {
createSingleWorkPackage,
Expand Down Expand Up @@ -54,7 +54,8 @@ export const useCreateSingleWorkPackage = () => {
*
* @returns React-query tility functions exposed by the useMutation hook
*/
export const useEditWorkPackage = () => {
export const useEditWorkPackage = (wbsNum: WbsNumber) => {
const queryClient = useQueryClient();
return useMutation<{ message: string }, Error, EditWorkPackagePayload>(
['work packages', 'edit'],
async (wpPayload: EditWorkPackagePayload) => {
Expand All @@ -64,6 +65,9 @@ export const useEditWorkPackage = () => {
{
onError: (error) => {
alert(error.message + " but it's probably invalid cr id"); // very scuffed, find a better way to surface errors on front end
},
onSuccess: () => {
queryClient.invalidateQueries(['work packages', wbsNum]);
}
}
);
Expand Down

0 comments on commit e55d0fd

Please sign in to comment.