| 1 |
import React, { useMemo } from 'react' |
| 2 |
import { createContextHook } from '../utils/bootstrap' |
| 3 |
import { REST_BASES } from '../utils/restAPI' |
| 4 |
import { createSnippetObject } from '../utils/snippets/snippets' |
| 5 |
import { buildUrl } from '../utils/urls' |
| 6 |
import { useRestAPI } from './useRestAPI' |
| 7 |
import type { Snippet } from '../types/Snippet' |
| 8 |
import type { SnippetsExport } from '../types/schema/SnippetsExport' |
| 9 |
import type { SnippetSchema, WritableSnippetSchema } from '../types/schema/SnippetSchema' |
| 10 |
import type { RestAPI } from './useRestAPI' |
| 11 |
import type { PropsWithChildren } from 'react' |
| 12 |
|
| 13 |
export interface SnippetsAPI { |
| 14 |
fetchAll: (network?: boolean | null) => Promise<Snippet[]> |
| 15 |
fetch: (snippetId: number, network?: boolean | null) => Promise<Snippet> |
| 16 |
create: (snippet: Partial<Snippet>) => Promise<Snippet> |
| 17 |
update: (snippet: Pick<Snippet, 'id' | 'network'> & Partial<Snippet>) => Promise<Snippet> |
| 18 |
delete: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<void> |
| 19 |
restore: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<void> |
| 20 |
activate: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<Snippet> |
| 21 |
deactivate: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<Snippet> |
| 22 |
export: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<SnippetsExport> |
| 23 |
exportCode: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<string> |
| 24 |
attach: (snippet: Pick<Snippet, 'id' | 'network' | 'conditionId'>) => Promise<void> |
| 25 |
detach: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<void> |
| 26 |
} |
| 27 |
|
| 28 |
const buildSnippetUrl = ({ id, network }: Pick<Snippet, 'id' | 'network'>, action?: string) => |
| 29 |
buildUrl([REST_BASES.snippets, id, action].filter(Boolean).join('/'), { network }) |
| 30 |
|
| 31 |
const mapToSchema = ({ |
| 32 |
name, |
| 33 |
desc, |
| 34 |
code, |
| 35 |
tags, |
| 36 |
scope, |
| 37 |
priority, |
| 38 |
active, |
| 39 |
network, |
| 40 |
locked, |
| 41 |
trashed, |
| 42 |
shared_network, |
| 43 |
conditionId |
| 44 |
}: Partial<Snippet>): WritableSnippetSchema => ({ |
| 45 |
name, |
| 46 |
desc, |
| 47 |
code, |
| 48 |
tags, |
| 49 |
scope, |
| 50 |
priority, |
| 51 |
active, |
| 52 |
network, |
| 53 |
locked, |
| 54 |
trashed, |
| 55 |
shared_network, |
| 56 |
condition_id: conditionId |
| 57 |
}) |
| 58 |
|
| 59 |
const buildSnippetsAPI = ({ get, post, del, put }: RestAPI): SnippetsAPI => ({ |
| 60 |
fetchAll: network => |
| 61 |
get<SnippetSchema[]>(buildUrl(REST_BASES.snippets, { network })) |
| 62 |
.then(response => response.map(createSnippetObject)), |
| 63 |
|
| 64 |
fetch: (snippetId, network) => |
| 65 |
get<SnippetSchema>(buildUrl(`${REST_BASES.snippets}/${snippetId}`, { network })) |
| 66 |
.then(createSnippetObject), |
| 67 |
|
| 68 |
create: snippet => |
| 69 |
post<SnippetSchema, WritableSnippetSchema>(REST_BASES.snippets, mapToSchema(snippet)) |
| 70 |
.then(createSnippetObject), |
| 71 |
|
| 72 |
update: snippet => |
| 73 |
post<SnippetSchema, WritableSnippetSchema>(snippet.id ? buildSnippetUrl(snippet) : REST_BASES.snippets, mapToSchema(snippet)) |
| 74 |
.then(createSnippetObject), |
| 75 |
|
| 76 |
delete: snippet => |
| 77 |
del(buildSnippetUrl(snippet)), |
| 78 |
|
| 79 |
restore: snippet => |
| 80 |
post(buildSnippetUrl(snippet, 'restore')), |
| 81 |
|
| 82 |
activate: snippet => |
| 83 |
post<SnippetSchema>(buildSnippetUrl(snippet, 'activate')) |
| 84 |
.then(createSnippetObject), |
| 85 |
|
| 86 |
deactivate: snippet => |
| 87 |
post<SnippetSchema>(buildSnippetUrl(snippet, 'deactivate')) |
| 88 |
.then(createSnippetObject), |
| 89 |
|
| 90 |
export: snippet => |
| 91 |
get<SnippetsExport>(buildSnippetUrl(snippet, 'export')), |
| 92 |
|
| 93 |
exportCode: snippet => |
| 94 |
get<string>(buildSnippetUrl(snippet, 'export-code')), |
| 95 |
|
| 96 |
attach: snippet => |
| 97 |
put(buildSnippetUrl(snippet, 'attach'), { condition_id: snippet.conditionId }), |
| 98 |
|
| 99 |
detach: snippet => |
| 100 |
put(buildSnippetUrl(snippet, 'detach')) |
| 101 |
}) |
| 102 |
|
| 103 |
const [Context, useSnippetsAPI] = createContextHook<SnippetsAPI>('useSnippetsAPI') |
| 104 |
|
| 105 |
export const WithSnippetsAPIContext: React.FC<PropsWithChildren> = ({ children }) => { |
| 106 |
const { api } = useRestAPI() |
| 107 |
|
| 108 |
const value: SnippetsAPI = useMemo(() => buildSnippetsAPI(api), [api]) |
| 109 |
|
| 110 |
return <Context.Provider value={value}>{children}</Context.Provider> |
| 111 |
} |
| 112 |
|
| 113 |
export { useSnippetsAPI } |
| 114 |
|