--- /dev/null
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import * as _ from "./resource-properties";
+import { omit } from "lodash";
+
+describe("Resource properties lib", () => {
+
+ let properties: any;
+
+ beforeEach(() => {
+ properties = {
+ animal: 'dog',
+ color: ['brown', 'black'],
+ name: ['Toby']
+ }
+ })
+
+ it("should convert a single string value into a list when adding values", () => {
+ expect(
+ _.addProperty(properties, 'animal', 'cat')
+ ).toEqual({
+ ...properties, animal: ['dog', 'cat']
+ });
+ });
+
+ it("should convert a 2 value list into a string when removing values", () => {
+ expect(
+ _.deleteProperty(properties, 'color', 'brown')
+ ).toEqual({
+ ...properties, color: 'black'
+ });
+ });
+
+ it("shouldn't add duplicated key:value items", () => {
+ expect(
+ _.addProperty(properties, 'animal', 'dog')
+ ).toEqual(properties);
+ });
+
+ it("should remove the key when deleting from a one value list", () => {
+ expect(
+ _.deleteProperty(properties, 'name', 'Toby')
+ ).toEqual(omit(properties, 'name'));
+ });
+
+ it("should return the same when deleting non-existant value", () => {
+ expect(
+ _.deleteProperty(properties, 'animal', 'dolphin')
+ ).toEqual(properties);
+ });
+
+ it("should return the same when deleting non-existant key", () => {
+ expect(
+ _.deleteProperty(properties, 'doesntexist', 'something')
+ ).toEqual(properties);
+ });
+});
\ No newline at end of file
--- /dev/null
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+export const deleteProperty = (properties: any, key: string, value: string) => {
+ if (Array.isArray(properties[key])) {
+ properties[key] = properties[key].filter((v: string) => v !== value);
+ if (properties[key].length === 1) {
+ properties[key] = properties[key][0];
+ } else if (properties[key].length === 0) {
+ delete properties[key];
+ }
+ } else if (properties[key] === value) {
+ delete properties[key];
+ }
+ return properties;
+}
+
+export const addProperty = (properties: any, key: string, value: string) => {
+ if (properties[key]) {
+ if (Array.isArray(properties[key])) {
+ properties[key] = [...properties[key], value];
+ } else {
+ properties[key] = [properties[key], value];
+ }
+ // Remove potential duplicate and save as single value if needed
+ properties[key] = Array.from(new Set(properties[key]));
+ if (properties[key].length === 1) {
+ properties[key] = properties[key][0];
+ }
+ } else {
+ properties[key] = value;
+ }
+ return properties;
+}
\ No newline at end of file
).toEqual(`["etag","ilike","%etagValue%"]`);
});
+ it("should add 'contains' rule", () => {
+ expect(
+ filters.addContains("properties.someProp", "someValue").getFilters()
+ ).toEqual(`["properties.someProp","contains","someValue"]`);
+ });
+
it("should add 'is_a' rule", () => {
expect(
filters.addIsA("etag", "etagValue").getFilters()
return this.addCondition(field, "ilike", value, "%", "%", resourcePrefix);
}
+ public addContains(field: string, value?: string, resourcePrefix?: string) {
+ return this.addCondition(field, "contains", value, "", "", resourcePrefix);
+ }
+
public addIsA(field: string, value?: string | string[], resourcePrefix?: string) {
return this.addCondition(field, "is_a", value, "", "", resourcePrefix);
}
import { SnackbarKind } from '~/store/snackbar/snackbar-actions';
import { navigateTo } from '~/store/navigation/navigation-action';
import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
+import { deleteProperty, addProperty } from "~/lib/resource-properties";
export const collectionPanelActions = unionize({
SET_COLLECTION: ofType<CollectionResource>(),
const uuid = item ? item.uuid : '';
try {
if (item) {
+ const key = data.keyID || data.key;
+ const value = data.valueID || data.value;
+ item.properties = addProperty(item.properties, key, value);
const updatedCollection = await services.collectionService.update(
uuid, {
- properties: {
- ...JSON.parse(JSON.stringify(item.properties)),
- [data.keyID || data.key]: data.valueID || data.value
- }
+ properties: {...item.properties}
}
);
item.properties = updatedCollection.properties;
}
};
-export const deleteCollectionTag = (key: string) =>
+export const deleteCollectionTag = (key: string, value: string) =>
async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
const item = getState().collectionPanel.item;
const uuid = item ? item.uuid : '';
try {
if (item) {
- delete item.properties[key];
+ item.properties = deleteProperty(item.properties, key, value);
+
const updatedCollection = await services.collectionService.update(
uuid, {
properties: {...item.properties}
import { startSubmit, stopSubmit } from 'redux-form';
import { resourcesActions } from '~/store/resources/resources-actions';
import {snackbarActions, SnackbarKind} from '~/store/snackbar/snackbar-actions';
+import { addProperty, deleteProperty } from '~/lib/resource-properties';
export const SLIDE_TIMEOUT = 500;
dispatch<any>(dialogActions.OPEN_DIALOG({ id: PROJECT_PROPERTIES_DIALOG_NAME, data: { } }));
};
-export const deleteProjectProperty = (key: string) =>
+export const deleteProjectProperty = (key: string, value: string) =>
async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
const { detailsPanel, resources } = getState();
const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
try {
if (project) {
- delete project.properties[key];
+ project.properties = deleteProperty(project.properties, key, value);
const updatedProject = await services.projectService.update(project.uuid, { properties: project.properties });
dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
dispatch(startSubmit(PROJECT_PROPERTIES_FORM_NAME));
try {
if (project) {
+ const key = data.keyID || data.key;
+ const value = data.valueID || data.value;
+ project.properties = addProperty(project.properties, key, value);
const updatedProject = await services.projectService.update(
project.uuid, {
- properties: {
- ...JSON.parse(JSON.stringify(project.properties)),
- [data.keyID || data.key]: data.valueID || data.value
- }
+ properties: {...project.properties}
}
);
dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
const v = data[key];
if (data.hasOwnProperty(key)) {
- const pattern = v === false
- ? `${field.replace(':', '\\:\\s*')}\\s*`
- : `${field.replace(':', '\\:\\s*')}\\:\\s*"[\\w|\\#|\\-|\\/]*"\\s*`;
+ let pattern: string;
+ if (v === false) {
+ pattern = `${field.replace(':', '\\:\\s*')}\\s*`;
+ } else if (key.startsWith('prop-')) {
+ // On properties, only remove key:value duplicates, allowing
+ // multiple properties with the same key.
+ pattern = `${field.replace(':', '\\:\\s*')}\\:\\s*${v}\\s*`;
+ } else {
+ pattern = `${field.replace(':', '\\:\\s*')}\\:\\s*[\\w|\\#|\\-|\\/]*\\s*`;
+ }
value = value.replace(new RegExp(pattern), '');
}
data.properties.forEach(p => {
if (p.value) {
filter
- .addILike(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROJECT)
- .addILike(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.COLLECTION);
+ .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROJECT)
+ .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.COLLECTION);
}
filter.addExists(p.key);
});
import { withStyles, StyleRulesCallback, WithStyles } from '@material-ui/core';
import { ArvadosTheme } from '~/common/custom-theme';
import { Dispatch } from 'redux';
-import { PropertyChipComponent } from '../resource-properties-form/property-chip';
+import { getPropertyChip } from '../resource-properties-form/property-chip';
export class ProjectDetails extends DetailsData<ProjectResource> {
getIcon(className?: string) {
</DetailsAttribute>
{
Object.keys(project.properties).map(k =>
- <PropertyChipComponent key={k}
- propKey={k} propValue={project.properties[k]}
- className={classes.tag} />
+ Array.isArray(project.properties[k])
+ ? project.properties[k].map((v: string) =>
+ getPropertyChip(k, v, undefined, classes.tag))
+ : getPropertyChip(k, project.properties[k], undefined, classes.tag)
)
}
</div>
import { ArvadosTheme } from '~/common/custom-theme';
import { ProjectPropertiesForm } from '~/views-components/project-properties-dialog/project-properties-form';
import { getResource } from '~/store/resources/resources';
-import { PropertyChipComponent } from "../resource-properties-form/property-chip";
+import { getPropertyChip } from "../resource-properties-form/property-chip";
type CssRules = 'tag';
}
interface ProjectPropertiesDialogActionProps {
- handleDelete: (key: string) => void;
+ handleDelete: (key: string, value: string) => void;
}
const mapStateToProps = ({ detailsPanel, resources, properties }: RootState): ProjectPropertiesDialogDataProps => ({
});
const mapDispatchToProps = (dispatch: Dispatch): ProjectPropertiesDialogActionProps => ({
- handleDelete: (key: string) => dispatch<any>(deleteProjectProperty(key)),
+ handleDelete: (key: string, value: string) => () => dispatch<any>(deleteProjectProperty(key, value)),
});
type ProjectPropertiesDialogProps = ProjectPropertiesDialogDataProps & ProjectPropertiesDialogActionProps & WithDialogProps<{}> & WithStyles<CssRules>;
<ProjectPropertiesForm />
{project && project.properties &&
Object.keys(project.properties).map(k =>
- <PropertyChipComponent
- onDelete={() => handleDelete(k)}
- key={k} className={classes.tag}
- propKey={k} propValue={project.properties[k]} />)
+ Array.isArray(project.properties[k])
+ ? project.properties[k].map((v: string) =>
+ getPropertyChip(
+ k, v,
+ handleDelete(k, v),
+ classes.tag))
+ : getPropertyChip(
+ k, project.properties[k],
+ handleDelete(k, project.properties[k]),
+ classes.tag)
+ )
}
</DialogContent>
<DialogActions>
);
}
);
+
+export const getPropertyChip = (k:string, v:string, handleDelete:any, className:string) =>
+ <PropertyChipComponent
+ key={k} className={className}
+ onDelete={handleDelete}
+ propKey={k} propValue={v} />;
setProp: (propertyValue: PropertyValue, properties: PropertyValue[]) => {
dispatch<any>(changeAdvancedFormProperty(
'properties',
- [...properties.filter(e => e.keyID! !== propertyValue.keyID!), propertyValue]
+ [...properties, propertyValue]
));
dispatch<any>(resetAdvancedFormProperty('key'));
dispatch<any>(resetAdvancedFormProperty('value'));
import { formatFileSize } from "~/common/formatters";
import { openDetailsPanel } from '~/store/details-panel/details-panel-action';
import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
-import { PropertyChipComponent } from '~/views-components/resource-properties-form/property-chip';
+import { getPropertyChip } from '~/views-components/resource-properties-form/property-chip';
import { IllegalNamingWarning } from '~/components/warning/warning';
type CssRules = 'card' | 'iconHeader' | 'tag' | 'label' | 'value' | 'link';
</Grid>
<Grid item xs={12}>
{Object.keys(item.properties).map(k =>
- <PropertyChipComponent
- key={k} className={classes.tag}
- onDelete={this.handleDelete(k)}
- propKey={k} propValue={item.properties[k]} />
+ Array.isArray(item.properties[k])
+ ? item.properties[k].map((v: string) =>
+ getPropertyChip(
+ k, v,
+ this.handleDelete(k, v),
+ classes.tag))
+ : getPropertyChip(
+ k, item.properties[k],
+ this.handleDelete(k, item.properties[k]),
+ classes.tag)
)}
</Grid>
</Grid>
kind: SnackbarKind.SUCCESS
}))
- handleDelete = (key: string) => () => {
- this.props.dispatch<any>(deleteCollectionTag(key));
+ handleDelete = (key: string, value: string) => () => {
+ this.props.dispatch<any>(deleteCollectionTag(key, value));
}
openCollectionDetails = () => {