/
rest-method.tsx
134 lines (126 loc) · 3.88 KB
/
rest-method.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
import React, { useState } from 'react';
import { Comment, Tooltip, Avatar, Card } from 'antd';
import { MethodParameters } from '../method-parameters';
import { MethodMetadata } from '../../rest';
import { ResponseValues } from '../response-values';
import { CustomParameters } from '../custom-parameters';
import { PathParameters } from '../path-parameters';
export const RestMethod = ({
author,
authorPicture,
description,
name,
method,
parameters,
useMethod,
}: MethodMetadata & { useMethod: any }) => {
const [showParameters, setShowParameters] = useState(false);
const [showCustomParameters, setShowCustomParameters] = useState(false);
const [showPathParameters, setShowPathParameters] = useState(false);
const [customParameters, setCustomParameters] = useState(() => {
const customParameterObject = {};
// @ts-ignore
parameters.forEach(
(parameter) =>
(customParameterObject[parameter['name']] = parameter['default'])
);
return customParameterObject;
});
const [pathParameters, setPathParameters] = useState(() => {
const customParameterObject = {};
const cleanPath = name.split('?')
const pathParametersList = cleanPath[0].split(':');
pathParametersList.shift();
pathParametersList.forEach((pathParameter) => {
const pathParameterClean = pathParameter.split('/');
//@ts-ignore
customParameterObject[pathParameterClean[0]] = '';
});
if (cleanPath.length>1) {
const pathParameters = cleanPath[1].split('&')
pathParameters.forEach((pathParameter) => {
const parameter = pathParameter.split(':')[1]
// @ts-ignore
customParameterObject[parameter] = '';
})
}
return customParameterObject;
});
const { getData, data } = useMethod({ pathParameters, body: customParameters });
const handleCall = async () => {
const dt = await getData();
console.log(dt);
};
const onValueChange = (key: string, value: string) => {
const parameter = parameters.filter(parameter => parameter['name'] = key)
setCustomParameters({ ...customParameters, [key]: parameter[0].dataType == 'string' ? value : Number(value)});
};
const onValuePathChange = (key: string, value: string) => {
setPathParameters({ ...pathParameters, [key]: value });
};
const actions = [
<span
key="comment-basic-reply-to"
onClick={() => setShowParameters(!showParameters)}
>
Toggle parameters
</span>,
<span key="comment-basic-reply-to" onClick={handleCall}>
Run
</span>,
<span
key="comment-basic-reply-to"
onClick={() => setShowCustomParameters(!showCustomParameters)}
>
Set custom parameters
</span>,
<span
key="comment-basic-reply-to"
onClick={() => setShowPathParameters(!showPathParameters)}
>
Set path parameters
</span>,
];
return (
<Card style={{ margin: '8px 0' }}>
<Comment
actions={actions}
author={<a>{name}</a>}
avatar={
<Tooltip title={'Author Name'}>
<Avatar
src={
authorPicture
? authorPicture
: 'https://joeschmoe.io/api/v1/random'
}
alt={author}
/>
</Tooltip>
}
content={<p>{description}</p>}
datetime={
<Tooltip title={'test'}>
<span>{method}</span>
</Tooltip>
}
/>
{/* @ts-ignore */}
{showParameters && <MethodParameters data={parameters} />}
{data && <ResponseValues data={data} />}
{/* @ts-ignore */}
{showCustomParameters && (
<CustomParameters
data={customParameters}
onValueChange={onValueChange}
/>
)}
{showPathParameters && (
<PathParameters
data={pathParameters}
onValueChange={onValuePathChange}
/>
)}
</Card>
);
};