({
onPostSuccess,
onComplete,
onMutate,
onError,
onExternalLinkSuccess,
onSourcePostModerationSuccess,
getSharedPostSuccessToast,
initialPreview,
displayMutationErrors = false,
}: UsePostToSquadProps = {})
| 98 | }; |
| 99 | |
| 100 | export const usePostToSquad = ({ |
| 101 | onPostSuccess, |
| 102 | onComplete, |
| 103 | onMutate, |
| 104 | onError, |
| 105 | onExternalLinkSuccess, |
| 106 | onSourcePostModerationSuccess, |
| 107 | getSharedPostSuccessToast, |
| 108 | initialPreview, |
| 109 | displayMutationErrors = false, |
| 110 | }: UsePostToSquadProps = {}): UsePostToSquad => { |
| 111 | const { toggleGroup, getGroupStatus } = useNotificationSettings(); |
| 112 | const { displayToast } = useToastNotification(); |
| 113 | const logPostCreated = useLogPostCreated(); |
| 114 | const { user } = useAuthContext(); |
| 115 | const client = useQueryClient(); |
| 116 | const { completeAction } = useActions(); |
| 117 | const moderationCreationRef = useRef<PostType | null>(null); |
| 118 | const [preview, setPreview] = useState<ExternalLinkPreview>( |
| 119 | initialPreview ?? {}, |
| 120 | ); |
| 121 | const { requestMethod: requestMethodContext } = useRequestProtocol(); |
| 122 | const requestMethod = requestMethodContext ?? gqlClient.request; |
| 123 | |
| 124 | const handleMutationError = useCallback( |
| 125 | (err: unknown, options: { displayError?: boolean } = {}): void => { |
| 126 | if (!isApiErrorResult(err)) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | const { displayError = true } = options; |
| 131 | if (displayMutationErrors && displayError) { |
| 132 | displayToast(err.response?.errors?.[0]?.message ?? DEFAULT_ERROR); |
| 133 | } |
| 134 | |
| 135 | onError?.(err); |
| 136 | }, |
| 137 | [displayMutationErrors, displayToast, onError], |
| 138 | ); |
| 139 | |
| 140 | const handlePostSuccess = useCallback( |
| 141 | (post: Post): void => { |
| 142 | onPostSuccess?.(post, post?.permalink ?? ''); |
| 143 | onComplete?.(); |
| 144 | }, |
| 145 | [onComplete, onPostSuccess], |
| 146 | ); |
| 147 | |
| 148 | const handleComplete = useCallback((): void => onComplete?.(), [onComplete]); |
| 149 | |
| 150 | const { |
| 151 | mutateAsync: onCreatePost, |
| 152 | isPending: isLoadingFreeform, |
| 153 | isSuccess: isFreeformPostSuccess, |
| 154 | } = useMutation({ |
| 155 | mutationFn: createPost, |
| 156 | onMutate, |
| 157 | onError: (err) => handleMutationError(err), |
no test coverage detected