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: exports via main button don't include orphans (#287) #288

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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: 6 additions & 0 deletions src/providers/fileManifestState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type FileManifestState = {
isSummaryLoading: boolean;
requestParams?: URLSearchParams;
requestURL?: string;
selectedFormFacetNames: Set<FileFacet["name"]>;
summary?: AzulSummaryResponse;
};

Expand Down Expand Up @@ -283,6 +284,7 @@ function fileManifestReducer(
isEnabled: false,
requestParams: undefined,
requestURL: undefined,
selectedFormFacetNames: new Set(),
};
}
// Fetches file manifest.
Expand Down Expand Up @@ -336,6 +338,8 @@ function fileManifestReducer(
filters,
state.fileSummaryFacetName
);
// Update set of form facet names.
state.selectedFormFacetNames.add(payload.categoryKey);
return {
...state,
fileSummaryFilters,
Expand All @@ -358,6 +362,8 @@ function fileManifestReducer(
filters,
state.fileSummaryFacetName
);
// Update set of form facet names.
state.selectedFormFacetNames.add(payload);
return {
...state,
fileSummaryFilters,
Expand Down
12 changes: 2 additions & 10 deletions src/providers/fileManifestState/constants.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import {
FILE_MANIFEST_TYPE,
FILES_FACETS_STATUS,
} from "../../hooks/useFileManifest/common/entities";
import { FILES_FACETS_STATUS } from "../../hooks/useFileManifest/common/entities";
import { FileManifestState } from "../fileManifestState";

export const ENTITIES_FILE_MANIFEST_TYPES: FILE_MANIFEST_TYPE[] = [
FILE_MANIFEST_TYPE.BULK_DOWNLOAD,
FILE_MANIFEST_TYPE.DOWNLOAD_MANIFEST,
FILE_MANIFEST_TYPE.EXPORT_TO_TERRA,
];

export const FILE_MANIFEST_STATE: FileManifestState = {
fileManifestFormat: undefined,
fileManifestType: undefined,
Expand All @@ -27,5 +18,6 @@ export const FILE_MANIFEST_STATE: FileManifestState = {
isSummaryLoading: false,
requestParams: undefined,
requestURL: undefined,
selectedFormFacetNames: new Set(),
summary: undefined,
};
116 changes: 77 additions & 39 deletions src/providers/fileManifestState/utils.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,104 @@
import { Filters } from "../../common/entities";
import {
FileFacet,
FILES_FACETS_STATUS,
} from "../../hooks/useFileManifest/common/entities";
import { Filters, SelectedFilter } from "../../common/entities";
import { FILES_FACETS_STATUS } from "../../hooks/useFileManifest/common/entities";
import { findFacet } from "../../hooks/useFileManifest/common/utils";
import {
FileManifestState,
UpdateFileManifestPayload,
} from "../fileManifestState";
import { ENTITIES_FILE_MANIFEST_TYPES } from "./constants";

/**
* Checks if all form facets are in a selected state.
* The form facets are considered selected if they are present in the filters.
* @param state - File manifest state.
* @returns true if all form facets are fully selected.
*/
export function areAllFormFacetsSelected(state: FileManifestState): boolean {
const { filters, selectedFormFacetNames } = state;
for (const facetName of [...selectedFormFacetNames]) {
if (isFacetSelected(filters, facetName)) continue;
return false;
}
return true;
}

/**
* Checks whether all form facets have all their terms fully selected.
* - **true**: All form facets have all their terms selected.
* - **false**: At least one form facet has an unselected term.
* @param state - File manifest state.
* @returns true if all form facets have all their terms selected.
*/
export function areAllFormFiltersSelected(state: FileManifestState): boolean {
const { filesFacets, filters, selectedFormFacetNames } = state;
for (const facetName of [...selectedFormFacetNames]) {
const facet = findFacet(filesFacets, facetName);
if (!facet)
throw new Error(`Facet "${facetName}" not found in filesFacets.`); // Facet, should be always present; form is generated from filesFacets.
const filter = findFilter(filters, facetName);
if (!filter) return false; // Form facet has no selected terms.
if (filter.value.length < facet.termCount) return false; // Form facet has unselected terms.
}
return true;
}

/**
* Filters out fully selected form facets from the filters.
* @param state - File manifest state.
* @returns filters.
*/
export function excludeFullySelectedFormFilters(
state: FileManifestState
): Filters {
const filters: Filters = [];
for (const filter of state.filters) {
if (state.selectedFormFacetNames.has(filter.categoryKey)) continue;
filters.push(filter);
}
return filters;
}

/**
* Generates the filters for a request URL based on the file manifest state.
* If all terms in a facet are selected, the corresponding facet is excluded.
* Filters are returned based on the following conditions:
* - **form terms unselected**: No filters are returned.
* - **form terms partially selected**: All filters returned, including form filters.
* - **form terms fully selected**: Filters returned, excluding form filters.
* @param state - File manifest state.
* @returns filters for the request URL.
*/
export function getRequestFilters(
state: FileManifestState
): Filters | undefined {
if (state.filesFacetsStatus !== FILES_FACETS_STATUS.COMPLETED) return;
// Determine if the filters are user-selected.
if (isFiltersUserSelected(state)) {
return state.filters;
}
for (const filter of state.filters) {
const facet = findFacet(state.filesFacets, filter.categoryKey);
if (!facet) return [filter]; // The entity identifier related filter will not have a corresponding term facet.
}
if (state.selectedFormFacetNames.size === 0) return;
if (!areAllFormFacetsSelected(state)) return;
// Form terms are partially selected; return filters.
if (!areAllFormFiltersSelected(state)) return state.filters;
// Form terms are fully selected; return filters excluding form filters.
return excludeFullySelectedFormFilters(state);
}

/**
* Returns true if filter values for each selected facet are partially selected.
* @param filters - Selected filters.
* @param filesFacets - Files facets.
* @returns true if the filters are partially selected.
* Finds selected filter by facet name.
* @param filters - Filters.
* @param facetName - Facet name.
* @returns selected filter.
*/
function isFiltersPartiallySelected(
export function findFilter(
filters: Filters,
filesFacets: FileFacet[]
): boolean {
for (const { categoryKey, value } of filters) {
const facet = findFacet(filesFacets, categoryKey);
if (!facet) continue; // Continue; the entity identifier related filter will not have a corresponding term facet.
if (value.length < facet.termCount) return true;
}
return false;
facetName: string
): SelectedFilter | undefined {
return filters.find(({ categoryKey }) => categoryKey === facetName);
}

/**
* Returns true if the filters are user-selected, when:
* - The file manifest type is not set.
* - The file manifest type is an entity list related type.
* - Filter values for each selected facet are partially selected.
* @param state - File manifest state.
* @returns true if the filters are user-selected.
* Returns true if the facet is selected i.e. present in the filters.
* @param filters - Filters.
* @param facetName - Facet name.
* @returns true if the facet is selected.
*/
export function isFiltersUserSelected(state: FileManifestState): boolean {
if (!state.fileManifestType) return true;
if (ENTITIES_FILE_MANIFEST_TYPES.includes(state.fileManifestType))
return true;
return isFiltersPartiallySelected(state.filters, state.filesFacets);
function isFacetSelected(filters: Filters, facetName: string): boolean {
return filters.some(({ categoryKey }) => categoryKey === facetName);
}

/**
Expand Down
Loading
Loading