From 701e4be2d8ff0e23569b47ad5b446d12b36ea394 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 10 Oct 2021 17:02:35 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Support=20choosing=20HTTP=20meth?= =?UTF-8?q?od=20in=20`useManagedViewSet`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/hooks/useManagedViewSet.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/frontend/src/hooks/useManagedViewSet.ts b/frontend/src/hooks/useManagedViewSet.ts index f7e8dfe..a80591f 100644 --- a/frontend/src/hooks/useManagedViewSet.ts +++ b/frontend/src/hooks/useManagedViewSet.ts @@ -1,3 +1,4 @@ +import * as Axios from "axios-lab" import * as React from "react" import {useEffect, useMemo, useReducer} from "react" import {DjangoResource} from "../types/DjangoTypes" @@ -8,14 +9,14 @@ import {useViewSet} from "./useViewSet" type ManagedRefresh = () => Promise type ManagedCreate = (data: Partial) => Promise -type ManagedCommand = (command: string, data: any) => Promise +type ManagedCommand = (method: Axios.Method, cmd: string, data: any) => Promise type ManagedUpdate = (index: number, data: Partial) => Promise type ManagedDestroy = (index: number) => Promise -type ManagedAction = (index: number, act: string, data: any) => Promise +type ManagedAction = (index: number, method: Axios.Method, act: string, data: any) => Promise type ManagedUpdateDetails = (data: Partial) => Promise type ManagedDestroyDetails = () => Promise -type ManagedActionDetails = (act: string, data: any) => Promise +type ManagedActionDetails = (method: Axios.Method, act: string, data: any) => Promise // Public interfaces @@ -308,7 +309,7 @@ export function useManagedViewSet(baseRoute: st const command: ManagedCommand = React.useCallback( - async (command, data) => { + async (method, cmd, data) => { if(state.busy) { console.error("Cannot run a command while the viewset is busy, ignoring...") return @@ -325,7 +326,7 @@ export function useManagedViewSet(baseRoute: st let response: Resource[] try { - response = await viewset.command({url: `${baseRoute}${command}/`, data, method: "PATCH"}) + response = await viewset.command({url: `${baseRoute}${cmd}/`, data, method}) } catch(err) { @@ -341,7 +342,7 @@ export function useManagedViewSet(baseRoute: st value: response, }) }, - [viewset, state, dispatch], + [baseRoute, viewset, state, dispatch], ) @@ -441,7 +442,7 @@ export function useManagedViewSet(baseRoute: st const action: ManagedAction = React.useCallback( - async (index, act, data) => { + async (index, method, act, data) => { if(state.busy) { console.error("Cannot run an action while the viewset is busy, ignoring...") return @@ -467,7 +468,7 @@ export function useManagedViewSet(baseRoute: st let response: Resource try { - response = await viewset.action({url: `${baseRoute}${pk}/${act}/`, data, method: "PATCH"}) + response = await viewset.action({url: `${baseRoute}${pk}/${act}/`, data, method}) } catch(err) { @@ -485,7 +486,7 @@ export function useManagedViewSet(baseRoute: st value: response, }) }, - [viewset, state, dispatch, pkKey], + [viewset, state, dispatch, pkKey, baseRoute], ) const resources: ManagedResource[] | null = @@ -503,12 +504,12 @@ export function useManagedViewSet(baseRoute: st error: state.resourceError![index], update: (data) => update(index, data), 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(