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

AJ-954 - Add capability to edit a cell in Data table view in Azure #4588

Merged
merged 25 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
14 changes: 14 additions & 0 deletions src/libs/ajax/WorkspaceDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash/fp';
import { authOpts, fetchWDS, jsonBody } from 'src/libs/ajax/ajax-common';
import {
RecordQueryResponse,
RecordResponseBody,
RecordTypeSchema,
SearchRequest,
TsvUploadResponse,
Expand Down Expand Up @@ -132,6 +133,19 @@ export const WorkspaceData = (signal) => ({
);
return res.json();
},
updateRecord: async (
root: string,
instanceId: string,
recordType: string,
recordId: string,
record: { [attribute: string]: any }
): Promise<RecordResponseBody> => {
const res = await fetchWDS(root)(
`${instanceId}/records/v0.2/${recordType}/${recordId}`,
_.mergeAll([authOpts(), jsonBody(record), { signal, method: 'PATCH' }])
);
return res.json();
},
getVersion: async (root: string): Promise<WDSVersionResponse> => {
const res = await fetchWDS(root)('version', _.merge(authOpts(), { signal }));
return res.json();
Expand Down
8 changes: 8 additions & 0 deletions src/libs/ajax/data-table-providers/DataTableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export type UploadParameters = {
recordType: string;
};

export type RecordEditParameters = {
instance: string;
recordName: string;
recordId: string;
record: { [attribute: string]: any };
};

export type InvalidTsvOptions = {
fileImportModeMatches: boolean;
filePresent: boolean;
Expand Down Expand Up @@ -117,6 +124,7 @@ export interface DataTableFeatures {
supportsTypeRenaming: boolean;
supportsEntityRenaming: boolean;
supportsEntityUpdating: boolean;
supportEntityUpdatingTypes?: string[];
yuliadub marked this conversation as resolved.
Show resolved Hide resolved
supportsAttributeRenaming: boolean;
supportsAttributeDeleting: boolean;
supportsAttributeClearing: boolean;
Expand Down
40 changes: 40 additions & 0 deletions src/libs/ajax/data-table-providers/WdsDataTableProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,26 @@ describe('WdsDataTableProvider', () => {
return Promise.resolve({ message: 'Upload Succeeded', recordsModified: 1 });
};

const updateRecordMockImpl: WorkspaceDataContract['updateRecord'] = (
_root: string,
_instanceId: string,
_recordType: string,
_recordId: string,
_record: { [attribute: string]: any }
) => {
const expected: RecordAttributes = {
something: 123,
testAttr: 'string_value',
};
return Promise.resolve({ id: 'record1', type: 'test', attributes: expected });
};

const listAppsV2MockImpl = (_workspaceId: string): Promise<ListAppItem[]> => {
return Promise.resolve(testProxyUrlResponse);
};

let getRecords: jest.MockedFunction<WorkspaceDataContract['getRecords']>;
let updateRecord: jest.MockedFunction<WorkspaceDataContract['updateRecord']>;
let getCapabilities: jest.MockedFunction<WorkspaceDataContract['getCapabilities']>;
let deleteTable: jest.MockedFunction<WorkspaceDataContract['deleteTable']>;
let downloadTsv: jest.MockedFunction<WorkspaceDataContract['downloadTsv']>;
Expand All @@ -185,6 +200,7 @@ describe('WdsDataTableProvider', () => {

beforeEach(() => {
getRecords = jest.fn().mockImplementation(getRecordsMockImpl);
updateRecord = jest.fn().mockImplementation(updateRecordMockImpl);
getCapabilities = jest.fn().mockImplementation(getCapabilitiesMockImpl);
deleteTable = jest.fn().mockImplementation(deleteTableMockImpl);
downloadTsv = jest.fn().mockImplementation(downloadTsvMockImpl);
Expand All @@ -196,6 +212,7 @@ describe('WdsDataTableProvider', () => {
({
WorkspaceData: {
getRecords,
updateRecord,
getCapabilities,
deleteTable,
downloadTsv,
Expand Down Expand Up @@ -691,6 +708,29 @@ describe('WdsDataTableProvider', () => {
});
});

describe('updateRecord', () => {
it('restructures a WDS response', async () => {
// Arrange
const provider = new TestableWdsProvider();

const expected: RecordAttributes = {
something: 123,
testAttr: 'string_value',
};

// Act
const actual = await provider.updateRecord({
instance: uuid,
recordName: 'test',
recordId: 'record1',
record: expected,
});

// Assert
expect(actual.attributes).toStrictEqual(expected);
});
});

describe('transformMetadata', () => {
it('restructures a WDS response', () => {
// Arrange
Expand Down
22 changes: 21 additions & 1 deletion src/libs/ajax/data-table-providers/WdsDataTableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EntityMetadata,
EntityQueryOptions,
EntityQueryResponse,
RecordEditParameters,
TSVFeatures,
TsvUploadButtonDisabledOptions,
TsvUploadButtonTooltipOptions,
Expand Down Expand Up @@ -58,6 +59,12 @@ export interface TsvUploadResponse {
recordsModified: number;
}

export interface RecordResponseBody {
id: string;
type: string;
attributes: object;
yuliadub marked this conversation as resolved.
Show resolved Hide resolved
}

export const wdsToEntityServiceMetadata = (wdsSchema: RecordTypeSchema[]): EntityMetadata => {
const keyedSchema: Record<string, RecordTypeSchema> = _.keyBy((x) => x.name, wdsSchema);
return _.mapValues((typeDef) => {
Expand Down Expand Up @@ -156,7 +163,8 @@ export class WdsDataTableProvider implements DataTableProvider {
supportsTypeDeletion: true,
supportsTypeRenaming: false,
supportsEntityRenaming: false,
supportsEntityUpdating: false, // TODO: enable as part of AJ-594
supportsEntityUpdating: true,
yuliadub marked this conversation as resolved.
Show resolved Hide resolved
supportEntityUpdatingTypes: ['string', 'number', 'boolean', 'json'], // remove this as part of AJ-<need to create ticket> for other types
supportsAttributeRenaming: false, // TODO: enable as part of AJ-1278, requires `edit.renameAttribute` capability
supportsAttributeDeleting: this.isCapabilityEnabled('edit.deleteAttribute'),
supportsAttributeClearing: false,
Expand Down Expand Up @@ -323,4 +331,16 @@ export class WdsDataTableProvider implements DataTableProvider {
uploadParams.file
);
};

updateRecord = (recordEditParams: RecordEditParameters): Promise<RecordResponseBody> => {
if (!this.proxyUrl) return Promise.reject('Proxy Url not loaded');

return Ajax().WorkspaceData.updateRecord(
this.proxyUrl,
recordEditParams.instance,
recordEditParams.recordName,
recordEditParams.recordId,
recordEditParams.record
);
};
}
69 changes: 1 addition & 68 deletions src/workspace-data/data-table/entity-service/AttributeInput.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import ReactJson from '@microlink/react-json-view';
import { Switch } from '@terra-ui-packages/components';
import _ from 'lodash/fp';
import { Fragment, useEffect, useRef, useState } from 'react';
import { div, fieldset, h, label, legend, span } from 'react-hyperscript-helpers';
import { IdContainer, LabeledCheckbox, Link, RadioButton, Select } from 'src/components/common';
import { icon } from 'src/components/icons';
import { NumberInput, TextInput } from 'src/components/input';
import TooltipTrigger from 'src/components/TooltipTrigger';
import * as Utils from 'src/libs/utils';

import { renderInputForAttributeType } from '../shared/AttributeInput';
import { convertAttributeValue, getAttributeType } from './attribute-utils';

export const AttributeTypeInput = ({
Expand Down Expand Up @@ -83,71 +81,6 @@ export const AttributeTypeInput = ({
]);
};

const renderInputForAttributeType = _.curry((attributeType, props) => {
return Utils.switchCase(
attributeType,
[
'string',
() => {
const { value = '', ...otherProps } = props;
return h(TextInput, {
autoFocus: true,
placeholder: 'Enter a value',
value,
...otherProps,
});
},
],
[
'reference',
() => {
const { value, onChange, ...otherProps } = props;
return h(TextInput, {
autoFocus: true,
placeholder: `Enter a ${value.entityType}_id`,
value: value.entityName,
onChange: (v) => onChange({ ...value, entityName: v }),
...otherProps,
});
},
],
[
'number',
() => {
const { value = 0, ...otherProps } = props;
return h(NumberInput, { autoFocus: true, isClearable: false, value, ...otherProps });
},
],
[
'boolean',
() => {
const { value = false, ...otherProps } = props;
return div({ style: { flexGrow: 1, display: 'flex', alignItems: 'center', height: '2.25rem' } }, [
h(Switch, { checked: value, ...otherProps }),
]);
},
],
[
'json',
() => {
const { value, onChange, ...otherProps } = props;
return h(ReactJson, {
...otherProps,
style: { ...otherProps.style, whiteSpace: 'pre-wrap' },
src: value,
displayObjectSize: false,
displayDataTypes: false,
enableClipboard: false,
name: false,
onAdd: _.flow(_.get('updated_src'), onChange),
onDelete: _.flow(_.get('updated_src'), onChange),
onEdit: _.flow(_.get('updated_src'), onChange),
});
},
]
);
});

const defaultValueForAttributeType = (attributeType, referenceEntityType) => {
return Utils.switchCase(
attributeType,
Expand Down
72 changes: 72 additions & 0 deletions src/workspace-data/data-table/shared/AttributeInput.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really nice that this could be shared between entity service and WDS tables.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import ReactJson from '@microlink/react-json-view';
import { Switch } from '@terra-ui-packages/components';
import _ from 'lodash/fp';
import { div, h } from 'react-hyperscript-helpers';
import { NumberInput, TextInput } from 'src/components/input';
import * as Utils from 'src/libs/utils';

// can be pulled into shared
yuliadub marked this conversation as resolved.
Show resolved Hide resolved
export const renderInputForAttributeType = _.curry((attributeType, props) => {
return Utils.switchCase(
attributeType,
[
'string',
() => {
const { value = '', ...otherProps } = props;
return h(TextInput, {
autoFocus: true,
placeholder: 'Enter a value',
value,
...otherProps,
});
},
],
[
'reference',
() => {
const { value, onChange, ...otherProps } = props;
return h(TextInput, {
autoFocus: true,
placeholder: `Enter a ${value.entityType}_id`,
value: value.entityName,
onChange: (v) => onChange({ ...value, entityName: v }),
...otherProps,
});
},
],
[
'number',
() => {
const { value = 0, ...otherProps } = props;
return h(NumberInput, { autoFocus: true, isClearable: false, value, ...otherProps });
},
],
[
'boolean',
() => {
const { value = false, ...otherProps } = props;
return div({ style: { flexGrow: 1, display: 'flex', alignItems: 'center', height: '2.25rem' } }, [
h(Switch, { checked: value, ...otherProps }),
]);
},
],
[
'json',
() => {
const { value, onChange, ...otherProps } = props;
return h(ReactJson, {
...otherProps,
style: { ...otherProps.style, whiteSpace: 'pre-wrap' },
src: value,
displayObjectSize: false,
displayDataTypes: false,
enableClipboard: false,
name: false,
onAdd: _.flow(_.get('updated_src'), onChange),
onDelete: _.flow(_.get('updated_src'), onChange),
onEdit: _.flow(_.get('updated_src'), onChange),
});
},
]
);
});
Loading
Loading