Created
January 2, 2021 13:58
-
-
Save Qasem-h/e61571a40e4f10f253dd9bc28b3d89f2 to your computer and use it in GitHub Desktop.
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
import { WindowTitle } from "@byso/components/WindowTitle"; | |
import { DEFAULT_INITIAL_SEARCH_DATA } from "@byso/config"; | |
import useNavigator from "@byso/hooks/useNavigator"; | |
import useNotifier from "@byso/hooks/useNotifier"; | |
import usePategorySearch from "@byso/searches/usePategorySearch"; | |
import createMetadataCreateHandler from "@byso/utils/handlers/metadataCreateHandler"; | |
import { | |
useMetadataUpdate, | |
usePrivateMetadataUpdate | |
} from "@byso/utils/metadata/updateMetadata"; | |
import React from "react"; | |
import { useIntl } from "react-intl"; | |
import PostCreatePage from "../components/PostCreatePage"; | |
import { PostCreateFormData } from "../components/PostCreatePage/form"; | |
import { usePostCreateMutation } from "../mutations"; | |
import { postListUrl, postUrl} from "../urls"; | |
export const PostCreateView: React.FC = () => { | |
const navigate = useNavigator(); | |
const notify = useNotifier(); | |
const intl = useIntl(); | |
const { | |
loadMore: loadMorePategories, | |
search: searchPategory, | |
result: searchPategoryOpts | |
} = usePategorySearch({ | |
variables: DEFAULT_INITIAL_SEARCH_DATA | |
}); | |
const [updateMetadata] = useMetadataUpdate({}); | |
const [updatePrivateMetadata] = usePrivateMetadataUpdate({}); | |
const handleBack = () => navigate(postListUrl()); | |
const [postCreate, postCreateOpts] = usePostCreateMutation({ | |
onCompleted: data => { | |
if (data.postCreate.errors.length === 0) { | |
notify({ | |
status: "success", | |
text: intl.formatMessage({ | |
defaultMessage: "Post created" | |
}) | |
}); | |
} | |
} | |
}); | |
const handleCreate = async (formData: PostCreateFormData) => { | |
const result = await postCreate({ | |
variables: { | |
input: { | |
pategory: formData.pategory, | |
contentJson: JSON.stringify(formData.content), | |
isPublished: formData.isPublished, | |
title: formData.title, | |
publicationDate: | |
formData.publicationDate !== "" ? formData.publicationDate : null, | |
seo: { | |
description: formData.seoDescription, | |
title: formData.seoTitle | |
}, | |
slug: formData.slug, | |
} | |
} | |
}); | |
const postId = result.data.postCreate?.post?.id; | |
if (postId) { | |
navigate(postUrl(postId)); | |
} | |
return postId || null; | |
}; | |
const handleSubmit = createMetadataCreateHandler( | |
handleCreate, | |
updateMetadata, | |
updatePrivateMetadata | |
); | |
return ( | |
<> | |
<WindowTitle | |
title={intl.formatMessage({ | |
defaultMessage: "Create Post", | |
description: "window title" | |
})} | |
/> | |
<PostCreatePage | |
pategories={(searchPategoryOpts.data?.search.edges || []).map( | |
edge => edge.node | |
)} | |
disabled={postCreateOpts.loading} | |
errors={postCreateOpts.data?.postCreate.errors || []} | |
fetchPategories={searchPategory} | |
header={intl.formatMessage({ | |
defaultMessage: "New Post", | |
description: "page header" | |
})} | |
onBack={handleBack} | |
onSubmit={handleSubmit} | |
saveButtonBarState={postCreateOpts.status} | |
fetchMorePategories={{ | |
hasMore: searchPategoryOpts.data?.search.pageInfo.hasNextPage, | |
loading: searchPategoryOpts.loading, | |
onFetchMore: loadMorePategories | |
}} | |
/> | |
</> | |
); | |
}; | |
export default PostCreateView; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment