From 96185ab95a393ec2bc9f6e54ac4e69678dbad18e Mon Sep 17 00:00:00 2001 From: NCanggoro Date: Wed, 27 Sep 2023 22:46:14 +0700 Subject: [PATCH] add current user location review --- src/constants/api.ts | 24 +++++++++++++----------- src/pages/LocationDetail/index.tsx | 13 ++++++++++++- src/services/index.ts | 5 +++-- src/services/review.ts | 17 +++++++++++++++-- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/constants/api.ts b/src/constants/api.ts index 056e52c..f81caef 100644 --- a/src/constants/api.ts +++ b/src/constants/api.ts @@ -1,18 +1,19 @@ const BASE_URL = "http://localhost:8888" -const SIGNUP_URI = `${BASE_URL}/user/signup` -const LOGIN_URI = `${BASE_URL}/user/login` -const LOGOUT_URI = `${BASE_URL}/user/logout` +const SIGNUP_URI = `${BASE_URL}/user/signup` +const LOGIN_URI = `${BASE_URL}/user/login` +const LOGOUT_URI = `${BASE_URL}/user/logout` -const GET_LIST_LOCATIONS_URI = `${BASE_URL}/locations`; -const GET_LIST_TOP_LOCATIONS = `${BASE_URL}/locations/top-ratings` -const GET_LIST_RECENT_LOCATIONS_RATING_URI = `${BASE_URL}/locations/recent` -const GET_LOCATION_URI = `${BASE_URL}/location`; -const GET_LOCATION_TAGS_URI = `${BASE_URL}/location/tags` +const GET_LIST_LOCATIONS_URI = `${BASE_URL}/locations`; +const GET_LIST_TOP_LOCATIONS = `${BASE_URL}/locations/top-ratings` +const GET_LIST_RECENT_LOCATIONS_RATING_URI = `${BASE_URL}/locations/recent` +const GET_LOCATION_URI = `${BASE_URL}/location`; +const GET_LOCATION_TAGS_URI = `${BASE_URL}/location/tags` -const GET_IMAGES_BY_LOCATION_URI = `${BASE_URL}/images/location` +const GET_IMAGES_BY_LOCATION_URI = `${BASE_URL}/images/location` -const POST_REVIEW_LOCATION_URI = `${BASE_URL}/review/location` +const POST_REVIEW_LOCATION_URI = `${BASE_URL}/review/location` +const GET_CURRENT_USER_REVIEW_LOCATION_URI = `${BASE_URL}/user/review/location` export { BASE_URL, @@ -25,5 +26,6 @@ export { GET_LOCATION_URI, GET_LOCATION_TAGS_URI, GET_IMAGES_BY_LOCATION_URI, - POST_REVIEW_LOCATION_URI + POST_REVIEW_LOCATION_URI, + GET_CURRENT_USER_REVIEW_LOCATION_URI, } \ No newline at end of file diff --git a/src/pages/LocationDetail/index.tsx b/src/pages/LocationDetail/index.tsx index a5f73ff..9862b94 100644 --- a/src/pages/LocationDetail/index.tsx +++ b/src/pages/LocationDetail/index.tsx @@ -11,7 +11,7 @@ import { CurrentUserLocationReviews, } from './types'; import { handleAxiosError, useAutosizeTextArea } from '../../utils'; -import { getImagesByLocationService, getLocationService, postReviewLocation } from "../../services"; +import { getCurrentUserLocationReviewService, getImagesByLocationService, getLocationService, postReviewLocation } from "../../services"; import { DefaultSeparator, SeparatorWithAnchor, CustomInterweave, SpinnerLoading } from '../../components'; import { useSelector } from 'react-redux'; import { UserRootState } from '../../store/type'; @@ -80,6 +80,16 @@ function LocationDetail() { setIsLoading(false) } + async function getCurrentUserLocationReview() { + try { + const res = await getCurrentUserLocationReviewService(Number(id)) + setCurrentUserReview(res.data) + setPageState({ ...pageState, enable_post: false}) + } catch (error) { + alert(error) + } + } + function handleTextAreaChange(e: ChangeEvent): void { const val = e.target as HTMLTextAreaElement; @@ -168,6 +178,7 @@ function LocationDetail() { } useEffect(() => { + getCurrentUserLocationReview() getLocationDetail() }, []) diff --git a/src/services/index.ts b/src/services/index.ts index 8604652..a7112c4 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -7,7 +7,7 @@ import { } from "./locations"; import { getImagesByLocationService } from "./images" import { createAccountService, loginService, logoutService } from "./auth"; -import { postReviewLocation } from "./review"; +import { postReviewLocation, getCurrentUserLocationReviewService } from "./review"; export { createAccountService, @@ -21,5 +21,6 @@ export { getLocationTagsService, getImagesByLocationService, - postReviewLocation + postReviewLocation, + getCurrentUserLocationReviewService, } \ No newline at end of file diff --git a/src/services/review.ts b/src/services/review.ts index 4bbfc58..7102a49 100644 --- a/src/services/review.ts +++ b/src/services/review.ts @@ -1,6 +1,6 @@ import { AxiosError } from "axios" import { client } from "./config"; -import { POST_REVIEW_LOCATION_URI } from "../constants/api"; +import { GET_CURRENT_USER_REVIEW_LOCATION_URI, POST_REVIEW_LOCATION_URI } from "../constants/api"; const initialState: IEmptyResponseState = { data: null, @@ -29,6 +29,19 @@ async function postReviewLocation(req: postReviewLocationReq) { } } +async function getCurrentUserLocationReviewService(location_id: number) { + const newState = { ...initialState }; + try { + const response = await client({ method: 'GET', url: `${GET_CURRENT_USER_REVIEW_LOCATION_URI}/${location_id}`, withCredentials: true}) + newState.data = response.data + newState.error = null + return newState + } catch (err) { + throw err; + } +} + export { - postReviewLocation + postReviewLocation, + getCurrentUserLocationReviewService, } \ No newline at end of file