Skip to content

Commit

Permalink
fetch all datasets for dataviews editor
Browse files Browse the repository at this point in the history
  • Loading branch information
j8seangel committed Oct 17, 2023
1 parent 05b38c5 commit 25c8e38
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
52 changes: 37 additions & 15 deletions apps/fishing-map/features/datasets/datasets.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,25 @@ export const fetchDatasetByIdThunk = createAsyncThunk<
}
})

type FetchDatasetsFromApiParams = {
ids: string[]
existingIds: string[]
signal: AbortSignal
maxDepth?: number
onlyUserDatasets?: boolean
}
const fetchDatasetsFromApi = async (
ids: string[] = [],
existingIds: string[] = [],
signal: AbortSignal,
maxDepth: number = 5
{
ids,
existingIds,
signal,
maxDepth = 5,
onlyUserDatasets = true,
} = {} as FetchDatasetsFromApiParams
) => {
const uniqIds = ids?.length ? ids.filter((id) => !existingIds.includes(id)) : []
const datasetsParams = {
...(uniqIds?.length ? { ids: uniqIds } : { 'logged-user': true }),
...(uniqIds?.length ? { ids: uniqIds } : { 'logged-user': onlyUserDatasets }),
include: 'endpoints',
cache: false,
...DEFAULT_PAGINATION_PARAMS,
Expand All @@ -103,26 +113,32 @@ const fetchDatasetsFromApi = async (
const currentIds = uniq([...existingIds, ...datasets.map((d) => d.id)])
const uniqRelatedDatasetsIds = without(relatedDatasetsIds, ...currentIds)
if (uniqRelatedDatasetsIds.length > 1 && maxDepth > 0) {
const relatedDatasets = await fetchDatasetsFromApi(
uniqRelatedDatasetsIds,
currentIds,
const relatedDatasets = await fetchDatasetsFromApi({
ids: uniqRelatedDatasetsIds,
existingIds: currentIds,
signal,
maxDepth - 1
)
maxDepth: maxDepth - 1,
})
datasets = uniqBy([...datasets, ...relatedDatasets], 'id')
}

return datasets
}

export const fetchDatasetsByIdsThunk = createAsyncThunk(
export const fetchDatasetsByIdsThunk = createAsyncThunk<
Dataset[],
{ ids: string[]; onlyUserDatasets?: boolean },
{
rejectValue: AsyncError
}
>(
'datasets/fetch',
async (ids: string[] = [], { signal, rejectWithValue, getState }) => {
async ({ ids, onlyUserDatasets = true }, { signal, rejectWithValue, getState }) => {
const state = getState() as DatasetsSliceState
const existingIds = selectIds(state) as string[]

try {
const datasets = await fetchDatasetsFromApi(ids, existingIds, signal)
const datasets = await fetchDatasetsFromApi({ ids, existingIds, signal, onlyUserDatasets })
return datasets.map(parsePOCsDatasets)
} catch (e: any) {
console.warn(e)
Expand All @@ -131,8 +147,14 @@ export const fetchDatasetsByIdsThunk = createAsyncThunk(
}
)

export const fetchAllDatasetsThunk = createAsyncThunk('datasets/all', (_, { dispatch }) => {
return dispatch(fetchDatasetsByIdsThunk([]))
export const fetchAllDatasetsThunk = createAsyncThunk<
any,
{ onlyUserDatasets?: boolean } | undefined,
{
rejectValue: AsyncError
}
>('datasets/all', ({ onlyUserDatasets } = {}, { dispatch }) => {
return dispatch(fetchDatasetsByIdsThunk({ ids: [], onlyUserDatasets }))
})

export type CreateDataset = { dataset: Partial<Dataset>; file: File; createAsPublic: boolean }
Expand Down
6 changes: 3 additions & 3 deletions apps/fishing-map/features/editor/DataviewEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const DataviewEditor = ({ editDataview, onCancelClick }: DataviewEditorProps) =>

useEffect(() => {
if (!isEditingDataview) {
dispatch(fetchAllDatasetsThunk())
dispatch(fetchAllDatasetsThunk({ onlyUserDatasets: false }))
}
}, [dispatch, isEditingDataview])

Expand Down Expand Up @@ -295,8 +295,8 @@ const DataviewEditor = ({ editDataview, onCancelClick }: DataviewEditorProps) =>
options={temporalResolutionOptions}
containerClassName={styles.input2Columns}
direction="top"
selectedOption={temporalResolutionOptions.find(({ id }) =>
dataview.config?.intervals?.includes(id)
selectedOption={temporalResolutionOptions.find(
({ id }) => dataview.config?.intervals?.includes(id)
)}
onSelect={(selected) => {
onDataviewConfigChange({ interval: selected.id })
Expand Down
2 changes: 1 addition & 1 deletion apps/fishing-map/features/editor/WorkspaceEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const WorkspaceEditor = ({ onEditClick }: WorkspaceEditorProps) => {
const dataviewInstance = getDataviewInstanceFromDataview(dataview)
const datasets = dataview.datasetsConfig?.map(({ datasetId }) => datasetId)
if (datasets && datasets?.length) {
const action = await dispatch(fetchDatasetsByIdsThunk(datasets))
const action = await dispatch(fetchDatasetsByIdsThunk({ ids: datasets }))
if (!fetchDatasetsByIdsThunk.fulfilled.match(action)) {
setError((action.payload as AsyncError).message)
}
Expand Down
2 changes: 1 addition & 1 deletion apps/fishing-map/features/workspace/workspace.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const fetchWorkspaceThunk = createAsyncThunk(
...(urlDataviewInstances || []),
]
const datasetsIds = getDatasetsInDataviews(dataviews, dataviewInstances, guestUser)
const fetchDatasetsAction: any = dispatch(fetchDatasetsByIdsThunk(datasetsIds))
const fetchDatasetsAction: any = dispatch(fetchDatasetsByIdsThunk({ ids: datasetsIds }))
signal.addEventListener('abort', fetchDatasetsAction.abort)
const { error, payload } = await fetchDatasetsAction
datasets = payload as Dataset[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function WorkspaceWizard() {
if (payload) {
const datasetsIds = getDatasetsInDataviews(payload as Dataview[])
if (datasetsIds?.length) {
dispatch(fetchDatasetsByIdsThunk(datasetsIds))
dispatch(fetchDatasetsByIdsThunk({ ids: datasetsIds }))
}
}
}
Expand Down

0 comments on commit 25c8e38

Please sign in to comment.