feat: editable title for owner
This commit is contained in:
72
src/actions/session.ts
Normal file
72
src/actions/session.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
'use server';
|
||||
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { auth } from '@/lib/auth';
|
||||
import * as sessionsService from '@/services/sessions';
|
||||
|
||||
export async function updateSessionTitle(sessionId: string, title: string) {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) {
|
||||
return { success: false, error: 'Non autorisé' };
|
||||
}
|
||||
|
||||
if (!title.trim()) {
|
||||
return { success: false, error: 'Le titre ne peut pas être vide' };
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await sessionsService.updateSession(sessionId, session.user.id, {
|
||||
title: title.trim(),
|
||||
});
|
||||
|
||||
if (result.count === 0) {
|
||||
return { success: false, error: 'Session non trouvée ou non autorisé' };
|
||||
}
|
||||
|
||||
// Emit event for real-time sync
|
||||
await sessionsService.createSessionEvent(sessionId, session.user.id, 'SESSION_UPDATED', {
|
||||
title: title.trim(),
|
||||
});
|
||||
|
||||
revalidatePath(`/sessions/${sessionId}`);
|
||||
revalidatePath('/sessions');
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error('Error updating session title:', error);
|
||||
return { success: false, error: 'Erreur lors de la mise à jour' };
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateSessionCollaborator(sessionId: string, collaborator: string) {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) {
|
||||
return { success: false, error: 'Non autorisé' };
|
||||
}
|
||||
|
||||
if (!collaborator.trim()) {
|
||||
return { success: false, error: 'Le nom du collaborateur ne peut pas être vide' };
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await sessionsService.updateSession(sessionId, session.user.id, {
|
||||
collaborator: collaborator.trim(),
|
||||
});
|
||||
|
||||
if (result.count === 0) {
|
||||
return { success: false, error: 'Session non trouvée ou non autorisé' };
|
||||
}
|
||||
|
||||
// Emit event for real-time sync
|
||||
await sessionsService.createSessionEvent(sessionId, session.user.id, 'SESSION_UPDATED', {
|
||||
collaborator: collaborator.trim(),
|
||||
});
|
||||
|
||||
revalidatePath(`/sessions/${sessionId}`);
|
||||
revalidatePath('/sessions');
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error('Error updating session collaborator:', error);
|
||||
return { success: false, error: 'Erreur lors de la mise à jour' };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { auth } from '@/lib/auth';
|
||||
import { getSessionById } from '@/services/sessions';
|
||||
import { SwotBoard } from '@/components/swot/SwotBoard';
|
||||
import { SessionLiveWrapper } from '@/components/collaboration';
|
||||
import { EditableTitle } from '@/components/session';
|
||||
import { Badge } from '@/components/ui';
|
||||
|
||||
interface SessionPageProps {
|
||||
@@ -43,7 +44,11 @@ export default async function SessionPage({ params }: SessionPageProps) {
|
||||
|
||||
<div className="flex items-start justify-between">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold text-foreground">{session.title}</h1>
|
||||
<EditableTitle
|
||||
sessionId={session.id}
|
||||
initialTitle={session.title}
|
||||
isOwner={session.isOwner}
|
||||
/>
|
||||
<p className="mt-1 text-lg text-muted">
|
||||
👤 {session.collaborator}
|
||||
</p>
|
||||
|
||||
106
src/components/session/EditableTitle.tsx
Normal file
106
src/components/session/EditableTitle.tsx
Normal file
@@ -0,0 +1,106 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useTransition, useRef, useEffect } from 'react';
|
||||
import { updateSessionTitle } from '@/actions/session';
|
||||
|
||||
interface EditableTitleProps {
|
||||
sessionId: string;
|
||||
initialTitle: string;
|
||||
isOwner: boolean;
|
||||
}
|
||||
|
||||
export function EditableTitle({ sessionId, initialTitle, isOwner }: EditableTitleProps) {
|
||||
const [isEditing, setIsEditing] = useState(false);
|
||||
const [title, setTitle] = useState(initialTitle);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (isEditing && inputRef.current) {
|
||||
inputRef.current.focus();
|
||||
inputRef.current.select();
|
||||
}
|
||||
}, [isEditing]);
|
||||
|
||||
// Update local state when prop changes (e.g., from SSE)
|
||||
useEffect(() => {
|
||||
if (!isEditing) {
|
||||
setTitle(initialTitle);
|
||||
}
|
||||
}, [initialTitle, isEditing]);
|
||||
|
||||
const handleSave = () => {
|
||||
if (!title.trim()) {
|
||||
setTitle(initialTitle);
|
||||
setIsEditing(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (title.trim() === initialTitle) {
|
||||
setIsEditing(false);
|
||||
return;
|
||||
}
|
||||
|
||||
startTransition(async () => {
|
||||
const result = await updateSessionTitle(sessionId, title.trim());
|
||||
if (!result.success) {
|
||||
setTitle(initialTitle);
|
||||
console.error(result.error);
|
||||
}
|
||||
setIsEditing(false);
|
||||
});
|
||||
};
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
handleSave();
|
||||
} else if (e.key === 'Escape') {
|
||||
setTitle(initialTitle);
|
||||
setIsEditing(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (!isOwner) {
|
||||
return <h1 className="text-3xl font-bold text-foreground">{title}</h1>;
|
||||
}
|
||||
|
||||
if (isEditing) {
|
||||
return (
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="text"
|
||||
value={title}
|
||||
onChange={(e) => setTitle(e.target.value)}
|
||||
onBlur={handleSave}
|
||||
onKeyDown={handleKeyDown}
|
||||
disabled={isPending}
|
||||
className="w-full max-w-md rounded-lg border border-border bg-input px-3 py-1.5 text-3xl font-bold text-foreground outline-none focus:border-primary focus:ring-2 focus:ring-primary/20 disabled:opacity-50"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={() => setIsEditing(true)}
|
||||
className="group flex items-center gap-2 text-left"
|
||||
title="Cliquez pour modifier"
|
||||
>
|
||||
<h1 className="text-3xl font-bold text-foreground">{title}</h1>
|
||||
<svg
|
||||
className="h-5 w-5 text-muted opacity-0 transition-opacity group-hover:opacity-100"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
stroke="currentColor"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
2
src/components/session/index.ts
Normal file
2
src/components/session/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { EditableTitle } from './EditableTitle';
|
||||
|
||||
Reference in New Issue
Block a user