Skip to content

Commit

Permalink
Merge pull request #2817 from udecode/fix/comments-provider-callbacks
Browse files Browse the repository at this point in the history
Fix CommentsProvider callbacks
  • Loading branch information
12joan authored Dec 22, 2023
2 parents e389db2 + bfcee27 commit 133a280
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 182 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-tables-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-comments': patch
---

- Fix the `onCommentAdd`, `onCommentUpdate` and `onCommentDelete` callbacks on CommentsProvider
5 changes: 5 additions & 0 deletions apps/www/src/lib/plate/demo/comments/CommentsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export function CommentsProvider({ children }: { children: ReactNode }) {
comments={commentsData}
users={usersData}
myUserId="1"
/* eslint-disable no-console */
onCommentAdd={(comment) => console.log('Comment added', comment)}
onCommentDelete={(comment) => console.log('Comment deleted', comment)}
onCommentUpdate={(comment) => console.log('Comment updated', comment)}
/* eslint-enable no-console */
>
{children}
</CommentsProviderPrimitive>
Expand Down
2 changes: 1 addition & 1 deletion packages/comments/src/components/CommentDeleteButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { unsetCommentNodesById } from '../utils/index';

export const useCommentDeleteButtonState = () => {
const activeCommentId = useCommentsSelectors().activeCommentId();
const onCommentDelete = useCommentsSelectors().onCommentDelete();
const onCommentDelete = useCommentsSelectors().onCommentDelete()?.fn;
const id = useCommentSelectors().id();
const setActiveCommentId = useCommentsActions().activeCommentId();
const removeComment = useRemoveComment();
Expand Down
2 changes: 1 addition & 1 deletion packages/comments/src/components/CommentEditSaveButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../stores/comments/CommentsProvider';

export const useCommentEditSaveButtonState = () => {
const onCommentUpdate = useCommentsSelectors().onCommentUpdate();
const onCommentUpdate = useCommentsSelectors().onCommentUpdate()?.fn;
const editingValue = useCommentSelectors().editingValue();
const setEditingValue = useCommentActions().editingValue();
const id = useCommentSelectors().id();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../stores/comments/CommentsProvider';

export const useCommentNewSubmitButtonState = () => {
const onCommentAdd = useCommentsSelectors().onCommentAdd();
const onCommentAdd = useCommentsSelectors().onCommentAdd()?.fn;
const activeCommentId = useCommentsSelectors().activeCommentId()!;
const comment = useComment(SCOPE_ACTIVE_COMMENT)!;
const newValue = useCommentsSelectors().newValue();
Expand Down
2 changes: 1 addition & 1 deletion packages/comments/src/components/CommentResolveButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../stores/comments/CommentsProvider';

export const useCommentResolveButton = () => {
const onCommentUpdate = useCommentsSelectors().onCommentUpdate();
const onCommentUpdate = useCommentsSelectors().onCommentUpdate()?.fn;
const activeCommentId = useCommentsSelectors().activeCommentId();
const setActiveCommentId = useCommentsActions().activeCommentId();
const updateComment = useUpdateComment(activeCommentId);
Expand Down
73 changes: 50 additions & 23 deletions packages/comments/src/stores/comments/CommentsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import {
createAtomStore,
getNodeString,
Expand Down Expand Up @@ -35,31 +36,57 @@ export interface CommentsStoreState {

focusTextarea: boolean;

onCommentAdd: ((value: WithPartial<TComment, 'userId'>) => void) | null;
onCommentUpdate:
| ((value: Pick<TComment, 'id'> & Partial<Omit<TComment, 'id'>>) => void)
| null;
onCommentDelete: ((id: string) => void) | null;
onCommentAdd: { fn: (value: WithPartial<TComment, 'userId'>) => void } | null;
onCommentUpdate: {
fn: (value: Pick<TComment, 'id'> & Partial<Omit<TComment, 'id'>>) => void;
} | null;
onCommentDelete: { fn: (id: string) => void } | null;
}

export const { commentsStore, useCommentsStore, CommentsProvider } =
createAtomStore(
{
myUserId: null,
users: {},
comments: {},
activeCommentId: null,
addingCommentId: null,
newValue: [{ type: 'p', children: [{ text: '' }] }],
focusTextarea: false,
onCommentAdd: null,
onCommentUpdate: null,
onCommentDelete: null,
} as CommentsStoreState,
{
name: 'comments',
}
);
export const {
commentsStore,
useCommentsStore,
CommentsProvider: PrimitiveCommentsProvider,
} = createAtomStore(
{
myUserId: null,
users: {},
comments: {},
activeCommentId: null,
addingCommentId: null,
newValue: [{ type: 'p', children: [{ text: '' }] }],
focusTextarea: false,
onCommentAdd: null,
onCommentUpdate: null,
onCommentDelete: null,
} as CommentsStoreState,
{
name: 'comments',
}
);

export const CommentsProvider = ({
onCommentAdd,
onCommentUpdate,
onCommentDelete,
...props
}: Omit<
React.ComponentProps<typeof PrimitiveCommentsProvider>,
'onCommentAdd' | 'onCommentUpdate' | 'onCommentDelete'
> & {
onCommentAdd?: (value: WithPartial<TComment, 'userId'>) => void;
onCommentUpdate?: (
value: Pick<TComment, 'id'> & Partial<Omit<TComment, 'id'>>
) => void;
onCommentDelete?: (id: string) => void;
}) => (
<PrimitiveCommentsProvider
{...props}
onCommentAdd={onCommentAdd ? { fn: onCommentAdd } : null}
onCommentUpdate={onCommentUpdate ? { fn: onCommentUpdate } : null}
onCommentDelete={onCommentDelete ? { fn: onCommentDelete } : null}
/>
);

export const useCommentsStates = () => useCommentsStore().use;
export const useCommentsSelectors = () => useCommentsStore().get;
Expand Down
Loading

0 comments on commit 133a280

Please sign in to comment.