-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe89fe2
commit 0ddb415
Showing
28 changed files
with
399 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { useContext } from 'react'; | ||
import { ErrorContext } from '@domain/error/ErrorContext'; | ||
|
||
const useError = () =>useContext(ErrorContext); | ||
const useError = () => useContext(ErrorContext); | ||
|
||
export default useError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,39 @@ | ||
import * as React from 'react'; | ||
import { useState, createContext } from 'react'; | ||
import { useState, createContext, useEffect } from 'react'; | ||
import { WorkspaceResource } from '@notespace/shared/workspace/types/resource.ts'; | ||
import { useCommunication } from '@/services/communication/context/useCommunication.ts'; | ||
import useError from '@domain/error/useError.ts'; | ||
|
||
export type WorkspaceContextType = { | ||
resources: WorkspaceResource[]; | ||
setResources: (resources: WorkspaceResource[]) => void; | ||
filePath: string | undefined; | ||
setFilePath: (path: string) => void; | ||
}; | ||
|
||
export const WorkspaceContext = createContext<WorkspaceContextType>({ | ||
resources: [], | ||
setResources: () => {}, | ||
filePath: undefined, | ||
setFilePath: () => {}, | ||
}); | ||
|
||
export function WorkspaceProvider({ children }: { children: React.ReactNode }) { | ||
const [resources, setResources] = useState<WorkspaceResource[]>([]); | ||
const [filePath, setFilePath] = useState<string | undefined>(undefined); | ||
return <WorkspaceContext.Provider value={{ filePath, setFilePath }}>{children}</WorkspaceContext.Provider>; | ||
const { http } = useCommunication(); | ||
const { showError } = useError(); | ||
|
||
useEffect(() => { | ||
async function getResources() { | ||
const res = await http.get('/documents'); | ||
setResources(res); | ||
} | ||
getResources().catch(showError); | ||
}, [http, showError]); | ||
return ( | ||
<WorkspaceContext.Provider value={{ resources, setResources, filePath, setFilePath }}> | ||
{children} | ||
</WorkspaceContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useState } from 'react'; | ||
import { Communication } from '@/services/communication/communication.ts'; | ||
import { DocumentResourceMetadata } from '@notespace/shared/workspace/types/resource.ts'; | ||
import useSocketListeners from '@/services/communication/socket/useSocketListeners.ts'; | ||
import useWorkspace from '@domain/workspace/useWorkspace.ts'; | ||
import { ResourceType } from '@notespace/shared/workspace/types/resource.ts'; | ||
|
||
export function useDocuments({ http, socket }: Communication) { | ||
const { resources } = useWorkspace(); | ||
const [documents, setDocuments] = useState<DocumentResourceMetadata[]>( | ||
resources.filter(res => res.type === ResourceType.DOCUMENT) as DocumentResourceMetadata[] | ||
); | ||
|
||
function onCreateDocument(id: string, name?: string) { | ||
const document: DocumentResourceMetadata = { id, name: name || '', type: ResourceType.DOCUMENT }; | ||
setDocuments(prev => [...prev, document]); | ||
} | ||
|
||
async function createDocument(title?: string) { | ||
const { id } = await http.post('/documents', { title }); | ||
onCreateDocument(id, title); | ||
} | ||
|
||
function onDeleteDocument(id: string) { | ||
setDocuments(documents.filter(res => res.id !== id)); | ||
} | ||
|
||
async function deleteDocument(id: string) { | ||
await http.delete(`/documents/${id}`); | ||
onDeleteDocument(id); | ||
} | ||
|
||
function onUpdateDocument(id: string, title: string) { | ||
setDocuments(documents.map(doc => (doc.id === id ? { ...doc, title } : doc))); | ||
} | ||
|
||
async function updateDocument(id: string, title: string) { | ||
await http.put(`/documents/${id}`, { title }); | ||
onUpdateDocument(id, title); | ||
} | ||
|
||
useSocketListeners(socket, { | ||
'document:create': onCreateDocument, | ||
'document:delete': onDeleteDocument, | ||
'document:update': onUpdateDocument, | ||
}); | ||
|
||
return { | ||
documents, | ||
createDocument, | ||
deleteDocument, | ||
updateDocument, | ||
}; | ||
} | ||
|
||
export default useDocuments; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 23 additions & 39 deletions
62
code/server/src/ts/controllers/http/workspace/resourcesHandlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,64 @@ | ||
import PromiseRouter from 'express-promise-router'; | ||
import { ResourceInputModel, WorkspaceResource } from '../../../../../../shared/workspace/resource'; | ||
import { ResourceInputModel, WorkspaceResource } from '@notespace/shared/workspace/resource'; | ||
import { httpResponse } from '@controllers/http/httpResponse'; | ||
import { Request, Response } from 'express'; | ||
import { ResourcesService } from '@services/resourcesService'; | ||
import { InvalidParameterError } from '@domain/errors/errors'; | ||
|
||
function resourcesHandlers(services : ResourcesService) { | ||
function resourcesHandlers(service: ResourcesService) { | ||
const router = PromiseRouter(); | ||
|
||
/** | ||
* Create a new resource - | ||
* @param req | ||
* @param res | ||
*/ | ||
const createResource = async (req : Request, res : Response) => { | ||
const createResource = async (req: Request, res: Response) => { | ||
const resource = req.body as ResourceInputModel; | ||
const id = await services.createResource(resource); | ||
|
||
if (!id) return httpResponse.internalServerError(res).send(); | ||
|
||
const id = await service.createResource(resource); | ||
return httpResponse.created(res).json({ id }); | ||
} | ||
}; | ||
|
||
/** | ||
* Get a resource by its id | ||
* @param req | ||
* @param res | ||
*/ | ||
const getResource = async (req : Request, res: Response) => { | ||
const id = req.params.resId; | ||
const resource = await services.getResource(id); | ||
|
||
if (!resource) return httpResponse.notFound(res).send(); | ||
|
||
const getResource = async (req: Request, res: Response) => { | ||
const { wid, id, metaOnly } = req.params; | ||
const resource = await service.getResource(wid, id, metaOnly === 'true'); | ||
return httpResponse.ok(res).json(resource); | ||
} | ||
|
||
|
||
const getDocContent = async (req : Request, res : Response) => { | ||
const {wid, rid} = req.params; | ||
const content = await services.getDocContent(wid, rid); | ||
return httpResponse.ok(res).json(content); | ||
} | ||
}; | ||
|
||
/** | ||
* Update a resource | ||
* @param req | ||
* @param res | ||
*/ | ||
const updateResource = async (req : Request, res : Response) => { | ||
const resource = req.body as Partial<WorkspaceResource> | ||
|
||
if (!resource.id) return httpResponse.badRequest(res).send('Resource id is required'); | ||
|
||
await services.updateResource(resource); | ||
const updateResource = async (req: Request, res: Response) => { | ||
const resource = req.body as Partial<WorkspaceResource>; | ||
if (!resource.id) throw new InvalidParameterError('Resource id is required'); | ||
await service.updateResource(resource); | ||
return httpResponse.noContent(res).send(); | ||
} | ||
}; | ||
|
||
/** | ||
* Delete a resource | ||
* @param req | ||
* @param res | ||
*/ | ||
const deleteResource = async (req : Request, res : Response) => { | ||
const id = req.params.resId; | ||
await services.deleteResource(id); | ||
const deleteResource = async (req: Request, res: Response) => { | ||
const { id } = req.params; | ||
await service.deleteResource(id); | ||
return httpResponse.noContent(res).send(); | ||
} | ||
}; | ||
|
||
router.post('/', createResource); | ||
router.get('/:rid', getResource); | ||
router.get('/:rid/content', getDocContent); | ||
|
||
router.put('/:rid', updateResource); | ||
router.delete('/:rid', deleteResource); | ||
router.get('/:id', getResource); | ||
router.put('/:id', updateResource); | ||
router.delete('/:id', deleteResource); | ||
|
||
return router; | ||
} | ||
|
||
export default resourcesHandlers; | ||
export default resourcesHandlers; |
Oops, something went wrong.