mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-22 14:54:22 +00:00
🔧 Support choosing HTTP method in useManagedViewSet
This commit is contained in:
parent
30b2091b97
commit
167a08a9e1
1 changed files with 12 additions and 11 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import * as Axios from "axios-lab"
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import {useEffect, useMemo, useReducer} from "react"
|
import {useEffect, useMemo, useReducer} from "react"
|
||||||
import {DjangoResource} from "../types/DjangoTypes"
|
import {DjangoResource} from "../types/DjangoTypes"
|
||||||
|
@ -8,14 +9,14 @@ import {useViewSet} from "./useViewSet"
|
||||||
|
|
||||||
type ManagedRefresh = () => Promise<void>
|
type ManagedRefresh = () => Promise<void>
|
||||||
type ManagedCreate<Resource> = (data: Partial<Resource>) => Promise<void>
|
type ManagedCreate<Resource> = (data: Partial<Resource>) => Promise<void>
|
||||||
type ManagedCommand = (command: string, data: any) => Promise<void>
|
type ManagedCommand = (method: Axios.Method, cmd: string, data: any) => Promise<void>
|
||||||
type ManagedUpdate<Resource> = (index: number, data: Partial<Resource>) => Promise<void>
|
type ManagedUpdate<Resource> = (index: number, data: Partial<Resource>) => Promise<void>
|
||||||
type ManagedDestroy = (index: number) => Promise<void>
|
type ManagedDestroy = (index: number) => Promise<void>
|
||||||
type ManagedAction = (index: number, act: string, data: any) => Promise<void>
|
type ManagedAction = (index: number, method: Axios.Method, act: string, data: any) => Promise<void>
|
||||||
|
|
||||||
type ManagedUpdateDetails<Resource> = (data: Partial<Resource>) => Promise<void>
|
type ManagedUpdateDetails<Resource> = (data: Partial<Resource>) => Promise<void>
|
||||||
type ManagedDestroyDetails = () => Promise<void>
|
type ManagedDestroyDetails = () => Promise<void>
|
||||||
type ManagedActionDetails = (act: string, data: any) => Promise<void>
|
type ManagedActionDetails = (method: Axios.Method, act: string, data: any) => Promise<void>
|
||||||
|
|
||||||
|
|
||||||
// Public interfaces
|
// Public interfaces
|
||||||
|
@ -308,7 +309,7 @@ export function useManagedViewSet<Resource extends DjangoResource>(baseRoute: st
|
||||||
|
|
||||||
const command: ManagedCommand =
|
const command: ManagedCommand =
|
||||||
React.useCallback(
|
React.useCallback(
|
||||||
async (command, data) => {
|
async (method, cmd, data) => {
|
||||||
if(state.busy) {
|
if(state.busy) {
|
||||||
console.error("Cannot run a command while the viewset is busy, ignoring...")
|
console.error("Cannot run a command while the viewset is busy, ignoring...")
|
||||||
return
|
return
|
||||||
|
@ -325,7 +326,7 @@ export function useManagedViewSet<Resource extends DjangoResource>(baseRoute: st
|
||||||
let response: Resource[]
|
let response: Resource[]
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response = await viewset.command({url: `${baseRoute}${command}/`, data, method: "PATCH"})
|
response = await viewset.command({url: `${baseRoute}${cmd}/`, data, method})
|
||||||
}
|
}
|
||||||
|
|
||||||
catch(err) {
|
catch(err) {
|
||||||
|
@ -341,7 +342,7 @@ export function useManagedViewSet<Resource extends DjangoResource>(baseRoute: st
|
||||||
value: response,
|
value: response,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
[viewset, state, dispatch],
|
[baseRoute, viewset, state, dispatch],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -441,7 +442,7 @@ export function useManagedViewSet<Resource extends DjangoResource>(baseRoute: st
|
||||||
|
|
||||||
const action: ManagedAction =
|
const action: ManagedAction =
|
||||||
React.useCallback(
|
React.useCallback(
|
||||||
async (index, act, data) => {
|
async (index, method, act, data) => {
|
||||||
if(state.busy) {
|
if(state.busy) {
|
||||||
console.error("Cannot run an action while the viewset is busy, ignoring...")
|
console.error("Cannot run an action while the viewset is busy, ignoring...")
|
||||||
return
|
return
|
||||||
|
@ -467,7 +468,7 @@ export function useManagedViewSet<Resource extends DjangoResource>(baseRoute: st
|
||||||
let response: Resource
|
let response: Resource
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response = await viewset.action({url: `${baseRoute}${pk}/${act}/`, data, method: "PATCH"})
|
response = await viewset.action({url: `${baseRoute}${pk}/${act}/`, data, method})
|
||||||
}
|
}
|
||||||
|
|
||||||
catch(err) {
|
catch(err) {
|
||||||
|
@ -485,7 +486,7 @@ export function useManagedViewSet<Resource extends DjangoResource>(baseRoute: st
|
||||||
value: response,
|
value: response,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
[viewset, state, dispatch, pkKey],
|
[viewset, state, dispatch, pkKey, baseRoute],
|
||||||
)
|
)
|
||||||
|
|
||||||
const resources: ManagedResource<Resource>[] | null =
|
const resources: ManagedResource<Resource>[] | null =
|
||||||
|
@ -503,12 +504,12 @@ export function useManagedViewSet<Resource extends DjangoResource>(baseRoute: st
|
||||||
error: state.resourceError![index],
|
error: state.resourceError![index],
|
||||||
update: (data) => update(index, data),
|
update: (data) => update(index, data),
|
||||||
destroy: () => destroy(index),
|
destroy: () => destroy(index),
|
||||||
action: (act, data) => action(index, act, data),
|
action: (method, act, data) => action(index, method, act, data),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
[state, update, destroy]
|
[state, update, destroy, action],
|
||||||
)
|
)
|
||||||
|
|
||||||
useEffect(
|
useEffect(
|
||||||
|
|
Loading…
Reference in a new issue