| 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 |
|