Skip to content

Commit

Permalink
Merge pull request #2098 from stakwork/feature/add-latest-endpoint
Browse files Browse the repository at this point in the history
feat: added latest endpoint for initial load
  • Loading branch information
Rassl authored Sep 3, 2024
2 parents c81e6f7 + 2fe38c5 commit 3b15b70
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 29 deletions.
6 changes: 2 additions & 4 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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'])
})
2 changes: 1 addition & 1 deletion src/components/App/SideBar/FilterSearch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
Expand Down
8 changes: 5 additions & 3 deletions src/network/fetchGraphData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import { getLSat } from '~/utils/getLSat'
export const fetchGraphData = async (
setBudget: (value: number | null) => void,
params: Record<string, string>,
isLatest = false,
signal: AbortSignal,
setAbortRequests: (status: boolean) => void,
): Promise<FetchDataResponse> => fetchNodes(setBudget, params, signal, setAbortRequests)
): Promise<FetchDataResponse> => fetchNodes(setBudget, params, isLatest, signal, setAbortRequests)

// Consolidated function to handle different fetch scenarios
const fetchNodes = async (
setBudget: (value: number | null) => void,
params: Record<string, string>,
isLatest = false,
signal: AbortSignal,
setAbortRequests: (status: boolean) => void,
): Promise<FetchDataResponse> => {
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<FetchDataResponse> => {
const lsatToken = await getLSat()
Expand All @@ -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
Expand Down
32 changes: 13 additions & 19 deletions src/stores/useDataStore/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -138,7 +137,9 @@ export const useDataStore = create<DataStore>()(
...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) }
Expand Down Expand Up @@ -169,18 +170,20 @@ export const useDataStore = create<DataStore>()(

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
Expand Down Expand Up @@ -264,22 +267,13 @@ export const useDataStore = create<DataStore>()(

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 }),
Expand Down
4 changes: 2 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b15b70

Please sign in to comment.