Skip to content

Commit

Permalink
Added Commit Authors & Refactored Code
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed Jun 24, 2024
1 parent 425238b commit 71bb057
Show file tree
Hide file tree
Showing 25 changed files with 241 additions and 232 deletions.
2 changes: 1 addition & 1 deletion code/client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import AuthProvider from '@/contexts/auth/AuthContext';
import Profile from '@ui/pages/profile/Profile';
import Landing from '@ui/pages/landing/Landing';
import Search from '@ui/pages/search/Search';
import DocumentCommits from '@ui/pages/document/components/history/DocumentCommits';
import DocumentCommits from '@ui/pages/document/components/commit-history/CommitHistory';

function App() {
return (
Expand Down
29 changes: 29 additions & 0 deletions code/client/src/services/commits/commitsService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { HttpCommunication } from '@services/communication/http/httpCommunication';
import { Commit } from '@notespace/shared/src/document/types/commits';

function commitsService(http: HttpCommunication, wid: string, id: string) {
async function commit() {
return await http.post(`/workspaces/${wid}/${id}/commit`);
}

async function getCommits(): Promise<Commit[]> {
return await http.get(`/workspaces/${wid}/${id}/commits`);
}

async function rollback(commitId: string) {
return await http.post(`/workspaces/${wid}/${id}/rollback`, { commitId });
}

async function fork(commitId: string) {
return await http.post(`/workspaces/${wid}/${id}/fork`, { commitId });
}

return {
commit,
getCommits,
rollback,
fork,
};
}

export default commitsService;
13 changes: 13 additions & 0 deletions code/client/src/services/commits/useCommitsService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useMemo } from 'react';
import { useCommunication } from '@/contexts/communication/useCommunication';
import { useParams } from 'react-router-dom';
import commitsService from '@services/commits/commitsService';

function useCommitsService() {
const { http } = useCommunication();
const { wid, id } = useParams();
if (!wid || !id) throw new Error('Cannot use commits service outside of a document');
return useMemo(() => commitsService(http, wid, id), [http, wid, id]);
}

export default useCommitsService;
13 changes: 0 additions & 13 deletions code/client/src/services/versions/useVersionsService.ts

This file was deleted.

29 changes: 0 additions & 29 deletions code/client/src/services/versions/versionsService.ts

This file was deleted.

4 changes: 2 additions & 2 deletions code/client/src/ui/pages/document/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { useCommunication } from '@/contexts/communication/useCommunication';
import useError from '@/contexts/error/useError';
import useDocumentService from '@services/resource/useResourcesService';
import useConnectors from '@domain/editor/connectors/useConnectors';
import FloatingButtons from '@ui/pages/document/components/floating-buttons/FloatingButtons';
import { DocumentResource } from '@notespace/shared/src/workspace/types/resource';
import './Document.scss';
import Version from '@ui/pages/document/components/version/Version';

function Document() {
const communication = useCommunication();
Expand Down Expand Up @@ -55,7 +55,7 @@ function Document() {
return (
<div className="document">
<Editor title={title} fugue={fugue} connectors={connectors} />
<Version />
<FloatingButtons />
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import './DocumentCommits.scss';
import useVersionsService from '@services/versions/useVersionsService';
import './CommitHistory.scss';
import useCommitsService from '@services/commits/useCommitsService';
import { Link, useParams } from 'react-router-dom';
import useResourcesService from '@services/resource/useResourcesService';
import { useEffect, useState } from 'react';
import useLoading from '@ui/hooks/useLoading';
import { DocumentResource } from '@notespace/shared/src/workspace/types/resource';
import { DocumentVersion } from '@notespace/shared/src/document/types/versions';
import { Commit } from '@notespace/shared/src/document/types/commits';
import { formatTimePassed } from '@/utils/utils';
import { FaCodeFork } from 'react-icons/fa6';
import { FaUndo } from 'react-icons/fa';

function DocumentCommits() {
function CommitHistory() {
const [document, setDocument] = useState<DocumentResource>();
const [versions, setVersions] = useState<DocumentVersion[]>([]);
const [commits, setCommits] = useState<Commit[]>([]);
const { loading, spinner, startLoading, stopLoading } = useLoading();
const { id } = useParams();
const { getResource } = useResourcesService();
const { getVersions, forkVersion, rollbackVersion } = useVersionsService();
const { getCommits, fork, rollback } = useCommitsService();

useEffect(() => {
async function fetchDocument() {
const document = (await getResource(id!)) as DocumentResource;
setDocument(document);
}
async function fetchVersions() {
const versions = await getVersions();
setVersions(versions);
async function fetchCommits() {
const commits = await getCommits();
setCommits(commits);
}
startLoading();
Promise.all([fetchDocument(), fetchVersions()])
Promise.all([fetchDocument(), fetchCommits()])
.then(() => stopLoading())
.catch(() => stopLoading());
}, [getResource, getVersions, id, startLoading, stopLoading]);
}, [getResource, getCommits, id, startLoading, stopLoading]);

return (
<div className="document-commits">
Expand All @@ -41,19 +41,19 @@ function DocumentCommits() {
<>
<h2>Commits from "{document?.name}"</h2>
<div className="commits-list">
{versions.length > 0 ? (
versions.map(version => (
<div key={version.id} className="commit">
{commits.length > 0 ? (
commits.map(commit => (
<div key={commit.id} className="commit">
<p>
<Link to={''}>John Doe</Link> committed{' '}
{formatTimePassed(new Date(version.timestamp).toLocaleString())}
<Link to={`/profile/${commit.author.id}`}>{commit.author.name}</Link> committed{' '}
{formatTimePassed(new Date(commit.timestamp).toLocaleString())}
</p>
<div className="commit-actions">
<button onClick={() => rollbackVersion(version.id)}>
<button onClick={() => rollback(commit.id)}>
<FaUndo />
Rollback
</button>
<button onClick={() => forkVersion(version.id)}>
<button onClick={() => fork(commit.id)}>
<FaCodeFork />
Fork
</button>
Expand All @@ -70,4 +70,4 @@ function DocumentCommits() {
);
}

export default DocumentCommits;
export default CommitHistory;
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import { useParams } from 'react-router-dom';
import { FaCodeCommit } from 'react-icons/fa6';
import { useAuth } from '@/contexts/auth/useAuth';

type VersionControlDialogProps = {
type CommitDialogProps = {
onCommit: () => void;
};

function VersionControlDialog({ onCommit }: VersionControlDialogProps) {
function CommitDialog({ onCommit }: CommitDialogProps) {
const { wid, id } = useParams();
const { currentUser } = useAuth();
if (!wid || !id) throw new Error('Cannot use version dialog outside of a document');
if (!wid || !id) throw new Error('Cannot use commit dialog outside of a document');
return (
<Dialog
title="New Commit"
fields={[]}
onSubmit={onCommit}
submitText="Commit"
extraContent={
<div className="commits-dialog">
<div className="commit-dialog">
<p>
Committing as <strong>{currentUser!.displayName}</strong>
</p>
Expand All @@ -30,4 +30,4 @@ function VersionControlDialog({ onCommit }: VersionControlDialogProps) {
);
}

export default VersionControlDialog;
export default CommitDialog;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.version {
.floating-buttons {
position: fixed;
bottom: 0;
right: 0;
Expand Down Expand Up @@ -31,7 +31,7 @@
}
}

.commits-dialog {
.commit-dialog {
display: flex;
flex-direction: column;
justify-content: space-evenly;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import './FloatingButtons.scss';
import CommitDialog from '@ui/pages/document/components/floating-buttons/CommitDialog';
import useCommitsService from '@services/commits/useCommitsService';
import { useNavigate, useParams } from 'react-router-dom';
import { MdHistory } from 'react-icons/md';

function FloatingButtons() {
const { wid, id } = useParams();
const { commit } = useCommitsService();
const navigate = useNavigate();
return (
<div className="floating-buttons">
<CommitDialog onCommit={commit} />
<button onClick={() => navigate(`/workspaces/${wid}/${id}/commits`)}>
<MdHistory />
</button>
</div>
);
}

export default FloatingButtons;
21 changes: 0 additions & 21 deletions code/client/src/ui/pages/document/components/version/Version.tsx

This file was deleted.

54 changes: 27 additions & 27 deletions code/client/tests/editor/slate/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { setup } from '@tests/test-utils';
import { mockCommunication } from '../../mocks/mockCommunication';
import SlateEditor from '@ui/pages/document/components/editor/Editor';
import { Fugue } from '@domain/editor/fugue/Fugue';

/**
* Sets up the editor for testing
* @returns user and the slate editor
*/
const setupEditor = async () => {
const fugue = new Fugue();
const { user, render } = setup(<SlateEditor fugue={fugue} communication={mockCommunication()} />);
const { findByTestId } = render;
const editorElement = await findByTestId('editor'); // calls 'act' under the hood, but is more readable
editorElement.focus();
return { user, editorElement };
};

/**
* Cleans up the editor after testing
*/
const cleanupEditor = async () => {
const editor = document.querySelector('[data-testid="editable"]');
if (editor) editor.innerHTML = '';
};

export { setupEditor, cleanupEditor };
// import { setup } from '@tests/test-utils';
// import { mockCommunication } from '../../mocks/mockCommunication';
// import Editor from '@ui/pages/document/components/editor/Editor';
// import { Fugue } from '@domain/editor/fugue/Fugue';
//
// /**
// * Sets up the editor for testing
// * @returns user and the slate editor
// */
// const setupEditor = async () => {
// const fugue = new Fugue();
// const { user, render } = setup(<Editor fugue={fugue} communication={mockCommunication()} />);
// const { findByTestId } = render;
// const editorElement = await findByTestId('editor'); // calls 'act' under the hood, but is more readable
// editorElement.focus();
// return { user, editorElement };
// };
//
// /**
// * Cleans up the editor after testing
// */
// const cleanupEditor = async () => {
// const editor = document.querySelector('[data-testid="editable"]');
// if (editor) editor.innerHTML = '';
// };
//
// export { setupEditor, cleanupEditor };
Loading

0 comments on commit 71bb057

Please sign in to comment.