Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new custom actions and changing query structure. #83

Open
wants to merge 2 commits into
base: rc-0.3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/components/data-model-table/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,27 @@
}
const parentIdFieldName = gqlAssociationProps.parentIdFieldName;
const associatedIdFieldName = gqlAssociationProps.associatedIdFieldName;
const gqlListQuery = gqlAssociationProps.gqlListQuery;
let gqlListQuery: any | ((parentRecord: any) => any) =

Check failure on line 597 in src/components/data-model-table/editable.tsx

View workflow job for this annotation

GitHub Actions / Linter (18)

'gqlListQuery' is never reassigned. Use 'const' instead
gqlAssociationProps.gqlListQuery;
const getGqlQueryVariables = gqlListQuery?.getQueryVariables;
let gqlQueryVariables = undefined;

// Getting record
let record;
try {
record = form.getFieldValue(fieldPath.pop().keyPath);
} catch (_e: any) {
// ignore
}
if (!record) {
record = parentRecord;
}
// Setting query value
const gqlDocumentQuery =
typeof gqlListQuery?.query === 'function'
? gqlListQuery?.query(record, useSelector)
: gqlListQuery?.query;

if (getGqlQueryVariables) {
gqlQueryVariables = getGqlQueryVariables(parentRecord, useSelector);
}
Expand All @@ -607,7 +625,7 @@
fieldPath={fieldPath}
parentIdFieldName={parentIdFieldName!}
associatedIdFieldName={associatedIdFieldName!}
gqlQuery={gqlListQuery?.query}
gqlQuery={gqlDocumentQuery}
parentRecord={parentRecord}
associatedRecordClass={field.dtoClass!}
value={form.getFieldValue(fieldPath.namePath)}
Expand Down
53 changes: 40 additions & 13 deletions src/components/form/array-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,27 @@
const associatedIdFieldName =
schema.gqlAssociationProps.associatedIdFieldName;
if (disabled) {
const gqlQuery = schema.gqlAssociationProps.gqlListSelectedQuery;
let gqlQuery: any | ((parentRecord: any) => any) =

Check failure on line 39 in src/components/form/array-field.tsx

View workflow job for this annotation

GitHub Actions / Linter (18)

'gqlQuery' is never reassigned. Use 'const' instead
schema.gqlAssociationProps.gqlListSelectedQuery;
const getGqlQueryVariables = gqlQuery?.getQueryVariables;
let gqlQueryVariables = undefined;

// Getting record
let record;
try {
record = form.getFieldValue(fieldPath.pop().keyPath);
} catch (_e: any) {
// ignore
}
if (!record) {
record = parentRecord;
}
// Setting query value
const gqlDocumentQuery =
typeof gqlQuery?.query === 'function'
? gqlQuery?.query(record, useSelector)
: gqlQuery?.query;

if (getGqlQueryVariables) {
gqlQueryVariables = getGqlQueryVariables(parentRecord, useSelector);
}
Expand All @@ -48,7 +66,7 @@
expandedContent={
<AssociatedTable
associatedRecordClass={schema.dtoClass!}
gqlQuery={gqlQuery?.query}
gqlQuery={gqlDocumentQuery}
gqlQueryVariables={gqlQueryVariables}
customActions={schema.customActions}
/>
Expand All @@ -58,19 +76,28 @@
</Form.Item>
);
} else {
const gqlQuery = schema.gqlAssociationProps.gqlListQuery;
const gqlQuery: any | ((parentRecord: any) => any) =
schema.gqlAssociationProps.gqlListQuery;
const getGqlQueryVariables = gqlQuery?.getQueryVariables;
let gqlQueryVariables = undefined;

// Getting record
let record;
try {
record = form.getFieldValue(fieldPath.pop().keyPath);
} catch (_e: any) {
// ignore
}
if (!record) {
record = parentRecord;
}
// Setting query value
const gqlDocumentQuery =
typeof gqlQuery?.query === 'function'
? gqlQuery?.query(record, useSelector)
: gqlQuery?.query;

if (getGqlQueryVariables) {
let record;
try {
record = form.getFieldValue(fieldPath.pop().keyPath);
} catch (_e: any) {
// ignore
}
if (!record) {
record = parentRecord;
}
gqlQueryVariables = getGqlQueryVariables(record, useSelector);
}
return (
Expand All @@ -81,7 +108,7 @@
selectable={SelectionType.MULTIPLE}
parentIdFieldName={parentIdFieldName!}
associatedIdFieldName={associatedIdFieldName!}
gqlQuery={gqlQuery?.query}
gqlQuery={gqlDocumentQuery}
gqlQueryVariables={gqlQueryVariables}
parentRecord={parentRecord}
associatedRecordClass={schema.dtoClass!}
Expand Down
43 changes: 29 additions & 14 deletions src/components/form/nested-object-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,35 @@
const parentIdFieldName = schema.gqlAssociationProps.parentIdFieldName;
const associatedIdFieldName =
schema.gqlAssociationProps.associatedIdFieldName;
const gqlQuery = schema.gqlAssociationProps.gqlQuery;
const gqlListQuery = schema.gqlAssociationProps.gqlListQuery;
let gqlQuery: any | ((parentRecord: any) => any) =

Check failure on line 32 in src/components/form/nested-object-field.tsx

View workflow job for this annotation

GitHub Actions / Linter (18)

'gqlQuery' is never reassigned. Use 'const' instead
schema.gqlAssociationProps.gqlQuery;
let gqlListQuery: any | ((parentRecord: any) => any) =

Check failure on line 34 in src/components/form/nested-object-field.tsx

View workflow job for this annotation

GitHub Actions / Linter (18)

'gqlListQuery' is never reassigned. Use 'const' instead
schema.gqlAssociationProps.gqlListQuery;
const getGqlQueryVariables = gqlListQuery?.getQueryVariables;
let gqlQueryVariables = undefined;

// Getting record
let record;
try {
record = form.getFieldValue(fieldPath.pop().keyPath);
} catch (_e: any) {
// ignore
}
if (!record) {
record = parentRecord;
}

// Setting query value
const gqlDocumentQuery =
typeof gqlQuery?.query === 'function'
? gqlQuery?.query(record, useSelector)
: gqlQuery?.query;
const gqlDocumentListQuery =
typeof gqlListQuery?.query === 'function'
? gqlListQuery?.query(record, useSelector)
: gqlListQuery?.query;

if (getGqlQueryVariables) {
let record;
try {
record = form.getFieldValue(fieldPath.pop().keyPath);
} catch (_e: any) {
// ignore
}
if (!record) {
record = parentRecord;
}
gqlQueryVariables = getGqlQueryVariables(record, useSelector);
}
if (disabled) {
Expand All @@ -52,8 +67,8 @@
associatedRecordClass={schema.dtoClass!}
parentIdFieldName={parentIdFieldName!}
associatedIdFieldName={associatedIdFieldName!}
gqlQuery={gqlQuery?.query}
gqlListQuery={gqlListQuery?.query}
gqlQuery={gqlDocumentQuery}
gqlListQuery={gqlDocumentListQuery}
/>
);
} else {
Expand All @@ -77,7 +92,7 @@
fieldPath={fieldPath}
parentIdFieldName={parentIdFieldName!}
associatedIdFieldName={associatedIdFieldName!}
gqlQuery={gqlListQuery?.query}
gqlQuery={gqlDocumentListQuery}
gqlQueryVariables={gqlQueryVariables}
parentRecord={parentRecord}
associatedRecordClass={schema.dtoClass!}
Expand Down
190 changes: 190 additions & 0 deletions src/message/get-monitoring-report/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import { Form } from 'antd';
import { GenericForm } from '../../components/form';
import { ChargingStation } from 'src/pages/charging-stations/ChargingStation';
import { IsArray, IsEnum, IsNotEmpty, IsNumber } from 'class-validator';
import { plainToInstance, Type } from 'class-transformer';
import { OCPP2_0_1 } from '@citrineos/base';
import { GqlAssociation } from '@util/decorators/GqlAssociation';
import {
Component,
ComponentProps,
} from '../../pages/variable-attributes/components/Component';
import {
Variable,
VariableProps,
} from '../../pages/variable-attributes/variables/Variable';
import { ClassCustomConstructor } from '@util/decorators/ClassCustomConstructor';
import { NEW_IDENTIFIER } from '@util/consts';
import {
VARIABLE_LIST_BY_COMPONENT_QUERY,
VARIABLE_LIST_QUERY,
} from '../../pages/variable-attributes/variables/queries';
import {
COMPONENT_LIST_BY_VARIABLE_QUERY,
COMPONENT_LIST_QUERY,
} from '../../pages/variable-attributes/components/queries';
import { triggerMessageAndHandleResponse } from '../util';
import { MessageConfirmation } from '../MessageConfirmation';

export interface GetMonitoringReportProps {
station: ChargingStation;
}

enum GetMonitoringReportRequestProps {
// customData = 'customData', // todo
componentVariable = 'componentVariable',
requestId = 'requestId',
monitoringCriteria = 'monitoringCriteria',
}

enum ComponentVariableProps {
component = 'component',
variable = 'variable',
}

const ComponentVariableCustomConstructor = () => {
const variable = new Variable();
const component = new Component();
variable[VariableProps.id] = NEW_IDENTIFIER as unknown as number;
component[ComponentProps.id] = NEW_IDENTIFIER as unknown as number;
const componentVariable = new ComponentVariable();
componentVariable[ComponentVariableProps.component] = component;
componentVariable[ComponentVariableProps.variable] = variable;
return componentVariable;
};

@ClassCustomConstructor(ComponentVariableCustomConstructor)
export class ComponentVariable {
@Type(() => Component)
@IsNotEmpty()
@GqlAssociation({
parentIdFieldName: ComponentVariableProps.component,
associatedIdFieldName: ComponentProps.id,
gqlQuery: {
query: COMPONENT_LIST_QUERY,
},
gqlListQuery: {
query: (record: ComponentVariable) => {
if (record.variable?.id != (NEW_IDENTIFIER as unknown as number)) {
return COMPONENT_LIST_BY_VARIABLE_QUERY;
} else {
return COMPONENT_LIST_QUERY;
}
},
getQueryVariables: (record: ComponentVariable) => {
if (record.variable?.id != (NEW_IDENTIFIER as unknown as number)) {
return {
variableId: record.variable?.id,
};
} else {
return {};
}
},
},
})
component!: Component;

@Type(() => Variable)
@IsNotEmpty()
@GqlAssociation({
parentIdFieldName: ComponentVariableProps.variable,
associatedIdFieldName: VariableProps.id,
gqlQuery: {
query: VARIABLE_LIST_QUERY,
},
gqlListQuery: {
query: (record: ComponentVariable) => {
if (record.component?.id != (NEW_IDENTIFIER as unknown as number)) {
return VARIABLE_LIST_BY_COMPONENT_QUERY;
} else {
return VARIABLE_LIST_QUERY;
}
},
getQueryVariables: (record: ComponentVariable) => {
if (record.component?.id != (NEW_IDENTIFIER as unknown as number)) {
return {
componentId: record.component?.id,
mutability: 'ReadOnly',
};
} else {
return {};
}
},
},
})
variable!: Variable;
}

export class GetMonitoringReportRequest {
// todo
// @Type(() => CustomDataType)
// @ValidateNested()
// customData?: CustomDataType;

@Type(() => ComponentVariable)
@IsNotEmpty()
@IsArray()
componentVariable!: ComponentVariable[] | null;

@IsNotEmpty()
@IsNumber()
requestId!: number | null;

@IsEnum(OCPP2_0_1.MonitoringCriterionEnumType)
@IsNotEmpty()
monitoringCriteria!: OCPP2_0_1.MonitoringCriterionEnumType;
}

export const GetMonitoringReport: React.FC<GetMonitoringReportProps> = ({
station,
}) => {
const [form] = Form.useForm();
const formProps = {
form,
};

const getMonitoringReportRequest = new GetMonitoringReportRequest();
getMonitoringReportRequest[
GetMonitoringReportRequestProps.componentVariable
] = [ComponentVariableCustomConstructor()];

const handleSubmit = async (plainValues: any) => {
const classInstance = plainToInstance(
GetMonitoringReportRequest,
plainValues,
);

let data: any = {};

if (
classInstance &&
classInstance[GetMonitoringReportRequestProps.componentVariable]
) {
data = {
requestId: classInstance[GetMonitoringReportRequestProps.requestId],
monitoringCriteria:
classInstance[GetMonitoringReportRequestProps.monitoringCriteria],
componentVariable:
classInstance[GetMonitoringReportRequestProps.componentVariable],
};
}

await triggerMessageAndHandleResponse({
url: `/reporting/getMonitoringReport?identifier=${station.id}&tenantId=1`,
responseClass: MessageConfirmation,
data: data,
responseSuccessCheck: (response: MessageConfirmation) =>
response && response.success,
});
};

return (
<GenericForm
formProps={formProps}
dtoClass={GetMonitoringReportRequest}
onFinish={handleSubmit}
initialValues={getMonitoringReportRequest}
parentRecord={getMonitoringReportRequest}
/>
);
};
Loading
Loading