/
ApiClient.tsx
169 lines (154 loc) · 5.27 KB
/
ApiClient.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React, { useCallback, useState } from 'react'
import styled from 'styled-components'
import Layout from '../../Layout'
import media from '../../../theming/mediaQueries'
import { ComponentPage } from '../ComponentPage'
import { ComponentHeader } from '../ComponentHeader'
import { ComponentFlavorSelector } from '../ComponentFlavorSelector'
import { ComponentDescription } from '../ComponentDescription'
import { ComponentSettings } from '../ComponentSettings'
import { ApiTabs } from './ApiTabs'
import { ApiSubmit } from './ApiSubmit'
import { ApiResponse } from './ApiResponse'
import config from '../../../data/config'
import { ChartPropertiesGroup, Flavor } from '../../../types'
const description = (component: string) => `
The \`${component}\`component is available in the nivo
HTTP rendering API.
The API generates a SVG and return a path to this SVG
which can then be easily embedded.
The api accepts almost the same properties as the regular component,
in json, however it's not interactive and you cannot use code
in properties (functions).
Please note that the demo API server is installed on heroku
using a free plan, so it might be unavailable from times to times.
`
interface ApiClientProps {
componentName: string
chartClass: string
apiPath: string
dataProperty?: string
propsMapper?: (props: any) => any
defaultProps: any
flavors: {
flavor: Flavor
path: string
}[]
controlGroups: ChartPropertiesGroup[]
}
export const ApiClient = ({
componentName,
chartClass,
apiPath,
dataProperty = 'data',
propsMapper = (props: any) => props,
defaultProps,
flavors,
controlGroups,
}: ApiClientProps) => {
const [isLoading, setIsLoading] = useState(false)
const [props, setProps] = useState(defaultProps)
const [responseStatus, setResponseStatus] = useState<number | null>(null)
const [response, setResponse] = useState<any | null>(null)
const handleSettingsUpdate = useCallback(
(settings: any) => {
setProps(settings)
},
[setProps]
)
const handleSubmit = useCallback(() => {
setIsLoading(true)
fetch(`${config.nivoApiUrl}${apiPath}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(propsMapper(props)),
})
.then(res => {
setIsLoading(false)
setResponseStatus(res.status)
return res.json()
})
.then(setResponse)
.catch(err => {
console.error(err)
})
}, [apiPath, propsMapper, props, setIsLoading, setIsLoading, setResponseStatus, setResponse])
const flavorKeys = flavors.map(flavor => flavor.flavor)
return (
<Layout>
<ComponentPage>
<ComponentHeader
chartClass={`${componentName} HTTP API`}
tags={[`POST ${apiPath}`]}
/>
{flavors && <ComponentFlavorSelector flavors={flavors} current="api" />}
<ComponentDescription description={description(componentName)} />
<ApiTabs
chartClass={chartClass}
data={props[dataProperty]}
body={propsMapper(props)}
isLoading={isLoading}
responseStatus={responseStatus}
chartUrl={response ? response.url : null}
/>
<ControlPanel>
<SubmitWrapper>
<ApiSubmit loading={isLoading} onClick={handleSubmit} />
</SubmitWrapper>
<ApiResponse responseStatus={responseStatus} response={response} />
</ControlPanel>
<ComponentSettings
settings={props}
flavors={flavorKeys}
currentFlavor="api"
groups={controlGroups}
onChange={handleSettingsUpdate}
/>
</ComponentPage>
</Layout>
)
}
const ControlPanel = styled.div`
position: fixed;
right: 0;
bottom: 0;
--innerWidth: calc(100% - ${({ theme }) => theme.dimensions.miniNavWidth}px);
--partialWidth: calc(var(--innerWidth) * 0.6);
width: var(--partialWidth);
background: ${({ theme }) => theme.colors.cardAltBackground};
--innerHeight: calc(100% - ${({ theme }) => theme.dimensions.headerHeight}px);
height: calc(var(--innerHeight) * 0.45);
z-index: 10;
overflow: hidden;
display: grid;
grid-template-rows: 1fr 2fr;
${media.tablet`
& {
right: 0;
bottom: 0;
--innerHeight: calc(100% - ${({ theme }) => theme.dimensions.headerHeight}px);
height: calc(var(--innerHeight) * 0.45);
width: 55%;
}
`}
${media.mobile`
& {
position: relative;
right: auto;
bottom: auto;
width: auto;
height: auto;
border-left-width: 0;
z-index: 0;
}
`}
`
const SubmitWrapper = styled.div`
display: flex;
align-items: center;
justify-content: center;
padding: 12px 0;
`