Skip to content

Commit

Permalink
Updates (#323)
Browse files Browse the repository at this point in the history
* Fix widget error when geometries is null

* Fix project error when the indicator is deleted

* Fix widget group and add indicator layer to use override styles

* Reuse permissions on form when form is failed

* Bump version
  • Loading branch information
meomancer authored Nov 28, 2024
1 parent 054d627 commit cdfb81e
Show file tree
Hide file tree
Showing 21 changed files with 106 additions and 47 deletions.
2 changes: 1 addition & 1 deletion django_project/_commit_hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
158730d76e51290ce01ac8d3c45d299d961bbae2
e4cef959b65e8c7832b1eaf412a568de153b67b5
2 changes: 1 addition & 1 deletion django_project/_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.1
2.0.2
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ export default function WidgetEditor(
})
const indicatorLayerList = indicatorLayersLikeIndicator(indicatorLayers).map(function (indicator) {
return [indicator.id, indicator.name]
})
}).concat(
indicatorLayers.filter(layer => layer.indicators.length === 1).map(function (indicator) {
return [indicator.id, indicator.name]
})
)


let selectedData = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function Index(
* @returns {JSX.Element}
*/
function getValue() {
if (data !== null) {
if (data !== null && geometries) {
switch (operation) {
case DEFINITION.WidgetOperation.SUM:
let maxValue = 0;
Expand Down
89 changes: 51 additions & 38 deletions django_project/frontend/src/components/Widget/Summary/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import SummaryWidget from "./SummaryWidget"
import SummaryGroupWidget from "./SummaryGroupWidget"
import {
dynamicLayerIndicatorList,
fetchDynamicLayerData
fetchDynamicLayerData,
isIndicatorLayerLikeIndicator
} from "../../../utils/indicatorLayer";
import { Session } from "../../../utils/Sessions";
import { UpdateStyleData } from "../../../utils/indicatorData";

/**
* Base widget that handler widget rendering.
Expand Down Expand Up @@ -69,6 +71,43 @@ export default function SummaryWidgetView({ idx, data }) {
}
}, [indicatorLayerData])

const fetchIndicatorData = async (indicator, params, session, config) => {
if (indicator) {
await fetchingData(
indicator.url, params, {}, function (response, error) {
let newState = {
fetching: false,
fetched: true,
receivedAt: Date.now(),
data: null,
error: null
};

if (error) {
newState.error = error;
} else {
newState.data = UpdateStyleData(response, config.override_style ? config : indicator);
}
if (!session.isValid) {
return
}
setLayerData(newState);
}
)
} else {
if (!session.isValid) {
return
}
setLayerData({
fetching: false,
fetched: true,
receivedAt: Date.now(),
data: null,
error: 'Indicator does not found, please reconfig the widget.'
});
}
}

// Fetch the data if it is using no filter or custom
useEffect(() => {
(
Expand Down Expand Up @@ -107,46 +146,20 @@ export default function SummaryWidgetView({ idx, data }) {
const indicator = indicators.find((layer) => {
return layer.id === layer_id;
})
if (indicator) {
await fetchingData(
indicator.url, params, {}, function (response, error) {
let newState = {
fetching: false,
fetched: true,
receivedAt: Date.now(),
data: null,
error: null
};

if (error) {
newState.error = error;
} else {
newState.data = response;
}
if (!session.isValid) {
return
}
setLayerData(newState);
}
)
} else {
if (!session.isValid) {
return
}
setLayerData({
fetching: false,
fetched: true,
receivedAt: Date.now(),
data: null,
error: 'Indicator does not found, please reconfig the widget.'
});
}
await fetchIndicatorData(indicator, params, session, indicator)
break;
// This is for indicator layer
case definition.WidgetLayerUsed.INDICATOR_LAYER:
const indicatorLayer = indicatorLayers.find((layer) => {
return layer.id === layer_id;
})
if (!isIndicatorLayerLikeIndicator(indicatorLayer)) {
const indicator = indicators.find((indicator) => {
return indicator.id === indicatorLayer.indicators[0].id;
})
await fetchIndicatorData(indicator, params, session, indicatorLayer)
return;
}
const dynamicLayerIndicators = dynamicLayerIndicatorList(indicatorLayer, indicators)
const indicatorsData = {}
for (let x = 0; x < dynamicLayerIndicators.length; x++) {
Expand Down Expand Up @@ -183,7 +196,8 @@ export default function SummaryWidgetView({ idx, data }) {
data: null,
error: error
});
}, response => {
},
response => {
if (!session.isValid) {
return
}
Expand All @@ -194,7 +208,7 @@ export default function SummaryWidgetView({ idx, data }) {
data: response,
error: null
});
}
}, false, true
)
}
}
Expand Down Expand Up @@ -230,7 +244,6 @@ export default function SummaryWidgetView({ idx, data }) {
layers = indicatorLayers
break;
}

// render widget by the type
switch (type) {
case DEFINITION.WidgetType.SUMMARY_WIDGET:
Expand Down
3 changes: 2 additions & 1 deletion django_project/frontend/src/components/Widget/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
text-align: left;

.widget__sw__title,
.widget__gw__title{
.widget__gw__title {
color: $widget-title;
font-size: 1rem;
margin-bottom: 10px;
Expand Down Expand Up @@ -132,6 +132,7 @@

.widget__sgw__row__name {
width: 1%;
white-space: nowrap;
}

.widget__sgw__row {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export default function PermissionFormAdmin({ permissionApi }) {

/** Fetch data when modal is opened **/
useEffect(() => {
try {
if (permissionFormData) {
setData(permissionFormData)
return
}
} catch (err) {

}
if (permissionApi) {
let url = permissionApi
if (selectableInput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export default function ReferenceLayerCentroid({ map }) {
if (!indicatorLayer.override_style && indicatorDetail) {
styleConfig = indicatorDetail
}
if (!labelConfig || !indicatorLayer.override_label && indicatorDetail) {
if (indicatorDetail && (!labelConfig || !indicatorLayer.override_label)) {
labelConfig = indicatorDetail.label_config
}
}
Expand Down
9 changes: 6 additions & 3 deletions django_project/frontend/src/utils/indicatorLayer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { capitalize, dictDeepCopy } from "./main";
import nunjucks from "nunjucks";
import { extractCode } from "./georepo";
import { getRelatedTableData } from "./relatedTable";
import { getIndicatorDataByLayer } from "./indicatorData";
import { getIndicatorDataByLayer, UpdateStyleData } from "./indicatorData";

export const SingleIndicatorType = 'Single Indicator'
export const SingleIndicatorTypes = [SingleIndicatorType, 'Float']
Expand Down Expand Up @@ -86,12 +86,12 @@ export function dynamicLayerData(indicatorLayer, context) {
*/
export function fetchDynamicLayerData(
indicatorLayer, indicators, indicatorsData, geoField,
onError, onSuccess, skipAggregate
onError, onSuccess, skipAggregate, updateStyle = false
) {
const dynamicLayerIndicators = dynamicLayerIndicatorList(indicatorLayer, indicators)

let error = ''
const data = []
let data = []
dynamicLayerIndicators.map(indicator => {
if (indicatorsData[indicator.id]?.data) {
indicatorsData[indicator.id].data.map(row => {
Expand Down Expand Up @@ -148,6 +148,9 @@ export function fetchDynamicLayerData(
value: dynamicLayerData(indicatorLayer, value)
})
}
if (updateStyle) {
response = UpdateStyleData(response, indicatorLayer)
}
onSuccess(response)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<script>
const initialData = {{ form|get_data|safe }};
const permission = {{ permission|safe }};
const permissionFormData = {% if form.permission_data %}{{ form.permission_data|safe }}{% else %}null{% endif %};

// Urls api
urls.api = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
const initialData = {{ form|get_data|safe }};
const permission = {{ permission|safe }};
const dynamicClassification = {{ dynamicClassification|safe }};
const permissionFormData = {% if form.permission_data %}{{ form.permission_data|safe }}{% else %}null{% endif %};

// Urls api
urls.api = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
{% block scripts %}
<script>
const indicator = {{ form.indicator_data|safe }};
const permissionFormData = {% if form.permission_data %}{{ form.permission_data|safe }}{% else %}null{% endif %};
const indicatorId = {% if indicator_id %}{{ indicator_id }}{% else %}null{% endif %};
const indicatorRules = {{ rules|safe }};
const types = {{ types|safe }};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
{% block scripts %}
<script>
const initialData = {{ form|get_data|safe }};
const permissionFormData = {% if form.permission_data %}{{ form.permission_data|safe }}{% else %}null{% endif %};
const style = {{ form.instance_data|safe }};
const currentRules = {{ rules|safe }};
const types = {{ types|safe }};
Expand Down
3 changes: 3 additions & 0 deletions django_project/frontend/views/admin/basemap/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def get_context_data(self, **kwargs) -> dict:

def post(self, request, **kwargs):
"""Create indicator."""
data = request.POST.copy()
form = BasemapForm(request.POST)
if form.is_valid():
instance = form.instance
Expand All @@ -92,6 +93,8 @@ def post(self, request, **kwargs):
) + '?success=true'
)
context = self.get_context_data(**kwargs)
if data.get('permission', None):
form.permission_data = data.get('permission', None)
context['form'] = form
return render(
request,
Expand Down
3 changes: 3 additions & 0 deletions django_project/frontend/views/admin/basemap/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def get_context_data(self, **kwargs) -> dict:

def post(self, request, **kwargs):
"""Edit basemap."""
data = request.POST.copy()
basemap = get_object_or_404(
BasemapLayer, id=self.kwargs.get('pk', '')
)
Expand All @@ -97,6 +98,8 @@ def post(self, request, **kwargs):
) + '?success=true'
)
context = self.get_context_data(**kwargs)
if data.get('permission', None):
form.permission_data = data.get('permission', None)
context['form'] = form
return render(request, self.template_name, context)

Expand Down
6 changes: 6 additions & 0 deletions django_project/frontend/views/admin/context_layer/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,18 @@ def post(self, request, **kwargs):
instance.creator = request.user
instance.save()
instance.save_relations(data)
# Save permission
instance.permission.update_from_request_data(
request.POST, request.user
)
return redirect(
reverse(
'admin-context-layer-edit-view', kwargs={'pk': instance.id}
) + '?success=true'
)
context = self.get_context_data(**kwargs)
if data.get('permission', None):
form.permission_data = data.get('permission', None)
context['form'] = form
return render(
request,
Expand Down
6 changes: 6 additions & 0 deletions django_project/frontend/views/admin/context_layer/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,18 @@ def post(self, request, **kwargs):
if form.is_valid():
context_layer = form.save()
context_layer.save_relations(data)
# Save permission
instance.permission.update_from_request_data(
request.POST, request.user
)
return redirect(
reverse(
'admin-context-layer-edit-view', kwargs={'pk': instance.id}
) + '?success=true'
)
context = self.get_context_data(**kwargs)
if data.get('permission', None):
form.permission_data = data.get('permission', None)
context['form'] = form
return render(request, self.template_name, context)

Expand Down
2 changes: 2 additions & 0 deletions django_project/frontend/views/admin/indicator/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ def post(self, request, **kwargs):
form.indicator_data = json.dumps(
IndicatorForm.model_to_initial(form.instance)
)
if data.get('permission', None):
form.permission_data = data.get('permission', None)
context['form'] = form
return render(
request,
Expand Down
2 changes: 2 additions & 0 deletions django_project/frontend/views/admin/indicator/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def post(self, request, **kwargs):
form.indicator_data = json.dumps(
IndicatorForm.model_to_initial(form.instance)
)
if data.get('permission', None):
form.permission_data = data.get('permission', None)
context['form'] = form
return render(request, self.template_name, context)

Expand Down
2 changes: 2 additions & 0 deletions django_project/frontend/views/admin/style/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def post(self, request, **kwargs):
form.instance_data = json.dumps(
StyleForm.model_to_initial(form.instance)
)
if data.get('permission', None):
form.permission_data = data.get('permission', None)
context['form'] = form
return render(
request,
Expand Down
2 changes: 2 additions & 0 deletions django_project/frontend/views/admin/style/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def post(self, request, **kwargs):

permission = style.permission.all_permission(self.request.user)
context['permission'] = permission
if data.get('permission', None):
form.permission_data = data.get('permission', None)
context['form'] = form
return render(request, self.template_name, context)

Expand Down

0 comments on commit cdfb81e

Please sign in to comment.