@mussnad/frappe-react-query
Version:
A React Query SDK for Frappe
58 lines (57 loc) • 3.32 kB
TypeScript
import { DocumentUpdateEventData, DocTypeListUpdateEventData } from '../types';
/** useFrappeEventListener hook for listening to events from the server
* @param eventName - name of the event to listen to
* @param callback - callback function to be called when the event is triggered. The callback function will receive the data sent from the server. It is recommended to memoize this function.
*
* @example
* ```typescript
* useFrappeEventListener('my_event', (data) => {
* if(data.status === 'success') {
* console.log('success')
* }
* })
* ```
*/
export declare const useFrappeEventListener: <T = any>(eventName: string, callback: (eventData: T) => void) => void;
/**
* Hook for listening to document events.
* The hook will automatically subscribe to the document room, and unsubscribe when the component unmounts.
* The hook listens to the following events:
* - doc_update: This is triggered when the document is updated. The callback function will receive the updated document.
* - doc_viewers: This is triggered when the list of viewers of the document changes. The hook will update the viewers state with the list of viewers.
*
* @param doctype Name of the doctype
* @param docname Name of the document
* @param emitOpenCloseEventsOnMount [Optional] If true, the hook will emit doc_open and doc_close events on mount and unmount respectively. Defaults to true.
* @param onUpdateCallback Function to be called when the document is updated. It is recommended to memoize this function.
*
* @returns Returns an object with the following properties: viewers - array of userID's, emitDocOpen - function to emit doc_open event, emitDocClose - function to emit doc_close event
*
* @example
*
* const { viewers, emitDocOpen, emitDocClose } = useFrappeDocumentEventListener('DocType', 'name', (data) => {
* console.log(data)
* })
*/
export declare const useFrappeDocumentEventListener: (doctype: string, docname: string, onUpdateCallback: (eventData: DocumentUpdateEventData) => void, emitOpenCloseEventsOnMount?: boolean) => {
/** Array of user IDs of users currently viewing the document. This is updated when "doc_viewers" event is published */
viewers: string[];
/** Emit doc_open event - this will explicitly send a doc_open event to the server. */
emitDocOpen: () => void;
/** Emit doc_close event - this will explicitly send a doc_close event to the server. */
emitDocClose: () => void;
};
/**
* Hook for listening to doctype events.
* The hook will automatically subscribe to the doctype room, and unsubscribe when the component unmounts.
* The hook listens to the following event:
* - list_update: This is triggered when a document of the doctype is updated (created, modified or deleted). The callback function will receive the updated document.
*
* @param doctype Name of the doctype
* @param onListUpdateCallback Function to be called when the document is updated. It is recommended to memoize this function.
*
* @example
*
* const { data, error, isFetching, mutate } = useFrappeGetDocCount('DocType', { filters: [{ field: 'name', operator: 'like', value: 'test' }] })
*/
export declare const useFrappeDocTypeEventListener: (doctype: string, onListUpdateCallback: (eventData: DocTypeListUpdateEventData) => void) => void;