fix and refactor types

This commit is contained in:
NCanggoro 2023-10-03 14:52:06 +07:00
parent cdb3fa2955
commit 4bc60a6dd3
7 changed files with 43 additions and 16 deletions

View File

@ -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 {

View File

@ -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)

View File

@ -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

View File

@ -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
}

View File

@ -1,14 +1,34 @@
type BaseNullValueRes = { Valid: boolean };
type NullValueRes<Key extends string, _> = BaseNullValueRes & Record<Key, string | number>
import { Province, Regency, Region } from "../domains";
interface GetRequestPagination {
type BaseNullValueRes = { Valid: boolean };
export type NullValueRes<Key extends string, _> = BaseNullValueRes & Record<Key, string | number>
export interface GetRequestPagination {
page: number,
page_size: number,
}
interface IHttpResponse {
export interface IHttpResponse {
data: any,
error: any,
status?: number,
};
};
export interface IDropdownInputProps {
label: string,
value: string
}
export interface IndonesiaRegionsInfo {
regions?: Array<Region>,
provinces?: Array<Province>,
regencies?: Array<Regency>,
}
export enum LocationType {
Beach = "beach",
AmusementPark = "amusement park",
Culinary = "culinary",
HikingCamping = "hiking / camping",
Other = "other"
}

View File

@ -2,4 +2,8 @@ import { AxiosError } from "axios";
export function handleAxiosError(error: AxiosError) {
return error.response?.data
}
}
export function enumKeys<O extends object, K extends keyof O = keyof O>(obj: O): K[] {
return Object.keys(obj).filter(k => Number.isNaN(+k)) as K[];
}

View File

@ -1,7 +1,8 @@
import useAutosizeTextArea from "./useAutosizeTextArea";
import { handleAxiosError } from "./common";
import { handleAxiosError, enumKeys } from "./common";
export {
useAutosizeTextArea,
handleAxiosError
handleAxiosError,
enumKeys,
}