{"version":3,"file":"usePostMutation-Cxo-XYBE.js","sources":["../../../node_modules/react-query/es/react/useIsMutating.js","../../../frontend/src/Components/Posts/Moderation/ModerationWarningModal.tsx","../../../frontend/src/Hooks/usePostMutation.ts"],"sourcesContent":["import React from 'react';\nimport { notifyManager } from '../core/notifyManager';\nimport { parseMutationFilterArgs } from '../core/utils';\nimport { useQueryClient } from './QueryClientProvider';\nexport function useIsMutating(arg1, arg2) {\n var mountedRef = React.useRef(false);\n var filters = parseMutationFilterArgs(arg1, arg2);\n var queryClient = useQueryClient();\n\n var _React$useState = React.useState(queryClient.isMutating(filters)),\n isMutating = _React$useState[0],\n setIsMutating = _React$useState[1];\n\n var filtersRef = React.useRef(filters);\n filtersRef.current = filters;\n var isMutatingRef = React.useRef(isMutating);\n isMutatingRef.current = isMutating;\n React.useEffect(function () {\n mountedRef.current = true;\n var unsubscribe = queryClient.getMutationCache().subscribe(notifyManager.batchCalls(function () {\n if (mountedRef.current) {\n var newIsMutating = queryClient.isMutating(filtersRef.current);\n\n if (isMutatingRef.current !== newIsMutating) {\n setIsMutating(newIsMutating);\n }\n }\n }));\n return function () {\n mountedRef.current = false;\n unsubscribe();\n };\n }, [queryClient]);\n return isMutating;\n}","import React from 'react';\nimport { useIntl, FormattedMessage } from 'react-intl';\nimport { Box, Button, Typography } from '@mui/material';\nimport { Link } from 'react-router-dom';\n\nimport Dialog from '../../DesignSystem/Dialog';\nimport { Post } from '../../../Utils/Types/Posts';\nimport { editPostPath } from '../../../Utils/routes';\nimport useSpace from '../../../Hooks/useSpace';\n\ninterface ModerationWarningModalProps {\n post: Post;\n open: boolean;\n handleClose: () => void;\n isEdit: boolean;\n onClose?: () => void;\n}\n\nfunction ModerationWarningModal({\n open,\n post,\n isEdit,\n handleClose,\n onClose,\n}: ModerationWarningModalProps) {\n const intl = useIntl();\n const { space } = useSpace();\n\n const editPath = editPostPath(space.slug, post.slug, post.type);\n\n return (\n }\n maxWidth=\"sm\"\n onClose={handleClose}\n dismissible={false}\n disableGutters\n fullWidth\n actions={\n <>\n {isEdit ? (\n \n ) : (\n \n )}\n\n \n >\n }\n >\n \n \n {post.meta.ai_data.moderation_reason}\n \n \n \n \n \n \n \n \n );\n}\n\nexport default ModerationWarningModal;\n","import { useState, useCallback } from 'react';\nimport { QueryClient, useIsMutating, useMutation, useQueryClient } from 'react-query';\nimport { useNavigate } from 'react-router-dom';\nimport { compact } from 'lodash';\nimport { useModal } from 'mui-modal-provider';\n\nimport { Post } from '../Utils/Types/Posts';\nimport queriesConfig from '../Services/queriesConfig';\nimport useSpace from './useSpace';\nimport {\n extractErrorsForNestedAttributes,\n extractErrorForToaster,\n} from '../Services/ApiErrorHelpers';\nimport Analytics from '../Services/AnalyticsService';\nimport { PostTypes } from '../Utils/Types';\nimport { isIdea, isMessage } from '../Utils/Post';\nimport { editPostPath, postPath } from '../Utils/routes';\nimport { messages, moment, currentLocale } from '../I18n';\nimport { Space } from '../Utils/Types/Space';\nimport ModerationWarningModal from '../Components/Posts/Moderation/ModerationWarningModal';\nimport { notify } from '../Utils/NotificationUtils';\n\ninterface PostMutationSuccessProps {\n response: any;\n space: Space;\n queryClient: QueryClient;\n data: any;\n responsePost: Post;\n refetch: any;\n navigate: any;\n params: any;\n intl: any;\n notifications: any;\n onSuccess?: (response: any) => void;\n}\n\nfunction onCreatePostSuccess({\n space,\n queryClient,\n data,\n response,\n responsePost,\n refetch,\n navigate,\n params,\n intl,\n notifications,\n onSuccess,\n}: PostMutationSuccessProps) {\n Analytics.track('Post', `Create ${data.post_type}`, {\n 'Post Type': data.post_type,\n 'Event Source': params.get('event_source'),\n });\n\n if (isIdea(responsePost.type) || isMessage(responsePost.type)) {\n if (!responsePost.published_at && !isIdea(responsePost.type)) {\n notify(notifications, intl.formatMessage({ id: 'POST_FORM.POST_SAVED' }));\n }\n\n if (responsePost.published_at) {\n navigate(`${postPath(space.slug, responsePost.slug)}?modal=post-created`);\n } else {\n navigate(postPath(space.slug, responsePost.slug));\n }\n } else {\n notify(notifications, intl.formatMessage({ id: 'POST_FORM.POST_SAVED' }));\n navigate(editPostPath(space.slug, responsePost.slug, responsePost.type));\n }\n\n refetch();\n queryClient.invalidateQueries(['post-children']);\n\n if (onSuccess) {\n onSuccess(response);\n }\n}\n\nfunction onUpdatePostSuccess({\n response,\n queryClient,\n data,\n responsePost,\n intl,\n notifications,\n onSuccess,\n}: PostMutationSuccessProps) {\n Analytics.track('Post', 'Update', `Update ${data.post_type}`, {\n 'Post Type': data.post_type,\n });\n\n const { ask_to_publish_at: askToPublishAt, published_at: publishedAt, type } = responsePost;\n let toasterTextId = null;\n let toasterValueOptions = {};\n\n // TODO: Refactor this to be more readable, and be reusable for the create case\n if (type === PostTypes.IDEA) {\n toasterTextId = 'POST_FORM.POST_SAVED';\n } else if (publishedAt) {\n toasterTextId = 'POST_FORM.POST_PUBLISHED';\n } else if (askToPublishAt) {\n const momentDefaultDate = messages[currentLocale]['MOMENT.DATE.DEFAULT'];\n\n toasterTextId = 'POST_FORM.POST_SCHEDULED';\n toasterValueOptions = {\n date: moment(askToPublishAt).format(momentDefaultDate),\n time: moment(askToPublishAt).format('LT'),\n };\n } else {\n toasterTextId = 'POST_FORM.POST_SAVED';\n }\n\n notify(notifications, intl.formatMessage({ id: toasterTextId }, { ...toasterValueOptions }));\n\n queryClient.setQueryData(queriesConfig.getPost(responsePost.id).queryKey, response);\n\n if (onSuccess) {\n onSuccess(response);\n }\n}\n\nexport default function usePostMutation({\n post,\n isEdit = false,\n isDashboard = false,\n intl,\n notifications,\n}: {\n post?: Post;\n isEdit?: boolean;\n isDashboard?: boolean;\n intl: any;\n notifications: any;\n}) {\n const queryClient = useQueryClient();\n const { space, refetch } = useSpace();\n const navigate = useNavigate();\n const [errors, setErrors] = useState({});\n const mutationKey = isEdit ? compact(['updatePost', post?.id]) : ['createPost'];\n const { showModal } = useModal();\n\n const showModerationModal = (post: Post, handleClose: any) => {\n showModal(ModerationWarningModal, {\n post,\n isEdit,\n handleClose,\n });\n };\n\n // this is to know if a mutation is happening on another hook call\n const postMutationsInProgress = useIsMutating(mutationKey);\n const isMutating = postMutationsInProgress > 0;\n\n const params = new URLSearchParams(window.location.search);\n\n let method: any;\n if (isEdit && post?.id) {\n method = isDashboard\n ? queriesConfig.updatePostDashboard(post.id).mutationFn\n : queriesConfig.updatePost(post.id).mutationFn;\n } else {\n method = queriesConfig.createPost().mutationFn;\n }\n\n const mutation = useMutation(method, { mutationKey });\n\n const mutate = useCallback(\n (data: any, { onSuccess }: { onSuccess?: (response: any) => void } = {}) => {\n return mutation.mutate(data, {\n onSuccess: (response: any) => {\n if (response.ok) {\n setErrors({});\n const responsePost = response.data?.data;\n\n // Once the save is successful\n // we need to trigger some actions like (show toaster, update cache, navigate to the post, etc)\n const successParams = {\n space,\n queryClient,\n data,\n responsePost,\n response,\n refetch,\n navigate,\n params,\n intl,\n notifications,\n onSuccess,\n };\n\n if (responsePost?.meta?.ai_data?.moderation) {\n // If the post is under moderation, we show the moderation modal\n showModerationModal(\n responsePost,\n post?.id\n ? () => onUpdatePostSuccess(successParams)\n : () => onCreatePostSuccess(successParams),\n );\n } else if (post?.id) {\n onUpdatePostSuccess(successParams);\n } else {\n onCreatePostSuccess(successParams);\n }\n } else if (response.status === 422) {\n const apiErrors = extractErrorsForNestedAttributes(response);\n notify(\n notifications,\n intl.formatMessage({ id: 'POST_FORM.ERRORS.FILL_FIELDS' }),\n 'error',\n );\n\n setErrors(apiErrors);\n } else {\n notify(notifications, extractErrorForToaster(response, intl), 'error');\n }\n },\n });\n },\n [],\n );\n\n return {\n ...mutation,\n mutate,\n isLoading: mutation.isLoading || isMutating,\n errors,\n };\n}\n"],"names":["useIsMutating","arg1","arg2","mountedRef","React","filters","parseMutationFilterArgs","queryClient","useQueryClient","_React$useState","isMutating","setIsMutating","filtersRef","isMutatingRef","unsubscribe","notifyManager","newIsMutating","ModerationWarningModal","_ref","open","post","isEdit","handleClose","onClose","intl","useIntl","_useSpace","useSpace","space","editPath","editPostPath","slug","type","createElement","Dialog","id","label","formatMessage","title","FormattedMessage","maxWidth","dismissible","disableGutters","fullWidth","actions","Fragment","Button","variant","color","onClick","to","component","Link","Box","sx","p","mb","fontWeight","meta","ai_data","moderation_reason","Typography","onCreatePostSuccess","data","response","responsePost","refetch","navigate","params","notifications","onSuccess","Analytics","track","concat","post_type","get","isIdea","isMessage","published_at","postPath","invalidateQueries","onUpdatePostSuccess","_ref2","askToPublishAt","ask_to_publish_at","publishedAt","toasterTextId","toasterValueOptions","PostTypes","IDEA","momentDefaultDate","messages","currentLocale","date","moment","format","time","_objectSpread","setQueryData","queriesConfig","getPost","queryKey","usePostMutation","_ref3","_ref3$isEdit","_ref3$isDashboard","isDashboard","useNavigate","_useState","useState","_useState2","_slicedToArray","errors","setErrors","mutationKey","compact","_useModal","useModal","showModal","showModerationModal","postMutationsInProgress","URLSearchParams","window","location","search","method","updatePostDashboard","mutationFn","updatePost","createPost","mutation","useMutation","mutate","useCallback","_ref4","arguments","length","undefined","ok","_response$data","_responsePost$meta","successParams","moderation","status","apiErrors","extractErrorsForNestedAttributes","notify","extractErrorForToaster","isLoading"],"mappings":"uTAIO,SAASA,GAAcC,EAAMC,EAAM,CACxC,IAAIC,EAAaC,EAAM,OAAO,EAAK,EAC/BC,EAAUC,GAAwBL,EAAMC,CAAI,EAC5CK,EAAcC,EAAgB,EAE9BC,EAAkBL,EAAM,SAASG,EAAY,WAAWF,CAAO,CAAC,EAChEK,EAAaD,EAAgB,CAAC,EAC9BE,EAAgBF,EAAgB,CAAC,EAEjCG,EAAaR,EAAM,OAAOC,CAAO,EACrCO,EAAW,QAAUP,EACrB,IAAIQ,EAAgBT,EAAM,OAAOM,CAAU,EAC3C,OAAAG,EAAc,QAAUH,EACxBN,EAAM,UAAU,UAAY,CAC1BD,EAAW,QAAU,GACrB,IAAIW,EAAcP,EAAY,iBAAgB,EAAG,UAAUQ,GAAc,WAAW,UAAY,CAC9F,GAAIZ,EAAW,QAAS,CACtB,IAAIa,EAAgBT,EAAY,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