import { createTree } from "~/models/tree";
import { RootState } from "../store";
import { ServiceRepository } from "~/services/services";
-import { TagResource, TagProperty } from "~/models/tag";
+import { TagProperty } from "~/models/tag";
import { snackbarActions } from "../snackbar/snackbar-actions";
import { resourcesActions } from "~/store/resources/resources-actions";
import { unionize, ofType, UnionOf } from '~/common/unionize';
export const collectionPanelActions = unionize({
LOAD_COLLECTION: ofType<{ uuid: string }>(),
- LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>(),
- LOAD_COLLECTION_TAGS: ofType<{ uuid: string }>(),
- LOAD_COLLECTION_TAGS_SUCCESS: ofType<{ tags: TagResource[] }>(),
- CREATE_COLLECTION_TAG: ofType<{ data: any }>(),
- CREATE_COLLECTION_TAG_SUCCESS: ofType<{ tag: TagResource }>(),
- DELETE_COLLECTION_TAG: ofType<{ uuid: string }>(),
- DELETE_COLLECTION_TAG_SUCCESS: ofType<{ uuid: string }>()
+ LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>()
});
export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection }));
dispatch(resourcesActions.SET_RESOURCES([collection]));
dispatch<any>(loadCollectionFiles(collection.uuid));
- dispatch<any>(loadCollectionTags(collection.uuid));
return collection;
};
-export const loadCollectionTags = (uuid: string) =>
- (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
- dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS({ uuid }));
- return services.tagService
- .list(uuid)
- .then(tags => {
- dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS_SUCCESS({ tags }));
- });
- };
-
-export const createCollectionTag = (data: TagProperty) =>
- (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
- dispatch(collectionPanelActions.CREATE_COLLECTION_TAG({ data }));
+export const createCollectionTag = (data: TagProperty) =>
+ async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
const item = getState().collectionPanel.item;
const uuid = item ? item.uuid : '';
- return services.tagService
- .create(uuid, data)
- .then(tag => {
- dispatch(collectionPanelActions.CREATE_COLLECTION_TAG_SUCCESS({ tag }));
- dispatch(snackbarActions.OPEN_SNACKBAR({
- message: "Tag has been successfully added.",
- hideDuration: 2000
- }));
- });
+ try {
+ if (item) {
+ item.properties[data.key] = data.value;
+ const updatedCollection = await services.collectionService.update(uuid, item);
+ dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
+ dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully added.", hideDuration: 2000 }));
+ return updatedCollection;
+ }
+ return;
+ } catch (e) {
+ return;
+ }
};
-export const deleteCollectionTag = (uuid: string) =>
- (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
- dispatch(collectionPanelActions.DELETE_COLLECTION_TAG({ uuid }));
- return services.linkService
- .delete(uuid)
- .then(tag => {
- dispatch(collectionPanelActions.DELETE_COLLECTION_TAG_SUCCESS({ uuid: tag.uuid }));
- dispatch(snackbarActions.OPEN_SNACKBAR({
- message: "Tag has been successfully deleted.",
- hideDuration: 2000
- }));
- });
+export const deleteCollectionTag = (key: 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];
+ const updatedCollection = await services.collectionService.update(uuid, item);
+ dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
+ dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000 }));
+ return updatedCollection;
+ }
+ return;
+ } catch (e) {
+ return;
+ }
};
import { collectionPanelActions, CollectionPanelAction } from "./collection-panel-action";
import { CollectionResource } from "~/models/collection";
-import { TagResource } from "~/models/tag";
export interface CollectionPanelState {
item: CollectionResource | null;
- tags: TagResource[];
}
const initialState = {
- item: null,
- tags: []
+ item: null
};
export const collectionPanelReducer = (state: CollectionPanelState = initialState, action: CollectionPanelAction) =>
collectionPanelActions.match(action, {
default: () => state,
LOAD_COLLECTION_SUCCESS: ({ item }) => ({ ...state, item }),
- LOAD_COLLECTION_TAGS_SUCCESS: ({ tags }) => ({...state, tags }),
- CREATE_COLLECTION_TAG_SUCCESS: ({ tag }) => ({...state, tags: [...state.tags, tag] }),
- DELETE_COLLECTION_TAG_SUCCESS: ({ uuid }) => ({...state, tags: state.tags.filter(tag => tag.uuid !== uuid) })
});
connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
const collection = getResource(props.match.params.id)(state.resources);
return {
- item: collection,
- tags: state.collectionPanel.tags
+ item: collection
};
})(
class extends React.Component<CollectionPanelProps> {
<Grid item xs={12}><CollectionTagForm /></Grid>
<Grid item xs={12}>
{
- tags.map(tag => {
- return <Chip key={tag.etag} className={classes.tag}
- onDelete={this.handleDelete(tag.uuid)}
- label={renderTagLabel(tag)} />;
+ Object.keys(item.properties).map( key => {
+ return <Chip key={key} className={classes.tag}
+ onDelete={this.handleDelete(key)}
+ label={`${key}: ${item.properties[key]}`} />;
})
}
</Grid>
this.props.dispatch<any>(openContextMenu(event, resource));
}
- handleDelete = (uuid: string) => () => {
- this.props.dispatch<any>(deleteCollectionTag(uuid));
+ handleDelete = (key: string) => () => {
+ this.props.dispatch<any>(deleteCollectionTag(key));
}
onCopy = () => {
}
)
);
-
-const renderTagLabel = (tag: TagResource) => {
- const { properties } = tag;
- return `${properties.key}: ${properties.value}`;
-};