Skip to content

Commit

Permalink
Merge pull request #122 from lightningrodlabs/feature/reference_bugs
Browse files Browse the repository at this point in the history
Feature/reference bugs
  • Loading branch information
adaburrows authored Aug 12, 2022
2 parents a52b6b2 + fe089d5 commit 967554c
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 46 deletions.
2 changes: 1 addition & 1 deletion ui/src/components/input/Commitment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const CommitmentInput: React.FC<Props> = ({commitmentState, conformingResource,
<br />
<MeasurementInput label="Effort" value={effortQuantity} defaultUnit={conformingResource.defaultUnitOfEffort} name='effortQuantity' onChange={onSlChange} units={units} />
<br />
<SlInput label="Due" type="datetime-local" valueAsDate={due as Date} value={due ? DateToInputValueString(due as Date): ''} name="due" onSlChange={onSlChange} onSlInput={onSlChange}></SlInput>
<SlInput label="Due" type="datetime-local" value={due ? DateToInputValueString(due as Date): ''} name="due" onSlChange={onSlChange} onSlInput={onSlChange}></SlInput>
<br />
<LocationInput label="Location" name="atLocation" value={atLocation} onChange={onSlChange}></LocationInput>
<br />
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/input/Event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const EventInput: React.FC<Props> = ({eventState, readonlyFields, conformingReso
<br />
<MeasurementInput disableUnit={disabled('effortQuantityUnit')} label="Effort" value={effortQuantity} defaultUnit={conformingResource.defaultUnitOfEffort} name='effortQuantity' onChange={onSlChange} units={units} />
<br />
<SlInput label="Datetime" type="datetime-local" valueAsDate={hasPointInTime as Date} value={hasPointInTime ? DateToInputValueString(hasPointInTime as Date): ''} name="hasPointInTime" onSlChange={onSlChange} onSlInput={onSlChange}></SlInput>
<SlInput label="Datetime" type="datetime-local" value={hasPointInTime ? DateToInputValueString(hasPointInTime as Date): ''} name="hasPointInTime" onSlChange={onSlChange} onSlInput={onSlChange}></SlInput>
<br />
<LocationInput disabled={disabled('atLocation')} label="At Location" name="atLocation" value={atLocation} onChange={onSlChange}></LocationInput>
<br />
Expand Down
4 changes: 2 additions & 2 deletions ui/src/components/modals/FlowModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ const FlowModal: React.FC<Props> = ({vfPath, source, target, closeModal, afterwa
* When the commitment changes, update it.
*/
const handleCommitmentChange = (e: any) => {
commitmentUpdates = e.target.value;
commitmentUpdates = new Commitment(e.target.value);
}

/**
* When the current event changes, update it.
*/
const handleEventChange = (e: any) => {
eventUpdates = e.target.value;
eventUpdates = new EconomicEvent(e.target.value);
}

/**
Expand Down
36 changes: 27 additions & 9 deletions ui/src/components/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,46 @@ export function slChangeConstructor<T> (
return onSlChange;
}

function padBy2 (n: number): string {
const num = n.toString();
const len = num.length;
let pad = '';
for (let i = 0; i < (2 - len); i ++) {
pad += '0';
}
return pad + num;
}

function getDateParts (dt: Date) {
const y = dt.getFullYear();
const M = padBy2(dt.getMonth()+1); // Date's getMonth() is zero based
const d = padBy2(dt.getDate())
const h = padBy2(dt.getHours());
const m = padBy2(dt.getMinutes());
return {y, M, d, h, m };
}

/**
* Convert a Date to a weird ISO string
*
* This is to get rid of the error:
* The specified value "*" does not conform to the required format. The format
* is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS".
*
* For some reason, it still warns:
* The specified value "2022-07-16T10:45:00" does not conform to the required
* format. The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or
* ":ss.SSS".
* Yet, that does conform to the right format. /me shrugs.
*/
export const DateToInputValueString = (dt: Date) => {
export const DateToInputValueString = (dt: Date): string => {
if (dt instanceof Date) {
// removes the milliseconds and Z from the end of the date string
return dt.toISOString().split('.')[0];
const { y, M, d, h, m } = getDateParts(dt);
return `${y}-${M}-${d}T${h}:${m}`;
} else {
throw new Error("Date is somehow not a Date");
}
}

export const DateToUiString = (dt: Date): string => {
const { y, M, d, h, m } = getDateParts(dt);
return `${y}/${M}/${d} ${h}:${m}`;
}

/**
* Coerce to string if needed
*/
Expand Down
3 changes: 1 addition & 2 deletions ui/src/data/models/Application/Display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { XYPosition, Node } from 'react-flow-renderer';
import { PathedData, getAlmostLastPart } from "../PathedData";
import { NamedData } from "../NamedData";
import { assignFields, fieldsToJSON } from '../../../utils';
import getDataStore from "../../DataStore";

export interface DisplayNodeShape {
id?: string;
Expand Down Expand Up @@ -104,4 +103,4 @@ export class DisplayEdge implements DisplayEdgeShape, PathedData {
['id', 'source', 'target', 'sourceHandle', 'targetHandle', 'vfPath', 'planId']
);
}
}
}
42 changes: 38 additions & 4 deletions ui/src/data/models/Valueflows/Knowledge.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Guid } from "guid-typescript";
import { PathedData } from "../PathedData";
import { NamedData } from "../NamedData";
import { AgentShape, ResourceSpecificationShape, ProcessSpecificationShape, ActionShape, InputOutput, ResourceEffect, UnitShape, MeasurementShape} from "../../../types/valueflows";
import {
AgentShape,
ResourceSpecificationShape,
ProcessSpecificationShape,
ActionShape,
InputOutput,
ResourceEffect,
UnitShape,
MeasurementShape,
GeoDataShape,
GeoPointShape
} from "../../../types/valueflows";
import { assignFields, toJSON } from '../../../utils';

// Knowledge Classes
Expand All @@ -20,7 +31,7 @@ export class Agent implements AgentShape, PathedData, NamedData {
constructor(init: AgentShape) {
assignFields<AgentShape, Agent>(init, this);
this.id = this.id ? this.id : Guid.raw();
this.created = this.created ? this.created : new Date();
this.created = this.created ? new Date(this.created) : new Date();
}

static getPrefix(): string {
Expand Down Expand Up @@ -201,6 +212,29 @@ export class Measurement implements MeasurementShape {
})
}

export class GeoPoint implements GeoPointShape {
lat: number;
lng: number;

constructor(init: GeoPointShape) {
assignFields<GeoPointShape, GeoPoint>(init, this);
}
}

export class GeoData implements GeoDataShape {
type: string;
name?: string;
address?: string;
point?: GeoPoint | string;

constructor(init: GeoDataShape) {
assignFields<GeoDataShape, GeoData>(init, this);
if (init.point && typeof init.point == 'object') {
this.point = new GeoPoint(init.point);
}
}
}

/**
* The archetype of a resource. The accounting happens on the `EconomicResource`.
*/
Expand All @@ -217,7 +251,7 @@ export class ResourceSpecification implements ResourceSpecificationShape, Pathed
constructor(init: ResourceSpecificationShape) {
assignFields<ResourceSpecificationShape, ResourceSpecification>(init, this);
this.id = this.id ? this.id : Guid.raw();
this.created = this.created ? this.created : new Date();
this.created = this.created ? new Date(this.created) : new Date();
}

static getPrefix(): string {
Expand Down Expand Up @@ -249,7 +283,7 @@ export class ProcessSpecification implements ProcessSpecificationShape, PathedDa
constructor(init: ProcessSpecificationShape) {
assignFields<ProcessSpecificationShape, ProcessSpecification>(init, this);
this.id = this.id ? this.id : Guid.raw();
this.created = this.created ? this.created : new Date();
this.created = this.created ? new Date(this.created) : new Date();
}

static getPrefix(): string {
Expand Down
25 changes: 16 additions & 9 deletions ui/src/data/models/Valueflows/Observation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PathedData } from "../PathedData";
import { NamedData } from "../NamedData";
import { GeoDataShape, EconomicResourceShape, EconomicEventShape, FulfillmentShape, MeasurementShape } from "../../../types/valueflows";
import { assignFields, toJSON } from '../../../utils';
import { Measurement } from "./Knowledge";
import { GeoData, Measurement } from "./Knowledge";

export class EconomicResource implements EconomicResourceShape, PathedData, NamedData {
id: string;
Expand All @@ -27,7 +27,9 @@ export class EconomicResource implements EconomicResourceShape, PathedData, Name
constructor(init: EconomicResourceShape) {
assignFields<EconomicResourceShape, EconomicResource>(init, this);
this.id = this.id ? this.id : Guid.raw();
this.created = this.created ? this.created : new Date();
this.created = this.created ? new Date(this.created) : new Date();
this.currentLocation = (this.currentLocation && this.currentLocation != null) ? new GeoData(this.currentLocation) : null;
this.note = init.note ? init.note : null;
}

static getPrefix(): string {
Expand Down Expand Up @@ -76,12 +78,15 @@ export class EconomicEvent implements EconomicEventShape {
constructor(init: EconomicEventShape) {
assignFields<EconomicEventShape, EconomicEvent>(init, this);
this.id = this.id ? this.id : Guid.raw();
this.created = this.created ? this.created : new Date();
this.hasPointInTime = init.hasPointInTime ? new Date(Date.parse(init.hasPointInTime as string)) : null;
this.hasBegining = init.hasBegining ? new Date(Date.parse(init.hasBegining as string)) : null;
this.hasEnd = init.hasEnd ? new Date(Date.parse(init.hasEnd as string)) : null;
this.created = this.created ? new Date(this.created) : new Date();
this.note = init.note ? init.note : null;
this.hasPointInTime = init.hasPointInTime ? new Date(init.hasPointInTime) : null;
this.hasBegining = init.hasBegining ? new Date(init.hasBegining) : null;
this.hasEnd = init.hasEnd ? new Date(init.hasEnd) : null;
this.resourceQuantity = (init.resourceQuantity && init.resourceQuantity != null) ? new Measurement(init.resourceQuantity): null;
this.effortQuantity = (init.effortQuantity && init.effortQuantity != null) ? new Measurement(init.effortQuantity): null;
this.atLocation = (this.atLocation && init.atLocation != null) ? new GeoData(this.atLocation) : null;
this.toLocation = (this.toLocation && init.toLocation != null) ? new GeoData(this.toLocation) : null;
}

static getPrefix(): string {
Expand All @@ -104,15 +109,17 @@ export class EconomicEvent implements EconomicEventShape {
export class Fulfillment implements FulfillmentShape {
id: string;
created: Date;
resourceQuantity?: number;
effortQuantity?: number;
resourceQuantity?: MeasurementShape;
effortQuantity?: MeasurementShape;
fulfills: string; // Commitment ID
fulfilledBy: string; // EconomicEvent ID

constructor(init: FulfillmentShape) {
assignFields<FulfillmentShape, Fulfillment>(init, this);
this.id = this.id ? this.id : Guid.raw();
this.created = this.created ? this.created : new Date();
this.created = this.created ? new Date(this.created) : new Date();
this.resourceQuantity = (init.resourceQuantity && init.resourceQuantity != null) ? new Measurement(init.resourceQuantity): null;
this.effortQuantity = (init.effortQuantity && init.effortQuantity != null) ? new Measurement(init.effortQuantity): null;
}

static getPrefix(): string {
Expand Down
25 changes: 17 additions & 8 deletions ui/src/data/models/Valueflows/Plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
GeoDataShape
} from "../../../types/valueflows";
import { assignFields, fieldsToJSON, toJSON } from '../../../utils';
import { Measurement } from "./Knowledge";
import { GeoData, Measurement } from "./Knowledge";

// Plan Classes

Expand All @@ -34,7 +34,9 @@ export class Plan implements PlanShape, PathedData, NamedData {
constructor(init: PlanShape) {
assignFields<PlanShape, Plan>(init, this);
this.id = this.id ? this.id : Guid.raw();
this.created = this.created ? this.created : new Date();
this.created = this.created ? new Date(this.created) : new Date();
this.due = init.due ? new Date(init.due) : null;
this.note = init.note ? init.note : null;
this.process = {};
this.commitment = {};
this.displayNode = {};
Expand Down Expand Up @@ -89,7 +91,12 @@ export class Process implements ProcessShape, PathedData, NamedData {
constructor(init: ProcessShape) {
assignFields<ProcessShape, Process>(init, this);
this.id = this.id ? this.id : Guid.raw();
this.created = this.created ? this.created : new Date();
this.created = this.created ? new Date(this.created) : new Date();
this.hasPointInTime = init.hasPointInTime ? new Date(Date.parse(init.hasPointInTime as string)) : null;
this.hasBegining = init.hasBegining ? new Date(Date.parse(init.hasBegining as string)) : null;
this.hasEnd = init.hasEnd ? new Date(Date.parse(init.hasEnd as string)) : null;
this.due = init.due ? new Date(Date.parse(init.due as string)) : null;
this.note = init.note ? init.note : null;
}

static getPrefix(planId: string): string {
Expand Down Expand Up @@ -164,13 +171,15 @@ export class Commitment implements CommitmentShape, PathedData {
constructor(init: CommitmentShape) {
assignFields<CommitmentShape, Commitment>(init, this);
this.id = this.id ? this.id : Guid.raw();
this.created = this.created ? this.created : new Date();
this.hasPointInTime = init.hasPointInTime ? new Date(Date.parse(init.hasPointInTime as string)) : null;
this.hasBegining = init.hasBegining ? new Date(Date.parse(init.hasBegining as string)) : null;
this.hasEnd = init.hasEnd ? new Date(Date.parse(init.hasEnd as string)) : null;
this.due = init.due ? new Date(Date.parse(init.due as string)) : null;
this.created = this.created ? new Date(this.created) : new Date();
this.hasPointInTime = init.hasPointInTime ? new Date(init.hasPointInTime) : null;
this.hasBegining = init.hasBegining ? new Date(init.hasBegining) : null;
this.hasEnd = init.hasEnd ? new Date(init.hasEnd) : null;
this.due = init.due ? new Date(init.due as string) : null;
this.note = init.note ? init.note : null;
this.resourceQuantity = (init.resourceQuantity && init.resourceQuantity != null) ? new Measurement(init.resourceQuantity): null;
this.effortQuantity = (init.effortQuantity && init.effortQuantity != null) ? new Measurement(init.effortQuantity): null;
this.atLocation = (this.atLocation && init.atLocation != null) ? new GeoData(this.atLocation) : null;
}

static getPrefix(planId: string): string {
Expand Down
8 changes: 4 additions & 4 deletions ui/src/logic/flows.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// EDGE BUSINESS LOGIC

import { Edge, MarkerType, Node } from "react-flow-renderer";
import { DateToUiString } from "../components/util";
import getDataStore from "../data/DataStore";
import { DisplayEdge, DisplayNode } from "../data/models/Application/Display";
import { ObjectTypeMap } from "../data/models/ObjectTransformations";
Expand Down Expand Up @@ -175,7 +176,6 @@ export const getEventDefaultsFromCommitment = (commitment: Commitment): FlowShap
delete init.plannedWithin;
delete init.due;
delete init.note;
init.hasPointInTime = new Date();
return init;
}

Expand All @@ -187,7 +187,7 @@ export const getEventDefaultsFromEvent = (event: EconomicEvent): FlowShape => {
delete init.id;
delete init.created;
delete init.note;
init.hasPointInTime = new Date();
init.hasPointInTime = event.hasPointInTime ? new Date(event.hasPointInTime) : null;
return init;
}

Expand Down Expand Up @@ -269,10 +269,10 @@ export const getLabelForFlow = (flow: FlowShape, resource: ResourceSpecification

function dateString(flow: FlowShape):string {
if (flow.due) {
return ` ${flow.due.toLocaleString()}`;
return ` ${DateToUiString(flow.due as Date)}`;
}
if (flow.hasPointInTime) {
return ` ${flow.hasPointInTime.toLocaleString()}`;
return ` ${DateToUiString(flow.hasPointInTime as Date)}`;
}
return '';
}
Expand Down
4 changes: 2 additions & 2 deletions ui/src/types/valueflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ export interface EconomicEventShape extends HasIdDate, HasTime, HasAction, ReaBa
}

export interface FulfillmentShape extends HasIdDate {
resourceQuantity?: number;
effortQuantity?: number;
resourceQuantity?: MeasurementShape;
effortQuantity?: MeasurementShape;
fulfills: string;
fulfilledBy: string;
}
Expand Down
9 changes: 5 additions & 4 deletions ui/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,25 @@ export function objectsDiff<T extends Object> (A: T, B: T): boolean {
const aFields = Reflect.ownKeys(A);
const bFields = Reflect.ownKeys(B);

// Compare right side with left side
// Compare right side with left side (logical AND)
const right = aFields.reduce((acc, aField) => {
return (
acc
&& bFields.includes(aField)
&& aFields[aField] === bFields[aField]
&& A[aField] === B[aField]
);
}, true);

// Compare left side with right side
// Compare left side with right side (logical AND)
const left = bFields.reduce((acc, bField) => {
return (
acc
&& aFields.includes(bField)
&& aFields[bField] === bFields[bField]
&& A[bField] === B[bField]
);
}, true);

// If one side of the comparison is false, it's changed in some way
return !(left&&right);
}
return true;
Expand Down

0 comments on commit 967554c

Please sign in to comment.