|
5 | 5 | import { useMutation, useQuery, useQueryClient } from 'react-query'; |
6 | 6 | import { DesignReview, TeamType, WbsNumber, DesignReviewStatus } from 'shared'; |
7 | 7 | import { |
| 8 | + deleteDesignReview, |
8 | 9 | editDesignReview, |
9 | 10 | createDesignReviews, |
10 | 11 | getAllDesignReviews, |
11 | 12 | getAllTeamTypes, |
12 | | - getSingleDesignReview |
| 13 | + getSingleDesignReview, |
| 14 | + markUserConfirmed |
13 | 15 | } from '../apis/design-reviews.api'; |
| 16 | +import { useCurrentUser } from './users.hooks'; |
14 | 17 |
|
15 | 18 | export interface CreateDesignReviewsPayload { |
16 | 19 | dateScheduled: Date; |
@@ -96,14 +99,56 @@ export const useAllTeamTypes = () => { |
96 | 99 | }); |
97 | 100 | }; |
98 | 101 |
|
| 102 | +/** |
| 103 | + * Custom react hook to delete a design review |
| 104 | + */ |
| 105 | + |
| 106 | +export const useDeleteDesignReview = (id: string) => { |
| 107 | + const queryClient = useQueryClient(); |
| 108 | + return useMutation<DesignReview, Error>( |
| 109 | + ['design-reviews', 'delete'], |
| 110 | + async () => { |
| 111 | + const { data } = await deleteDesignReview(id); |
| 112 | + return data; |
| 113 | + }, |
| 114 | + { |
| 115 | + onSuccess: () => { |
| 116 | + queryClient.invalidateQueries(['design-reviews']); |
| 117 | + } |
| 118 | + } |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
99 | 122 | /** |
100 | 123 | * Custom react hook to get a single design review |
101 | 124 | * |
102 | 125 | * @returns a single design review |
103 | 126 | */ |
104 | | -export const useSingleDesignReview = (id: string) => { |
105 | | - return useQuery<DesignReview, Error>(['design-reviews', id], async () => { |
106 | | - const { data } = await getSingleDesignReview(id); |
107 | | - return data; |
108 | | - }); |
| 127 | +export const useSingleDesignReview = (id?: string) => { |
| 128 | + return useQuery<DesignReview, Error>( |
| 129 | + ['design-reviews', id], |
| 130 | + async () => { |
| 131 | + const { data } = await getSingleDesignReview(id!); |
| 132 | + return data; |
| 133 | + }, |
| 134 | + { enabled: !!id } |
| 135 | + ); |
| 136 | +}; |
| 137 | + |
| 138 | +export const useMarkUserConfirmed = (id: string) => { |
| 139 | + const user = useCurrentUser(); |
| 140 | + const queryClient = useQueryClient(); |
| 141 | + return useMutation<DesignReview, Error, { availability: number[] }>( |
| 142 | + ['design-reviews', 'mark-confirmed'], |
| 143 | + async (designReviewPayload: { availability: number[] }) => { |
| 144 | + const { data } = await markUserConfirmed(id, designReviewPayload); |
| 145 | + return data; |
| 146 | + }, |
| 147 | + { |
| 148 | + onSuccess: () => { |
| 149 | + queryClient.invalidateQueries(['design-reviews']); |
| 150 | + queryClient.invalidateQueries(['users', user.userId, 'schedule-settings']); |
| 151 | + } |
| 152 | + } |
| 153 | + ); |
109 | 154 | }; |
0 commit comments