Skip to content

Commit 555961f

Browse files
authored
Added bulk get snippet routes (#273)
1 parent ff1e216 commit 555961f

File tree

5 files changed

+51
-47
lines changed

5 files changed

+51
-47
lines changed

.changeset/green-apes-eat.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'mexit-webapp': patch
3+
---
4+
5+
Added bulk snippet get endpoint

apps/webapp/src/Hooks/API/useNodeAPI.ts

+23-22
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { client } from '@workduck-io/dwindle'
22

33
import {
44
apiURLs,
5+
batchArray,
56
defaultContent,
67
DEFAULT_NAMESPACE,
78
extractMetadata,
89
getTagsFromContent,
910
GET_REQUEST_MINIMUM_GAP,
10-
iLinksToUpdate,
1111
mog,
1212
NodeEditorContent,
1313
removeNulls
@@ -26,7 +26,6 @@ import { useInternalLinks } from '../useInternalLinks'
2626
import { useLastOpened } from '../useLastOpened'
2727
import { useLinks } from '../useLinks'
2828
import { useNodes } from '../useNodes'
29-
import { useSearch } from '../useSearch'
3029
import { useSnippets } from '../useSnippets'
3130
import { useUpdater } from '../useUpdater'
3231
import { useAPIHeaders } from './useAPIHeaders'
@@ -376,33 +375,35 @@ export const useApi = () => {
376375
return newSnippets
377376
})
378377
.then(async (newSnippets) => {
379-
const ids = newSnippets?.map((item) => item.snippetID)
380-
mog('NewSnippets', { newSnippets, ids })
381-
382-
if (ids && ids.length > 0) {
383-
const res = await runBatchWorker(WorkerRequestType.GET_SNIPPETS, 6, ids)
384-
const requestData = { time: Date.now(), method: 'GET' }
385-
386-
res.fulfilled.forEach(async (snippet) => {
387-
setRequest(apiURLs.snippet.getSnippetById(snippet.id), {
388-
...requestData,
389-
url: apiURLs.snippet.getSnippetById(snippet.id)
378+
const toUpdateSnippets = newSnippets?.map((item) => item.snippetID)
379+
mog('NewSnippets', { newSnippets, toUpdateSnippets })
380+
if (toUpdateSnippets && toUpdateSnippets.length > 0) {
381+
const ids = batchArray(toUpdateSnippets, 10).map((id: string[]) => id.join(','))
382+
if (ids && ids.length > 0) {
383+
const res = await runBatchWorker(WorkerRequestType.GET_SNIPPETS, 6, ids)
384+
const requestData = { time: Date.now(), method: 'GET' }
385+
386+
res.fulfilled.forEach(async (snippets) => {
387+
setRequest(apiURLs.snippet.bulkGet, {
388+
...requestData,
389+
url: apiURLs.snippet.bulkGet
390+
})
391+
392+
if (snippets) {
393+
snippets.forEach((snippet) => updateSnippet(snippet))
394+
}
390395
})
391396

392-
if (snippet) {
393-
updateSnippet(snippet)
394-
}
395-
})
396-
397-
mog('RunBatchWorkerSnippetsRes', { res, ids })
397+
mog('RunBatchWorkerSnippetsRes', { res, ids })
398+
}
398399
}
399400
})
400401

401402
return data
402403
}
403404

404-
const getSnippetById = async (id: string) => {
405-
const url = apiURLs.snippet.getSnippetById(id)
405+
const getById = async (id: string) => {
406+
const url = apiURLs.snippet.getById(id)
406407

407408
const data = await client
408409
.get(url, {
@@ -467,7 +468,7 @@ export const useApi = () => {
467468
appendToNode,
468469
saveSnippetAPI,
469470
getAllSnippetsByWorkspace,
470-
getSnippetById,
471+
getById,
471472
refactorHierarchy,
472473
deleteAllVersionOfSnippet
473474
}

apps/webapp/src/Views/Snippets.tsx

+17-17
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ import { Button, IconButton, Infobox } from '@workduck-io/mex-components'
1212

1313
import { apiURLs, convertContentToRawText, DRAFT_NODE, generateSnippetId, GenericSearchResult, mog } from '@mexit/core'
1414
import {
15-
ItemTag,
16-
MainHeader,
17-
Result,
18-
ResultDesc,
19-
ResultMain,
20-
ResultRow,
21-
ResultTitle,
22-
SearchPreviewWrapper,
23-
SnippetCommand,
24-
SnippetHeader,
25-
SnippetHelp,
26-
SnippetsSearchContainer,
27-
SplitSearchPreviewWrapper,
28-
Title,
29-
View
15+
ItemTag,
16+
MainHeader,
17+
Result,
18+
ResultDesc,
19+
ResultMain,
20+
ResultRow,
21+
ResultTitle,
22+
SearchPreviewWrapper,
23+
SnippetCommand,
24+
SnippetHeader,
25+
SnippetHelp,
26+
SnippetsSearchContainer,
27+
SplitSearchPreviewWrapper,
28+
Title,
29+
View
3030
} from '@mexit/shared'
3131

3232
import Plateless from '../Components/Editor/Plateless'
@@ -269,9 +269,9 @@ const Snippets = () => {
269269

270270
res.fulfilled.forEach((snippet) => {
271271
if (snippet) {
272-
setRequest(apiURLs.snippet.getSnippetById(snippet.id), {
272+
setRequest(apiURLs.snippet.getById(snippet.id), {
273273
...requestData,
274-
url: apiURLs.snippet.getSnippetById(snippet.id)
274+
url: apiURLs.snippet.getById(snippet.id)
275275
})
276276
updateSnippet(snippet)
277277
}

apps/webapp/src/Workers/requests.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@ const getMultipleNodeAPI = async (nodeids: string) => {
4141
})
4242
}
4343

44-
const getSnippetAPI = async (id: string) => {
45-
const url = apiURLs.snippet.getSnippetById(id)
46-
47-
return client.get(url).then((d) => {
48-
return d.data
49-
})
44+
const getMultipleSnippetAPI = async (ids: string) => {
45+
const url = apiURLs.snippet.bulkGet
46+
return client.post(url, { ids: ids.split(',') }).then((d) => d.data)
5047
}
5148

5249
const runBatchWorker = async (requestType: WorkerRequestType, batchSize = 6, args: string[]) => {
@@ -64,7 +61,7 @@ const runBatchWorker = async (requestType: WorkerRequestType, batchSize = 6, arg
6461
}
6562

6663
case WorkerRequestType.GET_SNIPPETS: {
67-
args.forEach((i) => requestsToMake.push(getSnippetAPI(i)))
64+
args.forEach((i) => requestsToMake.push(getMultipleSnippetAPI(i)))
6865
break
6966
}
7067
}

libs/core/src/Utils/routes.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ export const apiURLs = {
8888
snippet: {
8989
create: BASE_URLS.snippet,
9090
getAllSnippetsByWorkspace: `${BASE_URLS.snippet}/all`,
91-
getSnippetById: (uid: string) => `${BASE_URLS.snippet}/${uid}`,
91+
getById: (uid: string) => `${BASE_URLS.snippet}/${uid}`,
92+
bulkGet: `${BASE_URLS.snippet}/bulk`,
9293
deleteAllVersionsOfSnippet: (uid: string) => `${BASE_URLS.snippet}/${uid}/all`,
9394
deleteSpecificVersionOfSnippet: (uid: string, version?: number) => {
9495
let baseURL = `${BASE_URLS.snippet}/${uid}`

0 commit comments

Comments
 (0)