Esteves Enzo commited on
Commit
8f469b4
1 Parent(s): 62b480a

format body to create sub object if needed

Browse files
components/editor/main/hooks/useRequest.ts CHANGED
@@ -28,8 +28,24 @@ export const useRequest = (method: "post" | "put" | "patch" | "delete" | "get",
28
 
29
  const needBody = ["post", "put", "patch"].includes(method);
30
 
31
- axios[method](url.pathname, needBody ? body : {
32
- data: method === "delete" ? body : undefined,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  params: url.searchParams
34
  })
35
  .then((res: any) => {
 
28
 
29
  const needBody = ["post", "put", "patch"].includes(method);
30
 
31
+ const formattedBody = Object.entries(body as any).reduce((acc: any, [key, value]) => {
32
+ if (key.includes(".")) {
33
+ const [first, ...rest] = key.split(".");
34
+ acc[first] = {
35
+ ...acc[first],
36
+ [rest.join(".")]: value
37
+ }
38
+ } else {
39
+ acc[key] = value;
40
+ }
41
+ return acc;
42
+ }
43
+ , {});
44
+
45
+ console.log(formattedBody);
46
+
47
+ axios[method](url.pathname, needBody ? formattedBody : {
48
+ data: method === "delete" ? formattedBody : undefined,
49
  params: url.searchParams
50
  })
51
  .then((res: any) => {
components/editor/sidebar.tsx CHANGED
@@ -45,11 +45,7 @@ export const EditorSidebar = ({
45
  collections.includes(collection.key),
46
  }
47
  )}
48
- onClick={() =>
49
- collection.wip
50
- ? () => {}
51
- : handleSetActiveCollection(collection.key)
52
- }
53
  >
54
  <p className="font-semibold uppercase text-xs flex items-center justify-between">
55
  {collection.key} API
 
45
  collections.includes(collection.key),
46
  }
47
  )}
48
+ onClick={() => handleSetActiveCollection(collection.key)}
 
 
 
 
49
  >
50
  <p className="font-semibold uppercase text-xs flex items-center justify-between">
51
  {collection.key} API
components/input/input.tsx CHANGED
@@ -1,4 +1,5 @@
1
- import React, { ChangeEvent } from "react";
 
2
 
3
  interface Props {
4
  value: string;
@@ -10,22 +11,26 @@ interface Props {
10
  }
11
 
12
  export const TextInput: React.FC<Props> = ({
13
- value,
14
  onChange,
15
  placeholder,
16
  subLabel,
17
  onlyAlphaNumeric = true,
18
  label,
19
  }) => {
 
 
20
  const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
21
  const newValue = event.target.value;
22
  // Only allow numbers or strings
23
  if (onlyAlphaNumeric && /^[0-9a-zA-Z]*$/.test(newValue)) {
24
- return onChange(newValue);
25
  }
26
- onChange(newValue);
27
  };
28
 
 
 
29
  return (
30
  <div className="w-full relative grid grid-cols-1 gap-2.5">
31
  <label className="text-slate-400 text-sm font-medium capitalize">
 
1
+ import React, { ChangeEvent, useState } from "react";
2
+ import { useUpdateEffect } from "react-use";
3
 
4
  interface Props {
5
  value: string;
 
11
  }
12
 
13
  export const TextInput: React.FC<Props> = ({
14
+ value: initialValue,
15
  onChange,
16
  placeholder,
17
  subLabel,
18
  onlyAlphaNumeric = true,
19
  label,
20
  }) => {
21
+ const [value, setValue] = useState(initialValue);
22
+
23
  const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
24
  const newValue = event.target.value;
25
  // Only allow numbers or strings
26
  if (onlyAlphaNumeric && /^[0-9a-zA-Z]*$/.test(newValue)) {
27
+ return setValue(newValue);
28
  }
29
+ setValue(newValue);
30
  };
31
 
32
+ useUpdateEffect(() => onChange(value), [value]);
33
+
34
  return (
35
  <div className="w-full relative grid grid-cols-1 gap-2.5">
36
  <label className="text-slate-400 text-sm font-medium capitalize">
utils/datas/api_collections.ts CHANGED
@@ -78,7 +78,6 @@ export const API_COLLECTIONS: Array<ApiCollection> = [{
78
  body: [
79
  {
80
  label: "Type of repo (dataset or space; model by default)",
81
- defaultValue: "model",
82
  key: "type",
83
  required: true,
84
  },
@@ -108,7 +107,6 @@ export const API_COLLECTIONS: Array<ApiCollection> = [{
108
  path: '/api/repos/delete',
109
  body: [{
110
  label: "Type of repo (dataset or space; model by default)",
111
- defaultValue: "model",
112
  key: "type",
113
  required: true,
114
  },
@@ -146,10 +144,9 @@ export const API_COLLECTIONS: Array<ApiCollection> = [{
146
  },
147
  {
148
  key: 'collection',
149
- wip: true,
150
  endpoints: [{
151
  method: 'POST',
152
- path: '/api/collections/create',
153
  body: [{
154
  label: "Title of collection",
155
  key: "title",
@@ -165,7 +162,6 @@ export const API_COLLECTIONS: Array<ApiCollection> = [{
165
  }, {
166
  label: "Type",
167
  key: "item.type",
168
- defaultValue: "model",
169
  required: true,
170
  }, {
171
  label: "RepoId/PaperId",
@@ -200,7 +196,6 @@ export const API_COLLECTIONS: Array<ApiCollection> = [{
200
  label: "Theme",
201
  required: true,
202
  key: "theme",
203
- defaultValue: "green",
204
  }, {
205
  label: "Whether the repo is private",
206
  required: true,
@@ -216,7 +211,6 @@ export const API_COLLECTIONS: Array<ApiCollection> = [{
216
  body: [{
217
  label: "Type",
218
  key: "item.type",
219
- defaultValue: "model",
220
  required: true,
221
  }, {
222
  label: "RepoId/PaperId",
@@ -225,7 +219,6 @@ export const API_COLLECTIONS: Array<ApiCollection> = [{
225
  }, {
226
  label: "Note",
227
  key: "note",
228
- defaultValue: "Here is the model I trained on ...",
229
  required: true,
230
  }]
231
  }, {
@@ -239,7 +232,6 @@ export const API_COLLECTIONS: Array<ApiCollection> = [{
239
  }, {
240
  label: "Note",
241
  key: "note",
242
- defaultValue: "Here is the model I trained on ...",
243
  required: true,
244
  }]
245
  }, {
 
78
  body: [
79
  {
80
  label: "Type of repo (dataset or space; model by default)",
 
81
  key: "type",
82
  required: true,
83
  },
 
107
  path: '/api/repos/delete',
108
  body: [{
109
  label: "Type of repo (dataset or space; model by default)",
 
110
  key: "type",
111
  required: true,
112
  },
 
144
  },
145
  {
146
  key: 'collection',
 
147
  endpoints: [{
148
  method: 'POST',
149
+ path: '/api/collections',
150
  body: [{
151
  label: "Title of collection",
152
  key: "title",
 
162
  }, {
163
  label: "Type",
164
  key: "item.type",
 
165
  required: true,
166
  }, {
167
  label: "RepoId/PaperId",
 
196
  label: "Theme",
197
  required: true,
198
  key: "theme",
 
199
  }, {
200
  label: "Whether the repo is private",
201
  required: true,
 
211
  body: [{
212
  label: "Type",
213
  key: "item.type",
 
214
  required: true,
215
  }, {
216
  label: "RepoId/PaperId",
 
219
  }, {
220
  label: "Note",
221
  key: "note",
 
222
  required: true,
223
  }]
224
  }, {
 
232
  }, {
233
  label: "Note",
234
  key: "note",
 
235
  required: true,
236
  }]
237
  }, {