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

Exclude fields from My Projects route #4409

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions jsapp/js/projects/customViewStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ class CustomViewStore {
if (savedViewData.filters) {
this.filters = savedViewData.filters;
}
if (savedViewData.order) {
if (savedViewData.order?.direction && savedViewData.order?.fieldName) {
this.order = savedViewData.order;
}
if (savedViewData.fields) {
if (savedViewData.fields && Array.isArray(savedViewData.fields)) {
this.fields = savedViewData.fields;
}
}
Expand Down
14 changes: 12 additions & 2 deletions jsapp/js/projects/myProjectsRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
HOME_VIEW,
HOME_ORDERABLE_FIELDS,
DEFAULT_VISIBLE_FIELDS,
HOME_EXCLUDED_FIELDS,
} from './projectViews/constants';
import ViewSwitcher from './projectViews/viewSwitcher';
import ProjectsTable from 'js/projects/projectsTable/projectsTable';
Expand All @@ -19,7 +20,6 @@ import routeStyles from './myProjectsRoute.module.scss';
import {toJS} from 'mobx';
import {COMMON_QUERIES, ROOT_URL} from 'js/constants';
import ProjectQuickActions from './projectsTable/projectQuickActions';
import mixins from 'js/mixins';
import Dropzone from 'react-dropzone';
import {validFileTypes} from 'js/utils';
import Icon from 'js/components/common/icon';
Expand Down Expand Up @@ -51,6 +51,14 @@ function MyProjectsRoute() {
selectedRows.includes(asset.uid)
);

/** Filters out excluded fields */
const getTableVisibleFields = () => {
const outcome = toJS(customView.fields) || DEFAULT_VISIBLE_FIELDS;
return outcome.filter(
(fieldName) => !HOME_EXCLUDED_FIELDS.includes(fieldName)
);
};

return (
<Dropzone
onDrop={dropImportXLSForms}
Expand All @@ -72,11 +80,13 @@ function MyProjectsRoute() {
<ProjectsFilter
onFiltersChange={customView.setFilters.bind(customView)}
filters={toJS(customView.filters)}
excludedFields={HOME_EXCLUDED_FIELDS}
/>

<ProjectsFieldsSelector
onFieldsChange={customView.setFields.bind(customView)}
selectedFields={toJS(customView.fields)}
excludedFields={HOME_EXCLUDED_FIELDS}
/>

{selectedAssets.length === 1 && (
Expand All @@ -90,7 +100,7 @@ function MyProjectsRoute() {
assets={customView.assets}
isLoading={!customView.isFirstLoadComplete}
highlightedFields={getFilteredFieldsNames()}
visibleFields={toJS(customView.fields) || DEFAULT_VISIBLE_FIELDS}
visibleFields={getTableVisibleFields()}
orderableFields={HOME_ORDERABLE_FIELDS}
order={customView.order}
onChangeOrderRequested={customView.setOrder.bind(customView)}
Expand Down
10 changes: 10 additions & 0 deletions jsapp/js/projects/projectViews/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,13 @@ export const DEFAULT_VISIBLE_FIELDS: ProjectFieldName[] = [
'status',
'submissions',
];

/**
* These are fields not available on the `/api/v2/assets/` endpoint - there is
* no point in displaying them to the user.
*/
export const HOME_EXCLUDED_FIELDS: ProjectFieldName[] = [
'ownerEmail',
'ownerFullName',
'ownerOrganization',
];
33 changes: 22 additions & 11 deletions jsapp/js/projects/projectViews/projectsFieldsSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ interface ProjectsFieldsSelectorProps {
* again through props.
*/
onFieldsChange: (fields: ProjectFieldName[] | undefined) => void;
/** A list of fields that should not be available to user. */
excludedFields?: ProjectFieldName[];
}

export default function ProjectsFieldsSelector(
props: ProjectsFieldsSelectorProps
) {
const getInitialSelectedFields = () => {
let outcome: ProjectFieldName[] = [];
if (!props.selectedFields || props.selectedFields.length === 0) {
return DEFAULT_VISIBLE_FIELDS;
outcome = DEFAULT_VISIBLE_FIELDS;
} else {
return Array.from(props.selectedFields);
outcome = Array.from(props.selectedFields);
}
return outcome.filter(
(fieldName) => !props.excludedFields?.includes(fieldName)
);
};

const [isModalOpen, setIsModalOpen] = useState(false);
Expand Down Expand Up @@ -65,15 +71,20 @@ export default function ProjectsFieldsSelector(
};

const getCheckboxes = (): MultiCheckboxItem[] =>
Object.values(PROJECT_FIELDS).map((field) => {
return {
name: field.name,
label: field.label,
// We ensure "name" field is always selected
checked: selectedFields.includes(field.name) || field.name === 'name',
disabled: field.name === 'name',
};
});
Object.values(PROJECT_FIELDS)
.filter(
(fieldDefinition) =>
!props.excludedFields?.includes(fieldDefinition.name)
)
.map((field) => {
return {
name: field.name,
label: field.label,
// We ensure "name" field is always selected
checked: selectedFields.includes(field.name) || field.name === 'name',
disabled: field.name === 'name',
};
});

return (
<div className={styles.root}>
Expand Down
5 changes: 4 additions & 1 deletion jsapp/js/projects/projectViews/projectsFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import clonedeep from 'lodash.clonedeep';
import Button from 'js/components/common/button';
import KoboModal from 'js/components/modals/koboModal';
import KoboModalHeader from 'js/components/modals/koboModalHeader';
import type {ProjectsFilterDefinition} from './constants';
import type {ProjectFieldName, ProjectsFilterDefinition} from './constants';
import ProjectsFilterEditor from './projectsFilterEditor';
import {removeIncorrectFilters} from './utils';
import styles from './projectsFilter.module.scss';
Expand All @@ -21,6 +21,8 @@ interface ProjectsFilterProps {
* new filters.
*/
onFiltersChange: (filters: ProjectsFilterDefinition[]) => void;
/** A list of fields that should not be available to user. */
excludedFields?: ProjectFieldName[];
}

export default function ProjectsFilter(props: ProjectsFilterProps) {
Expand Down Expand Up @@ -132,6 +134,7 @@ export default function ProjectsFilter(props: ProjectsFilterProps) {
onDelete={() => {
onFilterEditorDelete(filterIndex);
}}
excludedFields={props.excludedFields}
/>
))}

Expand Down
7 changes: 7 additions & 0 deletions jsapp/js/projects/projectViews/projectsFilterEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface ProjectsFilterEditorProps {
/** Called on every change. */
onFilterChange: (filter: ProjectsFilterDefinition) => void;
onDelete: () => void;
/** A list of fields that should not be available to user. */
excludedFields?: ProjectFieldName[];
}

const COUNTRIES = envStore.data.country_choices;
Expand Down Expand Up @@ -67,6 +69,11 @@ export default function ProjectsFilterEditor(props: ProjectsFilterEditorProps) {
.filter(
(filterDefinition) => filterDefinition.availableConditions.length >= 1
)
// We don't want to display excluded fields.
.filter(
(filterDefinition) =>
!props.excludedFields?.includes(filterDefinition.name)
)
.map((filterDefinition) => {
return {label: filterDefinition.label, value: filterDefinition.name};
});
Expand Down