mirror of
https://github.com/colanode/colanode.git
synced 2025-12-16 11:47:47 +01:00
Use tanstackdb for space update
This commit is contained in:
@@ -50,7 +50,6 @@ import { ServerCreateMutationHandler } from './servers/server-create';
|
||||
import { ServerDeleteMutationHandler } from './servers/server-delete';
|
||||
import { ServerSyncMutationHandler } from './servers/server-sync';
|
||||
import { SpaceChildReorderMutationHandler } from './spaces/space-child-reorder';
|
||||
import { SpaceUpdateMutationHandler } from './spaces/space-update';
|
||||
import { UserRoleUpdateMutationHandler } from './users/user-role-update';
|
||||
import { UserStorageUpdateMutationHandler } from './users/user-storage-update';
|
||||
import { UsersCreateMutationHandler } from './users/users-create';
|
||||
@@ -104,7 +103,6 @@ export const buildMutationHandlerMap = (
|
||||
'account.logout': new AccountLogoutMutationHandler(app),
|
||||
'file.create': new FileCreateMutationHandler(app),
|
||||
'file.download': new FileDownloadMutationHandler(app),
|
||||
'space.update': new SpaceUpdateMutationHandler(app),
|
||||
'space.child.reorder': new SpaceChildReorderMutationHandler(app),
|
||||
'account.update': new AccountUpdateMutationHandler(app),
|
||||
'view.update': new ViewUpdateMutationHandler(app),
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
import { WorkspaceMutationHandlerBase } from '@colanode/client/handlers/mutations/workspace-mutation-handler-base';
|
||||
import { MutationHandler } from '@colanode/client/lib/types';
|
||||
import { MutationError, MutationErrorCode } from '@colanode/client/mutations';
|
||||
import {
|
||||
SpaceUpdateMutationInput,
|
||||
SpaceUpdateMutationOutput,
|
||||
} from '@colanode/client/mutations/spaces/space-update';
|
||||
import { SpaceAttributes } from '@colanode/core';
|
||||
|
||||
export class SpaceUpdateMutationHandler
|
||||
extends WorkspaceMutationHandlerBase
|
||||
implements MutationHandler<SpaceUpdateMutationInput>
|
||||
{
|
||||
async handleMutation(
|
||||
input: SpaceUpdateMutationInput
|
||||
): Promise<SpaceUpdateMutationOutput> {
|
||||
const workspace = this.getWorkspace(input.userId);
|
||||
|
||||
const result = await workspace.nodes.updateNode<SpaceAttributes>(
|
||||
input.spaceId,
|
||||
(attributes) => {
|
||||
attributes.name = input.name;
|
||||
attributes.description = input.description;
|
||||
attributes.avatar = input.avatar;
|
||||
return attributes;
|
||||
}
|
||||
);
|
||||
|
||||
if (result === 'unauthorized') {
|
||||
throw new MutationError(
|
||||
MutationErrorCode.SpaceUpdateForbidden,
|
||||
"You don't have permission to update this space."
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,6 @@ export * from './nodes/node-reaction-delete';
|
||||
export * from './pages/page-update';
|
||||
export * from './servers/server-create';
|
||||
export * from './servers/server-delete';
|
||||
export * from './spaces/space-update';
|
||||
export * from './spaces/space-child-reorder';
|
||||
export * from './workspaces/workspace-create';
|
||||
export * from './workspaces/workspace-delete';
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
export type SpaceUpdateMutationInput = {
|
||||
type: 'space.update';
|
||||
userId: string;
|
||||
spaceId: string;
|
||||
name: string;
|
||||
description: string;
|
||||
avatar?: string | null;
|
||||
};
|
||||
|
||||
export type SpaceUpdateMutationOutput = {
|
||||
success: boolean;
|
||||
};
|
||||
|
||||
declare module '@colanode/client/mutations' {
|
||||
interface MutationMap {
|
||||
'space.update': {
|
||||
input: SpaceUpdateMutationInput;
|
||||
output: SpaceUpdateMutationOutput;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
import { toast } from 'sonner';
|
||||
|
||||
import { LocalSpaceNode } from '@colanode/client/types';
|
||||
import { NodeRole, hasNodeRole } from '@colanode/core';
|
||||
import { collections } from '@colanode/ui/collections';
|
||||
import { NodeCollaborators } from '@colanode/ui/components/collaborators/node-collaborators';
|
||||
import { SpaceDelete } from '@colanode/ui/components/spaces/space-delete';
|
||||
import { SpaceForm } from '@colanode/ui/components/spaces/space-form';
|
||||
import {
|
||||
SpaceForm,
|
||||
SpaceFormValues,
|
||||
} from '@colanode/ui/components/spaces/space-form';
|
||||
import { Separator } from '@colanode/ui/components/ui/separator';
|
||||
import { useWorkspace } from '@colanode/ui/contexts/workspace';
|
||||
import { useMutation } from '@colanode/ui/hooks/use-mutation';
|
||||
|
||||
interface SpaceContainerProps {
|
||||
space: LocalSpaceNode;
|
||||
@@ -16,11 +17,27 @@ interface SpaceContainerProps {
|
||||
|
||||
export const SpaceContainer = ({ space, role }: SpaceContainerProps) => {
|
||||
const workspace = useWorkspace();
|
||||
const { mutate } = useMutation();
|
||||
|
||||
const canEdit = hasNodeRole(role, 'admin');
|
||||
const canDelete = hasNodeRole(role, 'admin');
|
||||
|
||||
const handleSubmit = (values: SpaceFormValues) => {
|
||||
const nodes = collections.workspace(workspace.userId).nodes;
|
||||
if (!nodes.has(space.id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
nodes.update(space.id, (draft) => {
|
||||
if (draft.type !== 'space') {
|
||||
return;
|
||||
}
|
||||
|
||||
draft.name = values.name;
|
||||
draft.description = values.description;
|
||||
draft.avatar = values.avatar;
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="max-w-4xl space-y-8 w-full pb-10">
|
||||
<div className="space-y-6">
|
||||
@@ -35,24 +52,7 @@ export const SpaceContainer = ({ space, role }: SpaceContainerProps) => {
|
||||
avatar: space.avatar ?? null,
|
||||
}}
|
||||
readOnly={!canEdit}
|
||||
onSubmit={(values) => {
|
||||
mutate({
|
||||
input: {
|
||||
type: 'space.update',
|
||||
userId: workspace.userId,
|
||||
spaceId: space.id,
|
||||
name: values.name,
|
||||
description: values.description,
|
||||
avatar: values.avatar,
|
||||
},
|
||||
onSuccess() {
|
||||
toast.success('Space updated');
|
||||
},
|
||||
onError(error) {
|
||||
toast.error(error.message);
|
||||
},
|
||||
});
|
||||
}}
|
||||
onSubmit={handleSubmit}
|
||||
submitText="Update"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user