Skip to content

Commit

Permalink
Fix JS errors. (#1600)
Browse files Browse the repository at this point in the history
* Fix JS errors.

* Fix component unregistration
  • Loading branch information
adamdriscoll authored Apr 6, 2020
1 parent 057757b commit 27d0110
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 26 deletions.
16 changes: 14 additions & 2 deletions src/UniversalDashboard.MaterialUI/Components/paper.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
/** @jsx jsx */
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Paper from "@material-ui/core/Paper";
import classNames from "classnames"
import {jsx} from 'theme-ui'
import { withComponentFeatures } from './universal-dashboard';

const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
display: "flex",
padding: theme.spacing.unit * 2,
margin: theme.spacing.unit
}
}));

const UdPaper = (props) => {

const classes = useStyles();

const {
classes,
elevation,
style,
height,
Expand All @@ -18,7 +30,7 @@ const UdPaper = (props) => {
return (
<Paper
id={props.id}
className={classNames("ud-mu-paper")}
className={classNames(classes.root, "ud-mu-paper")}
elevation={elevation}
style={{...style}}
height={height}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,22 @@ export const withComponentFeatures = (component) => {

function isString (obj) {
return (Object.prototype.toString.call(obj) === '[object String]');
}
}

const render = (component, history) => {

if (isString(component)) {
component = JSON.parse(component);
}

// set props version
if (!component.version)
if (!isString(component))
{
component.version = "0";
}

if (!history && component.history) {
history = component.history;
// set props version
if (!component.version)
{
component.version = "0";
}

if (!history && component.history) {
history = component.history;
}
}

return UniversalDashboard.renderComponent(component, history);
}

Expand All @@ -100,8 +98,10 @@ export const withComponentFeatures = (component) => {
...event.state
});

if (type === "getState")
if (type === "getState") {
sendComponentState(event.requestId, componentState);
}


if (type === "addElement")
{
Expand Down Expand Up @@ -132,11 +132,16 @@ export const withComponentFeatures = (component) => {
setComponentState({...componentState, version: Math.random().toString(36).substr(2, 5) })
}
}

useEffect(() => {
const token = subscribeToIncomingEvents(props.id, incomingEvent)
return () => {
unsubscribeFromIncomingEvents(token)
}
});

useEffect(() => {
return () => {
UniversalDashboard.publish('element-event', {
type: "unregisterEvent",
eventId: props.id
Expand Down
26 changes: 26 additions & 0 deletions src/UniversalDashboard.MaterialUI/dashboard.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,32 @@ New-UDDashboard -Title "Dashboard" -Theme (get-udtheme basic) -Pages @(
$EventData = $Body | ConvertFrom-Json
Set-TestData $EventData.Length
} -Multiple

New-UDDynamic -Id 'spacingGrid' -Content {
$Spacing = (Get-UDElement -Id 'spacingSelect').value

New-UDGrid -Spacing $Spacing -Container -Content {
New-UDGrid -Item -ExtraSmallSize 3 -Content {
New-UDPaper -Content { "xs-3" } -Elevation 2
}
New-UDGrid -Item -ExtraSmallSize 3 -Content {
New-UDPaper -Content { "xs-3" } -Elevation 2
}
New-UDGrid -Item -ExtraSmallSize 3 -Content {
New-UDPaper -Content { "xs-3" } -Elevation 2
}
New-UDGrid -Item -ExtraSmallSize 3 -Content {
New-UDPaper -Content { "xs-3" } -Elevation 2
}
}
}

New-UDSelect -Id 'spacingSelect' -Label Spacing -Option {
for($i = 0; $i -lt 10; $i++)
{
New-UDSelectOption -Name $i -Value $i
}
} -OnChange { Sync-UDElement -Id 'spacingGrid' } -DefaultValue 3
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/UniversalDashboard/Cmdlets/GetElementCommand.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using NLog;
using System.Management.Automation;
using UniversalDashboard.Models.Basics;
using System.Linq;
using Microsoft.AspNetCore.SignalR;
using System;
using Microsoft.Extensions.Caching.Memory;
using System.Security.Claims;
using UniversalDashboard.Services;
using System.Collections;

Expand Down
5 changes: 5 additions & 0 deletions src/UniversalDashboard/Execution/EndpointService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void Unregister(string name, string sessionId)
logger.Debug("Session endpoint found. Removing endpoint.");
session.Endpoints.TryRemove(name, out AbstractEndpoint value);
}

if (Endpoints.TryGetValue(name, out AbstractEndpoint pageEndpoint) && pageEndpoint.IsPage)
{
session.Endpoints.Clear();
}
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions src/client/src/app/services/universal-dashboard-service.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ import toaster from './toaster';
var components = []

function isEmpty(obj) {
for(var key in obj) {
for(var key in obj) {
if(obj.hasOwnProperty(key))
return false;
}
return true;
}

function isString (obj) {
return (Object.prototype.toString.call(obj) === '[object String]');
}

const renderComponent = (component, history, dynamicallyLoaded) => {
if (component == null) return <React.Fragment />;
if (isEmpty(component)) return <React.Fragment />;


if (component.$$typeof === Symbol.for('react.element'))
{
return component;
Expand All @@ -29,6 +32,19 @@ const renderComponent = (component, history, dynamicallyLoaded) => {
return component.map(x => renderComponent(x, history));
}

if (isString(component)) {
try
{
component = JSON.parse(component);
}
catch
{
return component;
}
}

if (component.type == null) return <React.Fragment />;

var existingComponent = components.find(x => x.type === component.type);
if (existingComponent != null) {
return React.createElement(existingComponent.component, {
Expand Down
9 changes: 7 additions & 2 deletions src/poshud/start.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Import-Module "$PSScriptRoot\..\output\UniversalDashboard.Community.psd1"
$Dashboard = . "$PSScriptRoot\dashboard.ps1"
Start-UDDashboard -Port 10000 -Dashboard $Dashboard -Force
Import-Module "$PSScriptRoot\dashboard.psm1"
$Dashboard = New-DemoDashboard
Start-UDDashboard -Port 10000 -Dashboard $Dashboard -Force

# $D = (Get-UDDashboard).DashboardService
# (Get-UDDashboard).DashboardService.EndpointService.SessionManager.Sessions.GetEnumerator().Value.Endpoints.GetEnumerator() |Measure
# $D.EndpointService.Get('9c1b6cbe-877d-424f-88d1-069e84d01bfe', 'c7b48bb6-3dfe-4164-bcbc-9c6b511a0e33')

0 comments on commit 27d0110

Please sign in to comment.