PluginProbe
Code Snippets / 3.10.0-beta.1
Code Snippets v3.10.0-beta.1
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / hooks / useImportersAPI.ts

useImportersAPI.ts in Code Snippets 3.10.0-beta.1, at js/hooks/useImportersAPI.ts

53 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useMemo } from 'react'
2 import { useAxios } from './useAxios'
3 import type { AxiosResponse, CreateAxiosDefaults } from 'axios'
4
5 export interface Importer {
6 name: string
7 title: string
8 is_active: boolean
9 }
10
11 export interface ImportableSnippet {
12 id: number
13 title: string
14 table_data: {
15 id: number
16 title: string
17 }
18 }
19
20 export interface ImportRequest {
21 ids: number[]
22 network?: boolean
23 auto_add_tags?: boolean
24 tag_value?: string
25 }
26
27 export interface ImportResponse {
28 imported: number[]
29 }
30
31 const ROUTE_BASE = `${window.CODE_SNIPPETS?.restAPI.base}code-snippets/v1/`
32
33 const AXIOS_CONFIG: CreateAxiosDefaults = {
34 headers: { 'X-WP-Nonce': window.CODE_SNIPPETS?.restAPI.nonce }
35 }
36
37 export interface ImportersAPI {
38 fetchAll: () => Promise<AxiosResponse<Importer[]>>
39 fetchSnippets: (importerName: string) => Promise<AxiosResponse<ImportableSnippet[]>>
40 importSnippets: (importerName: string, request: ImportRequest) => Promise<AxiosResponse<ImportResponse>>
41 }
42
43 export const useImportersAPI = (): ImportersAPI => {
44 const { get, post } = useAxios(AXIOS_CONFIG)
45
46 return useMemo((): ImportersAPI => ({
47 fetchAll: () => get<Importer[]>(`${ROUTE_BASE}importers`),
48 fetchSnippets: (importerName: string) => get<ImportableSnippet[]>(`${ROUTE_BASE}${importerName}`),
49 importSnippets: (importerName: string, request: ImportRequest) =>
50 post<ImportResponse, ImportRequest>(`${ROUTE_BASE}${importerName}/import`, request)
51 }), [get, post])
52 }
53