import { AppContext } from "../common" ; import { useContext , useCallback } from "react" ; import { SPHttpClient , SPHttpClientResponse , ISPHttpClientOptions } from "@microsoft/sp-http" ; import { IlistItemCommentsResults } from "./../models" ; import { IAddCommentPayload } from "../models/IAddCommentPayload" ; import { IComment } from "../components/Comments/IComment" ; import { PageContext } from "@microsoft/sp-page-context" ; interface returnObject { getListItemComments : ( ) => Promise < IlistItemCommentsResults > ; getNextPageOfComments : ( nextLink : string ) => Promise < IlistItemCommentsResults > ; addComment : ( comment : IAddCommentPayload ) => Promise < IComment > ; deleteComment : ( commentId : number ) => Promise < void > ; } export const useSpAPI = ( ) : returnObject => { const { serviceScope , webUrl , listId , itemId , numberCommentsPerPage } = useContext ( AppContext ) ; let _webUrl = "" ; serviceScope . whenFinished ( async ( ) => { _webUrl = serviceScope . consume ( PageContext . serviceKey ) . web . absoluteUrl ; } ) ; const deleteComment = useCallback ( async ( commentId ) : Promise < void > => { const spHttpClient = serviceScope . consume ( SPHttpClient . serviceKey ) ; if ( ! spHttpClient ) return ; const _endPointUrl = `${ webUrl ?? _webUrl }/_api/web/lists(@a1)/GetItemById(@a2)/Comments(@a3)?@a1='${ listId }'&@a2='${ itemId }'&@a3='${ commentId }'` ; const spOpts = { method : "DELETE" , } ; const _deleteResults = await spHttpClient . fetch ( `${ _endPointUrl }` , SPHttpClient . configurations . v1 , spOpts ) ; return ; } , [ serviceScope ] ) ; const addComment = useCallback ( async ( comment ) : Promise < IComment > => { const spHttpClient = serviceScope . consume ( SPHttpClient . serviceKey ) ; if ( ! spHttpClient ) return ; const _endPointUrl = `${ webUrl ?? _webUrl }/_api/web/lists(@a1)/GetItemById(@a2)/Comments()?@a1='${ listId }'&@a2='${ itemId }'` ; const spOpts = { body : `{ "text": "${ comment . text }", "mentions": ${ JSON . stringify ( comment . mentions ) }}` , } ; const _listResults = await spHttpClient . post ( `${ _endPointUrl }` , SPHttpClient . configurations . v1 , spOpts ) ; const _commentResults = ( await _listResults . json ( ) ) as IComment ; return _commentResults ; } , [ serviceScope ] ) ; const getListItemComments = useCallback ( async ( ) : Promise < IlistItemCommentsResults > => { const spHttpClient = serviceScope . consume ( SPHttpClient . serviceKey ) ; if ( ! spHttpClient ) return ; const _endPointUrl = `${ webUrl ?? _webUrl }/_api/web/lists(@a1)/GetItemById(@a2)/GetComments()?@a1='${ listId }'&@a2='${ itemId }'&$top=${ numberCommentsPerPage ?? 10 }` ; const _listResults = await spHttpClient . get ( `${ _endPointUrl }` , SPHttpClient . configurations . v1 ) ; const _commentsResults = ( await _listResults . json ( ) ) as any ; const _returnComments = { comments : _commentsResults . value , hasMore : _commentsResults [ "@odata.nextLink" ] ? true : false , nextLink : _commentsResults [ "@odata.nextLink" ] ?? undefined , } ; return _returnComments ; } , [ serviceScope ] ) ; const getNextPageOfComments = useCallback ( async ( nextLink ) : Promise < IlistItemCommentsResults > => { const spHttpClient = serviceScope . consume ( SPHttpClient . serviceKey ) ; if ( ! spHttpClient || ! nextLink ) return ; const _endPointUrl = nextLink ; const _listResults = await spHttpClient . get ( `${ _endPointUrl }` , SPHttpClient . configurations . v1 ) ; const _commentsResults = ( await _listResults . json ( ) ) as any ; const _returnComments = { comments : _commentsResults . value , hasMore : _commentsResults [ "@odata.nextLink" ] ? true : false , nextLink : _commentsResults [ "@odata.nextLink" ] ?? undefined , } ; return _returnComments ; } , [ serviceScope ] ) ; return { getListItemComments , getNextPageOfComments , addComment , deleteComment } ; } ;