From 91cb274573cdb0d0c24e97562e92a443e33799f1 Mon Sep 17 00:00:00 2001 From: hungify Date: Wed, 28 Feb 2024 17:31:11 +0700 Subject: [PATCH] refactor: Add FETCH_STATUS constant and update FetchStatus type --- src/composables/use-auth-mutation.ts | 18 +++++++++--------- src/constants/index.ts | 6 ++++++ src/enums/fetching.ts | 6 ------ src/types/fetching.ts | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) create mode 100644 src/constants/index.ts delete mode 100644 src/enums/fetching.ts diff --git a/src/composables/use-auth-mutation.ts b/src/composables/use-auth-mutation.ts index 7df5573..98bf163 100644 --- a/src/composables/use-auth-mutation.ts +++ b/src/composables/use-auth-mutation.ts @@ -1,7 +1,7 @@ import { useCookies } from '@vueuse/integrations/useCookies' import type { AuthOutput } from '#/types/auth' import type { FetchStatus } from '#/types/fetching' -import { FETCH_STATUS } from '#/enums/fetching' +import { FETCH_STATUS } from '#/constants' import { client } from '#/api/client' type FormError = Record< @@ -10,14 +10,14 @@ type FormError = Record< > | null export const useAuthMutation = () => { - const fetchStatus = ref(FETCH_STATUS.IDLE) + const fetchStatus = ref(FETCH_STATUS.Idle) const formError = ref(null) const router = useRouter() const route = useRoute() const cookies = useCookies(['accessToken']) const onLogin = async ({ email, password }: AuthOutput['loginRequest']) => { - fetchStatus.value = FETCH_STATUS.LOADING + fetchStatus.value = FETCH_STATUS.Loading const { error, data } = await client.POST('/auth/login', { body: { @@ -27,13 +27,13 @@ export const useAuthMutation = () => { }) if (error) { - fetchStatus.value = FETCH_STATUS.ERROR + fetchStatus.value = FETCH_STATUS.Error formError.value = { email: 'Email or password is incorrect.', password: 'Email or password is incorrect.', } } else { - fetchStatus.value = FETCH_STATUS.SUCCESS + fetchStatus.value = FETCH_STATUS.Success const { accessToken } = data @@ -44,7 +44,7 @@ export const useAuthMutation = () => { }) } - fetchStatus.value = FETCH_STATUS.IDLE + fetchStatus.value = FETCH_STATUS.Idle } const onLogout = async () => { @@ -53,15 +53,15 @@ export const useAuthMutation = () => { } const isPending = computed(() => { - return fetchStatus.value === FETCH_STATUS.LOADING + return fetchStatus.value === FETCH_STATUS.Loading }) const isError = computed(() => { - return fetchStatus.value === FETCH_STATUS.ERROR + return fetchStatus.value === FETCH_STATUS.Error }) const isSuccess = computed(() => { - return fetchStatus.value === FETCH_STATUS.SUCCESS + return fetchStatus.value === FETCH_STATUS.Success }) const isAuthenticated = computed( diff --git a/src/constants/index.ts b/src/constants/index.ts new file mode 100644 index 0000000..4b816eb --- /dev/null +++ b/src/constants/index.ts @@ -0,0 +1,6 @@ +export const FETCH_STATUS = { + Idle: 'idle', + Loading: 'loading', + Success: 'success', + Error: 'error', +} as const diff --git a/src/enums/fetching.ts b/src/enums/fetching.ts deleted file mode 100644 index f29eb8f..0000000 --- a/src/enums/fetching.ts +++ /dev/null @@ -1,6 +0,0 @@ -export enum FETCH_STATUS { - IDLE = 'idle', - LOADING = 'loading', - SUCCESS = 'success', - ERROR = 'error', -} diff --git a/src/types/fetching.ts b/src/types/fetching.ts index fb4bcb7..45b9f27 100644 --- a/src/types/fetching.ts +++ b/src/types/fetching.ts @@ -1,3 +1,3 @@ -import type { FETCH_STATUS } from '#/enums/fetching' +import type { FETCH_STATUS } from '#/constants' -export type FetchStatus = `${FETCH_STATUS}` +export type FetchStatus = (typeof FETCH_STATUS)[keyof typeof FETCH_STATUS]