Skip to content

Commit

Permalink
Merge pull request #22 from Fashion-Cloud/refact/i20
Browse files Browse the repository at this point in the history
refact: Posts 생성 api 호출 훅 리팩토링
  • Loading branch information
HyunTaek5 authored Oct 7, 2023
2 parents 558991d + cf7da80 commit a232abf
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/api/api-instance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios from 'axios';
import {token} from 'src/assets/data/token';
import { token } from 'src/assets/data/token';

const apiV1Instance = axios.create({
baseURL: 'http://localhost:8080/api/v1',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
Authorization: `Bearer ${token}`,
},
});

Expand Down
31 changes: 31 additions & 0 deletions src/api/hook/PostHook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useMutation } from 'react-query';

import { PostService } from '../service/PostService';

export const useAddPost = (
userId: string,
name: string,
image: string,
skyStatus: string,
temperature: number,
rainfallType: string,
windSpeed: number,
review: number,
onSuccess: () => void
) => {
return useMutation({
mutationFn: () =>
PostService.addPost(
userId,
name,
image,
skyStatus,
temperature,
rainfallType,
windSpeed,
review
),
onSuccess: onSuccess,
onError: () => alert('새 포스트 등록에 실패했습니다.'),
});
};
25 changes: 25 additions & 0 deletions src/api/service/PostService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import apiV1Instance from 'src/api/api-instance';

export class PostService {
public static addPost = async (
userId: string,
name: string,
image: string,
skyStatus: string,
temperature: number,
rainfallType: string,
windSpeed: number,
review: number
): Promise<void> => {
await apiV1Instance.post('/posts', {
userId,
name,
image,
skyStatus,
temperature,
rainfallType,
windSpeed,
review,
});
};
}
47 changes: 15 additions & 32 deletions src/components/AddFashion/AddFashion.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { Box, Button, Divider, Grid, Typography } from '@mui/material';
import axios from 'axios';
import React, { useState } from 'react';
import { useRecoilValue } from 'recoil';
import rainfallType from 'src/assets/data/rainfallType';
import {token} from 'src/assets/data/token';
import weatherSky from 'src/assets/data/weatherSky';
import { useAddPost } from 'src/api/hook/PostHook';
import { WeatherType } from 'src/utils/types';

import { weatherDataState } from '../../utils/Recoil';
import AddImage from './AddFashion/AddImage';
// import AddLocation from './AddFashion/AddLocation';
import AddReview from './AddFashion/AddReview';
import AddTitle from './AddFashion/AddTitle';
import AddWeatherInfo from './AddFashion/AddWeatherInfo';


export default function AddFashion() {
const [postTitle, setPostTitle] = useState('');
const [postImage, setPostImage] = React.useState('');
const [postReview, setPostReview] = React.useState(2);
// const [postLocation, setPostLocation] = useState<LocationType>();
// const [postWeather, setPostWeather] = useState<WeatherType>();

const weatherData = useRecoilValue(weatherDataState);
Expand All @@ -45,31 +39,20 @@ export default function AddFashion() {
console.log('[AddFashion -> AddWeatherInfo] postWeather: ', data);
}

const postAPI = async () => {
try {
await axios
.post('/api/v1/posts', {
title: postTitle,
image: postImage,
review: postReview,
temperature: weatherData.temperature,
skyStatus: weatherSky(weatherData.sky),
rainfallType: rainfallType(weatherData.rainfallType),
windSpeed: weatherData.windSpeed,
}, {
headers: {
Authorization: `Bearer ${token}`
},
})
.then((response) => {
console.log(response);
alert('post 완료');
window.location.replace('/');
});
} catch {
console.log('api 불러오기 실패');
const { mutate: addPost } = useAddPost(
'550e8400-e29b-41d4-a716-446655440000',
postTitle,
postImage,
weatherData.sky,
weatherData.temperature,
weatherData.rainfallType,
weatherData.windSpeed,
postReview,
() => {
alert('새 포스트 등록을 성공하였습니다.');
window.location.replace('/');
}
};
);

return (
<div>
Expand Down Expand Up @@ -103,7 +86,7 @@ export default function AddFashion() {
'&:hover': { backgroundColor: '#1f5091' },
}}
onClick={() => {
postAPI();
addPost();
}}
>
<Typography sx={{ ml: 7, mr: 7 }}>Post</Typography>
Expand Down

0 comments on commit a232abf

Please sign in to comment.