Skip to content

Commit

Permalink
filter area search by service unit
Browse files Browse the repository at this point in the history
  • Loading branch information
NC-jsAhonen committed Apr 10, 2024
1 parent d2432eb commit 1d46695
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
48 changes: 43 additions & 5 deletions src/areaSearch/components/AreaSearchApplicationListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ import type {Attributes, Methods as MethodsType} from '$src/types';
import type {ApiResponse} from '$src/types';
import type {UsersPermissions as UsersPermissionsType} from '$src/usersPermissions/types';
import AreaSearchExportModal from '$src/areaSearch/components/AreaSearchExportModal';
import { getUserActiveServiceUnit } from "../../usersPermissions/selectors";
import type { UserServiceUnit } from "../../usersPermissions/types";

const VisualizationTypes = {
MAP: 'map',
Expand Down Expand Up @@ -106,6 +108,7 @@ type Props = {
lastEditError: any,
change: Function,
selectedSearches: Object,
userActiveServiceUnit: UserServiceUnit
}

type State = {
Expand All @@ -125,6 +128,7 @@ type State = {

class AreaSearchApplicationListPage extends PureComponent<Props, State> {
_isMounted: boolean
_hasFetchedAreaSearches: boolean;

state: State = {
properties: [],
Expand All @@ -139,6 +143,7 @@ class AreaSearchApplicationListPage extends PureComponent<Props, State> {
isEditModalOpen: false,
isExportModalOpen: false,
editModalTargetAreaSearch: null,
userActiveServiceUnit: undefined,
}

static contextTypes = {
Expand Down Expand Up @@ -356,7 +361,7 @@ class AreaSearchApplicationListPage extends PureComponent<Props, State> {
};

search = () => {
const {fetchAreaSearchList, location: {search}} = this.props;
const {fetchAreaSearchList, location: {search}, userActiveServiceUnit} = this.props;
const searchQuery = getUrlParams(search);
const page = searchQuery.page ? Number(searchQuery.page) : 1;

Expand All @@ -365,6 +370,11 @@ class AreaSearchApplicationListPage extends PureComponent<Props, State> {
}

searchQuery.limit = LIST_TABLE_PAGE_SIZE;

if (searchQuery.service_unit === undefined && userActiveServiceUnit) {
searchQuery.service_unit = userActiveServiceUnit.id;
}

delete searchQuery.page;
delete searchQuery.in_bbox;
delete searchQuery.visualization;
Expand All @@ -374,7 +384,7 @@ class AreaSearchApplicationListPage extends PureComponent<Props, State> {
}

searchByBBox = () => {
const {fetchAreaSearchListByBBox, location: {search}} = this.props;
const {fetchAreaSearchListByBBox, location: {search}, userActiveServiceUnit} = this.props;
const searchQuery = getUrlParams(search);
const leaseStates = this.getSearchStates(searchQuery);

Expand All @@ -388,6 +398,10 @@ class AreaSearchApplicationListPage extends PureComponent<Props, State> {
searchQuery.lease_state = leaseStates;
}

if (searchQuery.service_unit === undefined && userActiveServiceUnit) {
searchQuery.service_unit = userActiveServiceUnit.id;
}

searchQuery.limit = 10000;
delete searchQuery.page;
delete searchQuery.visualization;
Expand Down Expand Up @@ -480,11 +494,29 @@ class AreaSearchApplicationListPage extends PureComponent<Props, State> {
}

componentDidUpdate(prevProps) {
const {location: {search: currentSearch}, isEditingAreaSearch, lastEditError} = this.props;
const {location: {search: prevSearch}} = prevProps;
const {location: {search: currentSearch}, isEditingAreaSearch, lastEditError, userActiveServiceUnit} = this.props;
const {location: {search: prevSearch}, userActiveServiceUnit: prevUserActiveServiceUnit} = prevProps;
const {visualizationType} = this.state;
const searchQuery = getUrlParams(currentSearch);

const handleSearch = () => {
this.setSearchFormValues();
this.search();
};

if(userActiveServiceUnit) {

if(!this._hasFetchedAreaSearches) { // No search has been done yet
handleSearch();
this._hasFetchedAreaSearches = true;

} else if(userActiveServiceUnit !== prevUserActiveServiceUnit
&& !currentSearch.includes('service_unit')) {
// Search again after changing user active service unit only if not explicitly setting the service unit filter
handleSearch();
}
}

if ((currentSearch !== prevSearch) ||
(!isEditingAreaSearch && !lastEditError && prevProps.isEditingAreaSearch)) {
this.closeAreaSearchEditModal();
Expand Down Expand Up @@ -514,6 +546,7 @@ class AreaSearchApplicationListPage extends PureComponent<Props, State> {
componentWillUnmount() {
window.removeEventListener('popstate', this.handlePopState);
this._isMounted = false;
this._hasFetchedAreaSearches = false;
}

handlePopState = () => {
Expand Down Expand Up @@ -546,7 +579,7 @@ class AreaSearchApplicationListPage extends PureComponent<Props, State> {
}

setSearchFormValues = () => {
const {location: {search}, initializeForm} = this.props;
const {location: {search}, initializeForm, userActiveServiceUnit} = this.props;
const searchQuery = getUrlParams(search);
const page = searchQuery.page ? Number(searchQuery.page) : 1;
const states = this.getSearchStates(searchQuery);
Expand All @@ -565,6 +598,10 @@ class AreaSearchApplicationListPage extends PureComponent<Props, State> {
delete initialValues.visualization;
delete initialValues.zoom;

if(initialValues.service_unit === undefined && userActiveServiceUnit) {
initialValues.service_unit = userActiveServiceUnit.id;
}

await initializeForm(FormNames.AREA_SEARCH_SEARCH, initialValues);
};

Expand Down Expand Up @@ -799,6 +836,7 @@ export default (flowRight(
isEditingAreaSearch: getIsEditingAreaSearch(state),
lastEditError: getLastAreaSearchEditError(state),
selectedSearches: selector(state, 'selectedSearches'),
userActiveServiceUnit: getUserActiveServiceUnit(state),
};
},
{
Expand Down
26 changes: 26 additions & 0 deletions src/areaSearch/components/search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type State = {
areaSearches: ApiResponse,
areaSearchAttributes: Attributes,
attributes: Attributes,
serviceUnitOptions: Array<Object>,
}

class Search extends Component<Props, State> {
Expand All @@ -64,6 +65,7 @@ class Search extends Component<Props, State> {
areaSearches: null,
areaSearchAttributes: {},
attributes: {},
serviceUnitOptions: []
};

componentDidMount() {
Expand Down Expand Up @@ -148,6 +150,7 @@ class Search extends Component<Props, State> {
if (props.areaSearchAttributes !== state.areaSearchAttributes) {
newState.intendedUseOptions = getFieldOptions(props.areaSearchAttributes, AreaSearchFieldPaths.INTENDED_USE);
newState.lessorOptions = getFieldOptions(props.areaSearchAttributes, AreaSearchFieldPaths.LESSOR);
newState.serviceUnitOptions = getFieldOptions(props.areaSearchAttributes, 'service_unit', true);
}

return !isEmpty(newState) ? newState : null;
Expand All @@ -163,6 +166,7 @@ class Search extends Component<Props, State> {
isBasicSearch,
intendedUseOptions,
lessorOptions,
serviceUnitOptions,
} = this.state;

return (
Expand Down Expand Up @@ -459,6 +463,28 @@ class Search extends Component<Props, State> {
/>
</SearchInputColumn>
</SearchRow>
<SearchRow>
<SearchLabelColumn>
<SearchLabel>Palvelukokonaisuus</SearchLabel>
</SearchLabelColumn>
<SearchInputColumn>
<FormField
autoBlur
disableDirty
fieldAttributes={{
label: 'Palvelukokonaisuus',
type: FieldTypes.CHOICE,
read_only: false,
}}
invisibleLabel
name='service_unit'
overrideValues={{
options: serviceUnitOptions,
}}
/>
</SearchInputColumn>
</SearchRow>

</Column>
</Row>
</Fragment>
Expand Down

0 comments on commit 1d46695

Please sign in to comment.