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

Fix CommentsProvider callbacks #2817

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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