From 399baf09dcf8804debc12bc01ff8f25d3f17d6a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B0=D1=81=D1=83=D0=BB?= Date: Mon, 2 Sep 2024 14:12:58 +0300 Subject: [PATCH 1/3] feat: added latest endpoint for initial load --- src/network/fetchGraphData/index.ts | 8 +++++--- src/stores/useDataStore/index.ts | 32 ++++++++++++----------------- src/types/index.ts | 4 ++-- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/network/fetchGraphData/index.ts b/src/network/fetchGraphData/index.ts index 268c54e5d..d87d1c7a2 100644 --- a/src/network/fetchGraphData/index.ts +++ b/src/network/fetchGraphData/index.ts @@ -8,19 +8,21 @@ import { getLSat } from '~/utils/getLSat' export const fetchGraphData = async ( setBudget: (value: number | null) => void, params: Record, + isLatest = false, signal: AbortSignal, setAbortRequests: (status: boolean) => void, -): Promise => fetchNodes(setBudget, params, signal, setAbortRequests) +): Promise => fetchNodes(setBudget, params, isLatest, signal, setAbortRequests) // Consolidated function to handle different fetch scenarios const fetchNodes = async ( setBudget: (value: number | null) => void, params: Record, + isLatest = false, signal: AbortSignal, setAbortRequests: (status: boolean) => void, ): Promise => { const args = new URLSearchParams(params).toString() - const url = `/prediction/graph/search?${args}` + const url = isLatest ? `/prediction/graph/search/latest?` : `/prediction/graph/search?${args}` const fetchWithLSAT = async (): Promise => { const lsatToken = await getLSat() @@ -34,7 +36,7 @@ const fetchNodes = async ( if (error.status === 402) { await payLsat(setBudget) - return fetchNodes(setBudget, params, signal, setAbortRequests) + return fetchNodes(setBudget, params, isLatest, signal, setAbortRequests) } throw error diff --git a/src/stores/useDataStore/index.ts b/src/stores/useDataStore/index.ts index b1b81fb7e..155222346 100644 --- a/src/stores/useDataStore/index.ts +++ b/src/stores/useDataStore/index.ts @@ -1,6 +1,7 @@ // @ts-nocheck // @ts-ignore +import { isEqual } from 'lodash' import { create } from 'zustand' import { devtools } from 'zustand/middleware' import { fetchGraphData } from '~/network/fetchGraphData' @@ -30,8 +31,6 @@ export type DataStore = { categoryFilter: NodeType | null dataInitial: { nodes: NodeExtended[]; links: Link[] } | null dataNew: { nodes: NodeExtended[]; links: Link[] } | null - currentPage: number - itemsPerPage: number filters: FilterParams isFetching: boolean isLoadingNew: boolean @@ -106,8 +105,8 @@ const defaultData: Omit< currentPage: 0, itemsPerPage: 300, filters: { - skip: '0', - limit: '300', + skip: 0, + limit: 300, depth: '2', sort_by: 'date_added_to_graph', include_properties: 'true', @@ -138,7 +137,9 @@ export const useDataStore = create()( ...defaultData, fetchData: async (setBudget, setAbortRequests, AISearchQuery = '') => { - const { currentPage, itemsPerPage, dataInitial: existingData, filters } = get() + const { dataInitial: existingData, filters } = get() + const currentPage = filters.skip + const itemsPerPage = filters.limit const { currentSearch } = useAppStore.getState() const { setAiSummaryAnswer, setNewLoading, aiRefId } = useAiSummaryStore.getState() let ai = { ai_summary: String(!!AISearchQuery) } @@ -169,18 +170,20 @@ export const useDataStore = create()( const word = AISearchQuery || currentSearch + const isLatest = isEqual(filters, defaultData.filters) && !word + const updatedParams = { ...withoutNodeType, ...ai, skip: currentPage === 0 ? String(currentPage * itemsPerPage) : String(currentPage * itemsPerPage + 1), - limit: word ? 25 : String(itemsPerPage), + limit: word ? '25' : String(itemsPerPage), ...(filterNodeTypes.length > 0 ? { node_type: JSON.stringify(filterNodeTypes) } : {}), ...(word ? { word } : {}), ...(aiRefId && AISearchQuery ? { previous_search_ref_id: aiRefId } : {}), } try { - const data = await fetchGraphData(setBudget, updatedParams, signal, setAbortRequests) + const data = await fetchGraphData(setBudget, updatedParams, isLatest, signal, setAbortRequests) if (!data?.nodes) { return @@ -264,22 +267,13 @@ export const useDataStore = create()( setPage: (page: number) => set({ currentPage: page }), nextPage: () => { - const { currentPage, fetchData } = get() + const { filters, fetchData } = get() - set({ currentPage: currentPage + 1 }) + set({ filters: { ...filters, skip: filters.skip + 1 } }) fetchData() }, - prevPage: () => { - const { currentPage, fetchData } = get() - - if (currentPage > 0) { - set({ currentPage: currentPage - 1 }) - fetchData() - } - }, resetDataNew: () => null, - setFilters: (filters: FilterParams) => - set((state) => ({ filters: { ...state.filters, ...filters, page: 0 }, currentPage: 0 })), + setFilters: (filters: FilterParams) => set((state) => ({ filters: { ...state.filters, ...filters, page: 0 } })), setSidebarFilterCounts: (sidebarFilterCounts) => set({ sidebarFilterCounts }), setTrendingTopics: (trendingTopics) => set({ trendingTopics }), setStats: (stats) => set({ stats }), diff --git a/src/types/index.ts b/src/types/index.ts index d72088404..5ce944daa 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -12,8 +12,8 @@ export type FetchDataResponse = { } export type FilterParams = { - skip: string - limit: string + skip: number + limit: number depth: string sort_by: string top_node_count: string From a3226139be3f3041ca1bdce2673a20bf649e760d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B0=D1=81=D1=83=D0=BB?= Date: Mon, 2 Sep 2024 14:17:55 +0300 Subject: [PATCH 2/3] feat: fix build error --- src/components/App/SideBar/FilterSearch/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/App/SideBar/FilterSearch/index.tsx b/src/components/App/SideBar/FilterSearch/index.tsx index 291ffdcbd..1ffb58f03 100644 --- a/src/components/App/SideBar/FilterSearch/index.tsx +++ b/src/components/App/SideBar/FilterSearch/index.tsx @@ -44,7 +44,7 @@ export const FilterSearch = ({ showAllSchemas, setShowAllSchemas, schemaAll, anc const handleFiltersApply = async () => { setFilters({ node_type: selectedTypes, - limit: maxResults.toString(), + limit: maxResults, depth: hops.toString(), top_node_count: sourceNodes.toString(), }) From 2fe38c520ebc496e70312d7141985781c3b3afe8 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Tue, 3 Sep 2024 20:27:54 +0100 Subject: [PATCH 3/3] fix: changed latest endpoint --- cypress/support/commands.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index 842a681a4..1613e3146 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -34,7 +34,7 @@ import '@testing-library/cypress/add-commands' Cypress.Commands.add('initialSetup', (username, budget) => { cy.intercept({ method: 'GET', - url: 'http://localhost:8444/api/prediction/graph/search*', + url: 'http://localhost:8444/api/prediction/graph/search/latest*', }).as('loadLatest') cy.intercept({ @@ -62,8 +62,6 @@ Cypress.Commands.add('initialSetup', (username, budget) => { }, }) - cy.wait(20000) - cy.wait(['@loadLatest']) cy.get('[data-testid="explore-graph-btn"]', { timeout: 30000 }).should('be.visible').click() - cy.wait(['@loadAbout', '@loadStats', '@getTrends']) + cy.wait(['@loadAbout', '@loadStats', '@getTrends', '@loadLatest']) })