Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added latest endpoint for initial load #2098

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
...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 @@

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 @@ -240,7 +243,7 @@
sidebarFilterCounts,
})
} catch (error) {
console.log(error)

Check warning on line 246 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement

Check warning on line 246 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 246 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run

Unexpected console statement

if (error !== 'abort') {
set({ isLoadingNew: false, isFetching: false })
Expand All @@ -264,22 +267,13 @@

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 All @@ -293,7 +287,7 @@
setHideNodeDetails: (hideNodeDetails) => set({ hideNodeDetails }),
setSeedQuestions: (questions) => set({ seedQuestions: questions }),
updateNode: (updatedNode) => {
console.log(updatedNode)

Check warning on line 290 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement

Check warning on line 290 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 290 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run

Unexpected console statement
},
addNewNode: (data) => {
const { dataInitial: existingData } = get()
Expand Down Expand Up @@ -337,7 +331,7 @@
})
},
removeNode: (id) => {
console.log(id)

Check warning on line 334 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement

Check warning on line 334 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 334 in src/stores/useDataStore/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run

Unexpected console statement
},
setAbortRequests: (abortRequest) => set({ abortRequest }),
})),
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
Loading