@adaptabletools/adaptable
Version:
Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
165 lines (164 loc) • 5.75 kB
JavaScript
import { EMPTY_ARRAY } from '../../Utilities/Constants/GeneralConstants';
import ObjectFactory from '../../Utilities/ObjectFactory';
import { AnnotationsService } from '../../Utilities/Services/AnnotationsService';
/**
* @ReduxAction A Comments has been added
*/
export const COMMENTS_ADD = 'COMMENTS_ADD';
/**
* @ReduxAction Cell Comments has been added
*/
export const COMMENTS_CELL_ADD = 'COMMENTS_CELL_ADD';
/**
* @ReduxAction Cell comments have been removed
*/
export const COMMENTS_CELL_DELETE = 'COMMENTS_CELL_DELETE';
/**
* @ReduxAction A Comments has been edited
*/
export const COMMENTS_EDIT = 'COMMENTS_EDIT';
/**
* @ReduxAction A Comments has been deleted
*/
export const COMMENTS_DELETE = 'COMMENTS_DELETE';
/**
* @ReduxAction Comments Module is ready
*/
export const COMMENTS_READY = 'COMMENTS_READY';
export const COMMENTS_LOAD = 'COMMENTS_LOAD';
export const CommentsSet = ({ commentThread, }) => ({
type: COMMENTS_LOAD,
commentThread,
});
export const CommentsAdd = ({ comment, cellAddress, }) => ({
type: COMMENTS_ADD,
comment,
cellAddress,
});
export const CommentsCellAdd = ({ commentThread, }) => ({
type: COMMENTS_CELL_ADD,
commentThread,
});
export const CommentsCellDelete = ({ cellAddress: cellAddress, }) => ({
type: COMMENTS_CELL_DELETE,
cellAddress: cellAddress,
});
export const CommentsEdit = ({ comment, cellAddress, }) => ({
type: COMMENTS_EDIT,
comment,
cellAddress,
});
export const CommentsDelete = ({ commentUuid, cellAddress, }) => ({
type: COMMENTS_DELETE,
cellAddress,
commentUuid,
});
export const GetCellCommentSelector = (state, address) => {
if (!address) {
return undefined;
}
return (state?.CommentThreads ?? []).find((commentThread) => {
return AnnotationsService.isSameAddress(commentThread, address);
});
};
const initialState = {
CommentThreads: EMPTY_ARRAY,
};
export const CommentsReducer = (state = initialState, action) => {
switch (action.type) {
case COMMENTS_ADD: {
const addAction = action;
const commentThread = state.CommentThreads ?? [];
const cellCommentIndex = commentThread.findIndex((cellComments) => {
return AnnotationsService.isSameAddress(cellComments, addAction.cellAddress);
});
let comments = [...commentThread];
if (cellCommentIndex === -1) {
// new comments
comments.push({
...ObjectFactory.CreateEmptyCommentThread(addAction.cellAddress),
Comments: [addAction.comment],
});
}
else {
const existing = {
...commentThread[cellCommentIndex],
Comments: [...commentThread[cellCommentIndex].Comments, addAction.comment],
};
comments[cellCommentIndex] = existing;
}
return { ...state, CommentThreads: comments };
}
case COMMENTS_CELL_ADD: {
return {
...state,
CommentThreads: [
...(state.CommentThreads ?? []),
action.commentThread,
],
};
}
case COMMENTS_CELL_DELETE: {
const deleteAction = action;
return {
...state,
CommentThreads: (state.CommentThreads ?? []).filter((cellComments) => !AnnotationsService.isSameAddress(cellComments, deleteAction.cellAddress)),
};
}
case COMMENTS_EDIT: {
const editAction = action;
let edited = false;
const adaptableCellComments = (state.CommentThreads ?? []).map((cellComments) => {
if (AnnotationsService.isSameAddress(cellComments, editAction.cellAddress)) {
// found the place
edited = true;
return {
...cellComments,
Comments: cellComments.Comments.map((comment) => {
if (comment.Uuid === editAction.comment.Uuid) {
return editAction.comment;
}
return comment;
}),
};
}
});
return {
...state,
CommentThreads: adaptableCellComments,
};
}
case COMMENTS_DELETE: {
const deleteAction = action;
const adaptableCellComments = (state.CommentThreads ?? [])
.map((cellComments) => {
if (AnnotationsService.isSameAddress(cellComments, deleteAction.cellAddress)) {
// found the place
const newComments = cellComments.Comments.filter((comment) => comment.Uuid !== deleteAction.commentUuid);
if (newComments.length === 0) {
return null;
}
const newCellComments = {
...cellComments,
Comments: newComments,
};
return newCellComments;
}
return cellComments;
})
.filter((cellComments) => cellComments !== null);
return {
...state,
CommentThreads: adaptableCellComments,
};
}
case COMMENTS_LOAD: {
return {
...state,
CommentThreads: action.commentThread,
};
}
default:
return state;
}
};