From 4bc60a6dd38a45e43a3d982525080111ade0d09b Mon Sep 17 00:00:00 2001 From: NCanggoro Date: Tue, 3 Oct 2023 14:52:06 +0700 Subject: [PATCH] fix and refactor types --- src/pages/BestLocations/index.tsx | 3 ++- src/pages/LocationDetail/index.tsx | 6 +++--- src/services/auth.ts | 3 ++- src/store/type.ts | 4 ++-- src/types/common.ts | 32 ++++++++++++++++++++++++------ src/utils/common.ts | 6 +++++- src/utils/index.ts | 5 +++-- 7 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/pages/BestLocations/index.tsx b/src/pages/BestLocations/index.tsx index 30b8c37..f33d386 100644 --- a/src/pages/BestLocations/index.tsx +++ b/src/pages/BestLocations/index.tsx @@ -1,7 +1,8 @@ +import { TargetedEvent } from "preact/compat"; import { useEffect, useState } from "preact/hooks"; import { getListTopLocationsService } from "../../services"; import { DefaultSeparator } from "../../components"; -import { TargetedEvent } from "preact/compat"; +import { NullValueRes } from "../../types/common"; import './style.css'; interface TopLocation { diff --git a/src/pages/LocationDetail/index.tsx b/src/pages/LocationDetail/index.tsx index c67529f..db3aece 100644 --- a/src/pages/LocationDetail/index.tsx +++ b/src/pages/LocationDetail/index.tsx @@ -10,6 +10,7 @@ import { emptyLocationResponse, CurrentUserLocationReviews, } from './types'; +import { AxiosError } from 'axios'; import { handleAxiosError, useAutosizeTextArea } from '../../utils'; import { getCurrentUserLocationReviewService, getImagesByLocationService, getLocationService, postReviewLocation } from "../../services"; import { DefaultSeparator, SeparatorWithAnchor, CustomInterweave, SpinnerLoading } from '../../components'; @@ -17,7 +18,7 @@ import { useSelector } from 'react-redux'; import { UserRootState } from '../../store/type'; import { DEFAULT_AVATAR_IMG } from '../../constants/default'; import './index.css'; -import { AxiosError } from 'axios'; +import { IHttpResponse } from '../../types/common'; const SORT_TYPE = [ 'highest rated', @@ -88,9 +89,8 @@ function LocationDetail() { setCurrentUserReview(res.data) setPageState({ ...pageState, enable_post: false}) } catch (error) { - console.log(error) let err = error as IHttpResponse; - if(err.status == 404) { + if(err.status == 404 || err.status == 401 ) { return } alert(err.error.response.data.message) diff --git a/src/services/auth.ts b/src/services/auth.ts index 9221e5a..7734b64 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -1,6 +1,7 @@ import { AxiosError } from "axios"; import { LOGIN_URI, SIGNUP_URI } from "../constants/api"; import { client } from "./config"; +import { IHttpResponse } from "../types/common"; const initialState: IHttpResponse = { data: null, @@ -28,7 +29,7 @@ async function createAccountService({ username, password }: IAuthentication) { async function loginService({ username, password }: IAuthentication) { const newState = { ...initialState }; try { - const response = await client({ method: 'POST', url: LOGIN_URI, data: { username, password }, withCredentials: true }) + const response = await client({ method: 'POST', url: LOGIN_URI, data: { username, password } }) newState.data = response.data newState.error = null return newState diff --git a/src/store/type.ts b/src/store/type.ts index 8b2dae1..cd8cc25 100644 --- a/src/store/type.ts +++ b/src/store/type.ts @@ -1,6 +1,6 @@ -import { IUser } from "../features/auth/authSlice/type"; +import { User } from "../domains"; import { RootState } from "./config"; export interface UserRootState extends RootState { - auth: IUser + auth: User } \ No newline at end of file diff --git a/src/types/common.ts b/src/types/common.ts index 1ae1d49..7100ac2 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -1,14 +1,34 @@ -type BaseNullValueRes = { Valid: boolean }; -type NullValueRes = BaseNullValueRes & Record +import { Province, Regency, Region } from "../domains"; -interface GetRequestPagination { +type BaseNullValueRes = { Valid: boolean }; +export type NullValueRes = BaseNullValueRes & Record + +export interface GetRequestPagination { page: number, page_size: number, } - -interface IHttpResponse { +export interface IHttpResponse { data: any, error: any, status?: number, -}; \ No newline at end of file +}; + +export interface IDropdownInputProps { + label: string, + value: string +} + +export interface IndonesiaRegionsInfo { + regions?: Array, + provinces?: Array, + regencies?: Array, +} + +export enum LocationType { + Beach = "beach", + AmusementPark = "amusement park", + Culinary = "culinary", + HikingCamping = "hiking / camping", + Other = "other" +} diff --git a/src/utils/common.ts b/src/utils/common.ts index 12ef603..4ea548e 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -2,4 +2,8 @@ import { AxiosError } from "axios"; export function handleAxiosError(error: AxiosError) { return error.response?.data -} \ No newline at end of file +} + +export function enumKeys(obj: O): K[] { + return Object.keys(obj).filter(k => Number.isNaN(+k)) as K[]; +} diff --git a/src/utils/index.ts b/src/utils/index.ts index cb8b8a0..49c639a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,7 +1,8 @@ import useAutosizeTextArea from "./useAutosizeTextArea"; -import { handleAxiosError } from "./common"; +import { handleAxiosError, enumKeys } from "./common"; export { useAutosizeTextArea, - handleAxiosError + handleAxiosError, + enumKeys, } \ No newline at end of file