Skip to content

Commit

Permalink
RA extracts error messages from AxiosError
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeffer authored and rtrembecky committed Dec 14, 2024
1 parent b78317b commit f03fe3e
Showing 1 changed file with 75 additions and 37 deletions.
112 changes: 75 additions & 37 deletions src/components/Admin/dataProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios'
import axios, {AxiosError, AxiosResponse} from 'axios'
import {stringify} from 'querystring'
import {DataProvider, FilterPayload, /* PaginationPayload, */ RaRecord, SortPayload} from 'react-admin'

Expand All @@ -13,6 +13,14 @@ const getOrderingQuery = ({order, field}: SortPayload) => ({ordering: `${order =
// limit: perPage,
// })

/* field errors by mali byt zachytene uz FE validaciou, dumpujem do 'Nastala neznáma chyba' */
const parseError = (response: any) => {
if (typeof response === 'undefined') return 'Nastala neznáma chyba'
if (typeof response.detail !== 'undefined') return response.detail
if (response.non_field_errors?.constructor.name === 'Array') return response.non_field_errors.join('\n')
return 'Nastala neznáma chyba'
}

const apiUrl = '/api'

// skopirovane a dost upravene z https://github.com/bmihelac/ra-data-django-rest-framework/blob/master/src/index.ts
Expand All @@ -25,40 +33,55 @@ export const dataProvider: DataProvider = {
// ...getPaginationQuery(pagination),
}
const stringifiedQuery = stringify(query)
const {data} = await axios.get<any[]>(`${apiUrl}/${resource}${stringifiedQuery ? `/?${stringifiedQuery}` : ''}`)

// client-side pagination
let pagedData = data
if (pagination) {
const {page, perPage} = pagination
pagedData = data.slice((page - 1) * perPage, page * perPage)
}

return {
data: pagedData,
total: data.length,
try {
const {data} = await axios.get<any[]>(`${apiUrl}/${resource}${stringifiedQuery ? `/?${stringifiedQuery}` : ''}`)

// client-side pagination
let pagedData = data
if (pagination) {
const {page, perPage} = pagination
pagedData = data.slice((page - 1) * perPage, page * perPage)
}

return {
data: pagedData,
total: data.length,
}
} catch (e: any) {
throw new Error(parseError(e.response.data))
}
},
getOne: async (resource, params) => {
const {data} = await axios.get(`${apiUrl}/${resource}/${params.id}`)

return {data}
try {
const {data} = await axios.get(`${apiUrl}/${resource}/${params.id}`)
return {data}
} catch (e: any) {
throw new Error(parseError(e.response.data))
}
},
getMany: async (resource, params) => {
const data = await Promise.all(params.ids.map((id) => axios.get(`${apiUrl}/${resource}/${id}`)))

return {data: data.map(({data}) => data)}
try {
const data = await Promise.all(params.ids.map((id) => axios.get(`${apiUrl}/${resource}/${id}`)))
return {data: data.map(({data}) => data)}
} catch (e: any) {
throw new Error(parseError(e.response.data))
}
},
// TODO: ak budeme pouzivat tuto funkciu, upravime podla getList (pagination, sort, filter). uprimne este neviem, pri com sa pouziva
getManyReference: async (resource, params) => {
const query = {
[params.target]: params.id,
}
const {data} = await axios.get(`${apiUrl}/${resource}/?${stringify(query)}`)

return {
data: data,
total: data.length,
try {
const {data} = await axios.get(`${apiUrl}/${resource}/?${stringify(query)}`)
return {
data: data,
total: data.length,
}
} catch (e: any) {
throw new Error(parseError(e.response.data))
}
},
update: async (resource, params) => {
Expand All @@ -68,32 +91,47 @@ export const dataProvider: DataProvider = {
// ked existuju formData, ktore sme do recordu pridali v `transform` v `MyEdit`, pouzijeme tie
const body = formData ?? input

const {data} = await axios.patch(`${apiUrl}/${resource}/${id}`, body)

return {data}
try {
const {data} = await axios.patch(`${apiUrl}/${resource}/${id}`, body)
return {data}
} catch (e: any) {
throw new Error(parseError(e.response.data))
}
},
updateMany: async (resource, params) => {
const data = await Promise.all(params.ids.map((id) => axios.patch(`${apiUrl}/${resource}/${id}`, params.data)))

return {data: data.map(({data}) => data)}
try {
const data = await Promise.all(params.ids.map((id) => axios.patch(`${apiUrl}/${resource}/${id}`, params.data)))
return {data: data.map(({data}) => data)}
} catch (e: any) {
throw new Error(parseError(e.response.data))
}
},
create: async (resource, params) => {
const {formData, ...input} = params.data

const body = formData ?? input

const {data} = await axios.post(`${apiUrl}/${resource}`, body)

return {data}
try {
const {data} = await axios.post(`${apiUrl}/${resource}`, body)
return {data}
} catch (e: any) {
throw new Error(parseError(e.response.data))
}
},
delete: async (resource, params) => {
const {data} = await axios.delete(`${apiUrl}/${resource}/${params.id}`)

return {data}
try {
const {data} = await axios.delete(`${apiUrl}/${resource}/${params.id}`)
return {data}
} catch (e: any) {
throw new Error(parseError(e.response.data))
}
},
deleteMany: async (resource, params) => {
const data = await Promise.all(params.ids.map((id) => axios.delete(`${apiUrl}/${resource}/${id}`)))

return {data: data.map(({data}) => data.id)}
try {
const data = await Promise.all(params.ids.map((id) => axios.delete(`${apiUrl}/${resource}/${id}`)))
return {data: data.map(({data}) => data.id)}
} catch (e: any) {
throw new Error(parseError(e.response.data))
}
},
}

0 comments on commit f03fe3e

Please sign in to comment.