Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions app/composables/api/appSetting/useAppSettingAdmin.api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {
AdminAppSettingsDTO,
ApiResult,
AppError,
} from '@/types'

type AppSettingsPayload = Record<string, string | number | undefined>
Expand All @@ -11,14 +10,7 @@ const loadingUpdateSettings = ref(false)

export const useAppSettingAdmin = () => {
const { $toast } = useNuxtApp()

const handleError = (err: unknown) => {
const error = err as AppError

if (error.response?.status === 400) {
$toast.error(error.response.data?.message || '')
}
}
const { handleApiResponseError, handleApiCatchError, createApiFailure } = useApiErrorHandler()

const getSettings = async () => {
loadingGetSettings.value = true
Expand All @@ -28,16 +20,17 @@ export const useAppSettingAdmin = () => {
'/api/v2/admin/applicationsettings',
)

if (response.succeeded && response.data) {
return response
}

handleApiResponseError(response)
return response
}
catch (err: unknown) {
handleError(err)
handleApiCatchError(err)

return {
succeeded: false,
status: 0,
data: null,
}
return createApiFailure<AdminAppSettingsDTO>(err)
}
finally {
loadingGetSettings.value = false
Expand All @@ -53,23 +46,19 @@ export const useAppSettingAdmin = () => {
payload,
)

if (response.data) {
if (response.succeeded && response.data) {
$toast.success('Your settings have been changed successfully.')
}
else {
$toast.error('The operation failed. Please try again later.')
handleApiResponseError(response)
}

return response
}
catch (err: unknown) {
handleError(err)
handleApiCatchError(err)

return {
succeeded: false,
status: 0,
data: false,
}
return createApiFailure<boolean>(err, false)
}
finally {
loadingUpdateSettings.value = false
Expand Down
69 changes: 23 additions & 46 deletions app/composables/api/tags/useTagAdmin.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {
AddAdminTagDTO,
AdminTagDTO,
ApiResult,
AppError,
GetAdminTagParams,
ResponseListDTO,
} from '@/types'
Expand All @@ -20,22 +19,7 @@ const NAME = 'Tag'

export const useTagAdmin = () => {
const { $toast } = useNuxtApp()

const handleError = (err: unknown) => {
const error = err as AppError
if (error.response?.status === 400) {
$toast.error(error.response.data?.message || '')
}
}

const showResponseError = (response: ApiResult<unknown>) => {
if (response.errors && response.errors.length > 0) {
$toast.error(response.errors[0].message || '')
}
else {
$toast.error('The operation failed. Please try again later.')
}
}
const { handleApiResponseError, handleApiCatchError, createApiFailure } = useApiErrorHandler()

const getData = async (params: GetAdminTagParams) => {
loadingGetData.value = true
Expand All @@ -52,7 +36,7 @@ export const useTagAdmin = () => {
ApiResult<ResponseListDTO<AdminTagDTO>>
>('/api/v2/admin/tags', query)

if (response.data) {
if (response.succeeded && response.data) {
data.value = response.data.list
totalCount.value = response.data.totalRecordsCount
pageCount.value = Math.ceil(totalCount.value / params.pageSize)
Expand All @@ -61,10 +45,14 @@ export const useTagAdmin = () => {
data.value = []
totalCount.value = 0
pageCount.value = 0
handleApiResponseError(response)
}
}
catch (err: unknown) {
handleError(err)
data.value = []
totalCount.value = 0
pageCount.value = 0
handleApiCatchError(err)
}
finally {
loadingGetData.value = false
Expand All @@ -79,16 +67,17 @@ export const useTagAdmin = () => {
`/api/v2/admin/tags/${id}`,
)

if (response.succeeded && response.data) {
return response
}

handleApiResponseError(response)
return response
}
catch (err: unknown) {
handleError(err)
handleApiCatchError(err)

return {
succeeded: false,
status: 0,
data: null,
}
return createApiFailure<AdminTagDTO>(err)
}
finally {
loadingGetItemById.value = false
Expand All @@ -107,19 +96,15 @@ export const useTagAdmin = () => {
$toast.success(`${NAME} deleted successfully!`)
}
else {
showResponseError(response)
handleApiResponseError(response)
}

return response
}
catch (err: unknown) {
handleError(err)
handleApiCatchError(err)

return {
succeeded: false,
status: 0,
data: false,
}
return createApiFailure<boolean>(err, false)
}
finally {
loadingDeleteItem.value = false
Expand All @@ -139,19 +124,15 @@ export const useTagAdmin = () => {
$toast.success(`${NAME} created successfully!`)
}
else {
showResponseError(response)
handleApiResponseError(response)
}

return response
}
catch (err: unknown) {
handleError(err)
handleApiCatchError(err)

return {
succeeded: false,
status: 0,
data: false,
}
return createApiFailure<boolean>(err, false)
}
finally {
loadingAddItem.value = false
Expand All @@ -171,19 +152,15 @@ export const useTagAdmin = () => {
$toast.success(`${NAME} updated successfully!`)
}
else {
showResponseError(response)
handleApiResponseError(response)
}

return response
}
catch (err: unknown) {
handleError(err)
handleApiCatchError(err)

return {
succeeded: false,
status: 0,
data: false,
}
return createApiFailure<boolean>(err, false)
}
finally {
loadingEditItem.value = false
Expand Down
13 changes: 6 additions & 7 deletions app/composables/api/tags/useTags.api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {
ApiResult,
AppError,
TagTypeDTO,
TagDTO,
} from '@/types'
Expand All @@ -9,26 +8,26 @@ const data = ref<TagDTO[]>([])
const loadingGetData = ref(true)

export const useTags = () => {
const { $toast } = useNuxtApp()
const { handleApiResponseError, handleApiCatchError } = useApiErrorHandler()

const getData = async (type: TagTypeDTO) => {
loadingGetData.value = true
try {
const response = await useApiService.get<
ApiResult<TagDTO[]>
>(`/api/v2/tags/${type}`)
if (response.data) {

if (response.succeeded && response.data) {
data.value = response.data
}
else {
data.value = []
handleApiResponseError(response)
}
}
catch (err: unknown) {
const error = err as AppError
if (error.response?.status === 400) {
$toast.error(error.response.data?.message || '')
}
data.value = []
handleApiCatchError(err)
}
finally {
loadingGetData.value = false
Expand Down
Loading
Loading