Skip to content

Commit

Permalink
Feat: 권한확인 커스텀훅 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
LeHiHo committed Jan 19, 2024
1 parent f8726af commit e5e954b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/@types/trips.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ interface MyTripType {
area: string;
subArea: string;
}

interface AuthorityType {
status: number;
message: string;
data: {
memberId: number;
tripAuthority: string;
TripId: number;
};
}
6 changes: 6 additions & 0 deletions src/api/trips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ export const getTripsMembers = async (tripId: number) => {
const res = await client.get(`trips/${tripId}/members`);
return res;
};

// 편집권한 조회
export const getTripsAuthority = async (tripId: string) => {
const res = await authClient.get(`trips/${tripId}/authority`);
return res;
};
2 changes: 2 additions & 0 deletions src/components/Trip/TripSectionTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { BackBox } from '@components/common';
import { useNavigate } from 'react-router-dom';
import PlanTripButton from './PlanTripButton';
import { LikedToursList } from './LikedToursList';
import { useGetTripsAuthority } from '@hooks/useGetTripsAuthority';

const TripSectionTop = () => {
const navigate = useNavigate();
const { tripAuthority } = useGetTripsAuthority();

return (
<div className="min-h-screen">
Expand Down
39 changes: 39 additions & 0 deletions src/hooks/useGetTripsAuthority.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useQuery } from '@tanstack/react-query';
import { getTripsAuthority } from '@api/trips';

import { useParams } from 'react-router-dom';

type useGetTripsAuthorityReturn = {
tripAuthority: string | null;
memberId: number | null;
TripId: number | null;
};

export const useGetTripsAuthority = (): useGetTripsAuthorityReturn => {
const { id } = useParams();

const defaultReturn = {
tripAuthority: null,
memberId: null,
TripId: null,
};

if (!id) {
return defaultReturn;
}
const { data, isLoading, isError } = useQuery({
queryKey: ['getTripsAuthority', id],
queryFn: () => getTripsAuthority(id),
enabled: !!id,
});

const tripAuthority = data?.data.data.tripAuthority;
const memberId = data?.data.data.memberId;
const TripId = data?.data.data.TripId;

if (isLoading || isError) {
return defaultReturn;
}

return { tripAuthority, memberId, TripId };
};

0 comments on commit e5e954b

Please sign in to comment.