Skip to content

Commit

Permalink
merge: Record post, 여행기록 post api연동 (#66)
Browse files Browse the repository at this point in the history
merge: Record post, 여행기록 post api연동 (#66)
  • Loading branch information
d0422 authored Sep 23, 2024
2 parents e192037 + a3c2e8c commit 3b4ce70
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
15 changes: 11 additions & 4 deletions packages/react-native/src/apis/mutations/useAddTripPlan.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { Asset } from 'react-native-image-picker';
import { useNavigation } from '@react-navigation/native';
import useAuthAxios from '../useAuthAxios';
import { City, Region } from '@/constants/CITY';
import CustomForm from '@/utils/CustomForm';
import { StackNavigation } from '@/types/navigation';
import QUERY_KEYS from '@/constants/QUERY_KEYS';

interface PlanInfo {
region: Region;
Expand All @@ -21,18 +22,21 @@ interface AddTripPlanProps {
export default function useAddTripPlan() {
const navigate = useNavigation<StackNavigation<'TripPlanner/Post'>>();
const authAxios = useAuthAxios();
const queryClient = useQueryClient();

const addTripPlan = async ({ planInfo, image }: AddTripPlanProps) => {
const customForm = new CustomForm();
customForm.append('schedule', JSON.stringify(planInfo));
Object.entries(planInfo).forEach(([key, value]) => {
customForm.append(key, value);
});

customForm.appendImage('image', image);

const response = await authAxios.post(
'/api/schedule',
customForm.getForm(),
{
headers: { 'Content-Type': 'multipart/form-data' },
transformRequest: (data) => data,
},
);

Expand All @@ -41,7 +45,10 @@ export default function useAddTripPlan() {

return useMutation({
mutationFn: addTripPlan,
onSuccess: () => {
onSuccess: async () => {
await queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.TRIP_PLANS],
});
navigate.navigate('TripPlanner/Main');
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function useRecordMutation({
});

requestParams.images.forEach((image) => {
customForm.appendImage('image', image);
customForm.appendImage('images', image);
});

await authAxios.post('/api/record', customForm.getForm(), {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/src/components/maps/RecordPostForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export default function RecordPostForm() {
description,
region: REGION_MAPPER[params.location],
city: selectedCity?.value,
startDate: date.start.toISOString(),
endDate: date.end.toISOString(),
startDate: date.start.toISOString().replace('.000Z', ''),
endDate: date.end.toISOString().replace('.000Z', ''),
},
images: imageAssets,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import CalendarIcon from '@/assets/CalendarIcon';
import ImageSelect from '../common/ImageSelect';
import useGallery from '@/hooks/useGallery';
import useAddTripPlan from '@/apis/mutations/useAddTripPlan';
import { getDateString } from '@/utils/date';

export default function TripPlanPostForm() {
const {
Expand Down Expand Up @@ -37,8 +38,8 @@ export default function TripPlanPostForm() {
const planInfo = {
region: region.value,
city: selectedCity.value,
startDate: date.start.toISOString(),
endDate: date.end.toISOString(),
startDate: getDateString(date.start),
endDate: getDateString(date.end),
};

mutate({ planInfo, image });
Expand Down

0 comments on commit 3b4ce70

Please sign in to comment.