Skip to content

Commit

Permalink
do not crash when using visibility 'only for groups' (#343)
Browse files Browse the repository at this point in the history
* do not crash when using visibility 'only for groups'

- closes #341

* fix logic for applying visiblity

* rm logs
  • Loading branch information
foxriver76 authored Jan 29, 2024
1 parent 5d4d456 commit 25b59e5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ E.g., if it was used in a menu and the menu is red, the circle would be red.
## Changelog
### **WORK IN PROGRESS**
* (foxriver76) fixed resizing issue for relative widgets
* (foxriver76) do not crash when using visibility "only for groups"

### 2.9.24 (2024-01-24)
* (foxriver76) Image 8 widget ported to react
Expand Down
4 changes: 2 additions & 2 deletions src/src/Attributes/Widget/WidgetField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ const WidgetField = props => {
let onChangeTimeout;

const applyValue = newValues => {
const project = JSON.parse(JSON.stringify(store.getState().visProject));
const project = deepClone(store.getState().visProject);
props.selectedWidgets.forEach((selectedWidget, i) => {
const value = Array.isArray(newValues) ? newValues[i] : newValues;
const value = Array.isArray(newValues) && field.type !== 'groups' ? newValues[i] : newValues;

const data = props.isStyle
? project[props.selectedView].widgets[selectedWidget].style
Expand Down
34 changes: 33 additions & 1 deletion src/src/Vis/visRxWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ interface VisRxWidgetProps extends VisBaseWidgetProps {
interface RxData {
_originalData: any;
filterkey: any;
/** If value is hide widget should be hidden if user not in group, else disabled */
'visibility-groups-action': 'hide' | 'disabled';
/** If entry in array but user not in array, apply visibility-groups-action logic */
'visibility-groups': string[];
}

/** TODO move to VisBaseWidget when ported */
Expand Down Expand Up @@ -319,7 +323,7 @@ class VisRxWidget<TRxData extends Record<string, any>> extends VisBaseWidget {
this.newState.rxData.filterkey = this.newState.rxData.filterkey.split(/[;,]+/).map(f => f.trim()).filter(f => f);
}

if (userGroups && userGroups.length && !this.isUserMemberOfGroup(this.props.context.user, userGroups)) {
if (userGroups?.length && !this.isUserMemberOfGroup(this.props.context.user, userGroups)) {
// @ts-expect-error fix later
if (this.newState.rxData['visibility-groups-action'] === 'disabled') {
this.newState.disabled = true;
Expand Down Expand Up @@ -410,8 +414,36 @@ class VisRxWidget<TRxData extends Record<string, any>> extends VisBaseWidget {
super.componentWillUnmount();
}

/**
* Check if the logged-in user's group has visibility permissions for this widget
*/
isWidgetVisibleForGroup(newState: typeof this.newState): boolean {
// @ts-expect-error fix later
const userGroups = newState.rxData['visibility-groups'];

// @ts-expect-error fix later
if (newState.rxData['visibility-groups-action'] === 'hide') {
if (userGroups?.length && !this.isUserMemberOfGroup(this.props.context.user, userGroups)) {
return false;
}
}

return true;
}

/**
* Checks if widget is visible according to the state id
*
* @param stateId state id to check visibility for
* @param newState the new state
*/
checkVisibility(stateId?: string | null, newState?: typeof this.newState) {
newState = newState || this.state;

if (!this.isWidgetVisibleForGroup(newState)) {
return false;
}

if (!this.state.editMode) {
if (!this.isWidgetFilteredOut(newState.rxData)) {
if (stateId) {
Expand Down

0 comments on commit 25b59e5

Please sign in to comment.