Skip to content

Commit

Permalink
make tasks in task properties widget collapsible
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinBeczak committed Dec 9, 2024
1 parent 141eb3e commit 8817d42
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 36 deletions.
10 changes: 10 additions & 0 deletions src/components/Widgets/TaskPropertiesWidget/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ export default defineMessages({
id: "Widgets.TaskPropertiesWidget.task.label",
defaultMessage: "Task {taskId}",
},

expandAll: {
id: "Widgets.TaskPropertiesWidget.expandAll",
defaultMessage: "Expand All",
},

collapseAll: {
id: "Widgets.TaskPropertiesWidget.collapseAll",
defaultMessage: "Collapse All",
},
})
118 changes: 82 additions & 36 deletions src/components/Widgets/TaskPropertiesWidget/TaskPropertiesWidget.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from 'react'
import React, { useState } from 'react'
import { FormattedMessage } from 'react-intl'
import _get from 'lodash/get'
import _map from 'lodash/map'
Expand All @@ -19,45 +19,91 @@ const descriptor = {
defaultHeight: 6,
}

export default class TaskPropertiesWidget extends Component {
render() {
const taskList = _get(this.props.taskBundle, 'tasks') || [this.props.task]
const propertyLists = _map(taskList, (task) => {
const featurePropertiesList = AsMappableTask(task).osmFeatureProperties(this.props.osmElements)

return (
<div key={task.id} className="mr-mb-6">
<div className="mr-text-yellow">
<FormattedMessage {...messages.taskLabel} values={{taskId: task.id}}/>
</div>
{
featurePropertiesList.map((feature, index) => {
return (
<div key={index}>
<div className="mr-text-yellow mr-ml-2">
{feature.properties?.id || feature.geometry?.type}
</div>
<PropertyList featureProperties={feature.properties} hideHeader
lightMode={false} {...this.props} />
</div>
)
})
}
</div>
)
})
const TaskPropertiesWidget = (props) => {
const taskList = _get(props.taskBundle, 'tasks') || [props.task];
const initialCollapsedState = {};

taskList.forEach((task) => {
const featurePropertiesList = AsMappableTask(task).osmFeatureProperties(props.osmElements);
featurePropertiesList.forEach((_, index) => {
initialCollapsedState[`${task.id}-${index}`] = true;
});
});

const [collapsed, setCollapsed] = useState(initialCollapsedState);
const [allCollapsed, setAllCollapsed] = useState(true);

const toggleCollapseAll = () => {
const newCollapsedState = {};
taskList.forEach((task) => {
const featurePropertiesList = AsMappableTask(task).osmFeatureProperties(props.osmElements);
featurePropertiesList.forEach((_, index) => {
newCollapsedState[`${task.id}-${index}`] = !allCollapsed;
});
});
setCollapsed(newCollapsedState);
setAllCollapsed(!allCollapsed);
}

const toggleCollapse = (index) => {
setCollapsed(prevState => ({
...prevState,
[index]: !prevState[index],
}));
}

const propertyLists = _map(taskList, (task) => {
const featurePropertiesList = AsMappableTask(task).osmFeatureProperties(props.osmElements)

return (
<QuickWidget
{...this.props}
className="task-properties-widget"
widgetTitle={<FormattedMessage {...messages.title} />}
>
{propertyLists}
</QuickWidget>
<div key={task.id} className="mr-mb-4 border-b mr-border-gray-300 pb-2">
{
featurePropertiesList.map((feature, index) => {
const featureIndex = `${task.id}-${index}`;
const isCollapsed = collapsed[featureIndex];
return (
<div key={featureIndex} className="mr-pb-2 mr-border-b mr-bg-gray-100">
<div className="mr-text-yellow mr-cursor-pointer mr-p-1 mr-rounded mr-flex mr-justify-between mr-items-center" onClick={() => toggleCollapse(featureIndex)}>
<div className="mr-flex mr-items-center">
<div className="mr-text-yellow mr-font-bold mr-text-lg">
<FormattedMessage {...messages.taskLabel} values={{taskId: task.id}}/>
</div>
<span className="mr-text-grey-lighter mr-mx-2 mr-mt-1">
{feature.properties?.id || feature.geometry?.type}
</span>
</div>
<div className="mr-cursor-pointer">
{isCollapsed ? '▼' : '▲'}
</div>
</div>
{!isCollapsed && (
<PropertyList featureProperties={feature.properties} hideHeader
lightMode={false} {...props} />
)}
</div>
)
})
}
</div>
)
}
})

return (
<QuickWidget
{...props}
className="task-properties-widget"
widgetTitle={<FormattedMessage {...messages.title} />}
rightHeaderControls={
<div onClick={toggleCollapseAll} className="mr-cursor-pointer mr-button mr-button--small mr-text-sm mr-ml-4">
{allCollapsed ? <FormattedMessage {...messages.expandAll} /> : <FormattedMessage {...messages.collapseAll} />}
</div>
}
>
{propertyLists}
</QuickWidget>
)
}

registerWidgetType(TaskPropertiesWidget, descriptor)

export default TaskPropertiesWidget;

0 comments on commit 8817d42

Please sign in to comment.