-
Notifications
You must be signed in to change notification settings - Fork 2
Content Feed New Post #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
26 changes: 26 additions & 0 deletions
26
packages/components/modules/content-feed/common/graphql/mutations/ContentPostCreate.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { graphql, useMutation } from 'react-relay' | ||
|
|
||
| import { ContentPostCreateMutation } from '../../../../../__generated__/ContentPostCreateMutation.graphql' | ||
|
|
||
| export const ContentPostCreateMutationQuery = graphql` | ||
| mutation ContentPostCreateMutation($input: ContentPostCreateInput!) { | ||
| contentPostCreate(input: $input) { | ||
| contentPost { | ||
| node { | ||
| id | ||
| content | ||
| user { | ||
| } | ||
| } | ||
| } | ||
| errors { | ||
| field | ||
| messages | ||
| } | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| export const useContentPostCreateMutation = () => | ||
| useMutation<ContentPostCreateMutation>(ContentPostCreateMutationQuery) |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // exports common content-feed code | ||
|
|
||
| export * from './graphql/mutations/ContentPostCreate' |
38 changes: 38 additions & 0 deletions
38
packages/components/modules/content-feed/web/ContentFeed/index.tsx
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| 'use client' | ||
|
|
||
| import { FC, useCallback } from 'react' | ||
|
|
||
| import { Button, Typography } from '@mui/material' | ||
| import { useRouter } from 'next/navigation' | ||
|
|
||
| import { HeaderContainer, RootContainer } from './styled' | ||
| import { ContentFeedProps } from './types' | ||
|
|
||
| const ContentFeed: FC<ContentFeedProps> = () => { | ||
| const router = useRouter() | ||
|
|
||
| const onNewPost = useCallback(() => { | ||
| router.push('/posts/new') | ||
| }, [router]) | ||
|
|
||
| return ( | ||
| <RootContainer> | ||
| <HeaderContainer> | ||
| <Typography component="h4" variant="h4"> | ||
| Content Feed | ||
| </Typography> | ||
| <Button | ||
| variant="outlined" | ||
| color="inherit" | ||
| onClick={onNewPost} | ||
| disableRipple | ||
| sx={{ maxWidth: 'fit-content' }} | ||
| > | ||
| New Post | ||
| </Button> | ||
| </HeaderContainer> | ||
| </RootContainer> | ||
| ) | ||
| } | ||
|
|
||
| export default ContentFeed |
24 changes: 24 additions & 0 deletions
24
packages/components/modules/content-feed/web/ContentFeed/styled.tsx
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { Box, styled } from '@mui/material' | ||
|
|
||
| export const RootContainer = styled(Box)(() => ({ | ||
| display: 'flex', | ||
| width: '600px', | ||
| alignSelf: 'center', | ||
| flexDirection: 'column', | ||
| })) | ||
|
|
||
| export const HeaderContainer = styled(Box)(() => ({ | ||
| display: 'flex', | ||
| width: '100%', | ||
| alignSelf: 'center', | ||
| flexDirection: 'row', | ||
| justifyContent: 'space-between', | ||
| })) | ||
|
|
||
| export const ButtonContainer = styled(Box)(() => ({ | ||
| display: 'flex', | ||
| width: 'fit-content', | ||
| flexDirection: 'row', | ||
| justifyContent: 'space-between', | ||
| gap: '10px', | ||
| })) |
1 change: 1 addition & 0 deletions
1
packages/components/modules/content-feed/web/ContentFeed/types.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export interface ContentFeedProps {} | ||
72 changes: 72 additions & 0 deletions
72
packages/components/modules/content-feed/web/ContentFeedImage/index.tsx
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| 'use client' | ||
|
|
||
| import { FC, useState } from 'react' | ||
|
|
||
| import { Dropzone } from '@baseapp-frontend/design-system/components/web/dropzones' | ||
|
|
||
| import { Box, Typography } from '@mui/material' | ||
| import Image from 'next/image' | ||
|
|
||
| import { IContentFeedImageProps } from './types' | ||
|
|
||
| const ContentFeedImage: FC<IContentFeedImageProps> = ({ form }) => { | ||
| const [selectedPreview, setSelectedPreview] = useState<File>() | ||
| const [selectedPreviewIndex, setSelectedPreviewIndex] = useState<number>() | ||
|
|
||
| const { watch } = form | ||
|
|
||
| const formFiles = watch('images') | ||
|
|
||
| const handleRemoveFile = (fileIndex?: number) => { | ||
| const updatedFiles = formFiles?.filter((_, index) => index !== fileIndex) | ||
| form.setValue('images', updatedFiles as File[], { shouldValidate: true }) | ||
|
|
||
| if (selectedPreviewIndex === fileIndex) { | ||
| setSelectedPreview(undefined) | ||
| setSelectedPreviewIndex(undefined) | ||
| } | ||
| } | ||
|
|
||
| const onSelect = (files: (File | Blob)[]) => { | ||
| if (files.length) { | ||
| form.setValue('images', [...formFiles, ...(files as File[])]) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <Box> | ||
| {selectedPreview && ( | ||
| <Box width="100%" position="relative" height="500px" mb="24px"> | ||
| <Image | ||
| src={URL.createObjectURL(selectedPreview)} | ||
| alt={selectedPreview.name} | ||
| fill | ||
| style={{ objectFit: 'cover', borderRadius: '8px', height: '100%', width: '100%' }} | ||
| onLoad={() => URL.revokeObjectURL(URL.createObjectURL(selectedPreview))} | ||
| /> | ||
| </Box> | ||
| )} | ||
| <Dropzone | ||
| onSelect={onSelect} | ||
| includeActionButton={false} | ||
| maxFileSize={15} | ||
| onRemove={handleRemoveFile} | ||
| InputContainerStyle={{ backgroundColor: 'transparent' }} | ||
| asBase64={false} | ||
| multiple | ||
| accept={{ 'image/png': ['.png'], 'image/gif': ['.gif'], 'image/jpeg': ['.jpg', '.jpeg'] }} | ||
| title={ | ||
| <Typography variant="body2" sx={{ color: 'text.secondary' }}> | ||
| Click to browse or drag and drop images and videos. | ||
davidbermudez-tsl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </Typography> | ||
| } | ||
| onFileClick={(selectedFile, index) => { | ||
| setSelectedPreview(selectedFile as File) | ||
| setSelectedPreviewIndex(index) | ||
| }} | ||
| /> | ||
| </Box> | ||
| ) | ||
| } | ||
|
|
||
| export default ContentFeedImage | ||
12 changes: 12 additions & 0 deletions
12
packages/components/modules/content-feed/web/ContentFeedImage/types.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { UseFormReturn } from 'react-hook-form' | ||
|
|
||
| export interface IContentFeedImageProps { | ||
| form: UseFormReturn< | ||
| { | ||
| content: string | ||
| images: File[] | ||
| }, | ||
| any, | ||
| undefined | ||
| > | ||
| } |
13 changes: 13 additions & 0 deletions
13
packages/components/modules/content-feed/web/NewContentPost/constants.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { z } from 'zod' | ||
|
|
||
| import { ContentPostCreateForm } from './types' | ||
|
|
||
| export const DEFAULT_CONTENT_POST_CREATE_FORM_VALUES = { | ||
| content: '', | ||
| images: [] as File[], | ||
| } | ||
|
|
||
| export const CONTENT_POST_CREATE_FORM_VALIDATION = z.object({ | ||
| content: z.string(), | ||
| images: z.array(z.instanceof(File)), | ||
| } satisfies Record<keyof ContentPostCreateForm, unknown>) | ||
davidbermudez-tsl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
113 changes: 113 additions & 0 deletions
113
packages/components/modules/content-feed/web/NewContentPost/index.tsx
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| 'use client' | ||
|
|
||
| import { FC, useCallback } from 'react' | ||
|
|
||
| import { TextField } from '@baseapp-frontend/design-system/components/web/inputs' | ||
| import { setFormRelayErrors, useNotification } from '@baseapp-frontend/utils' | ||
|
|
||
| import { zodResolver } from '@hookform/resolvers/zod' | ||
| import LoadingButton from '@mui/lab/LoadingButton' | ||
| import { Box, Button, Typography } from '@mui/material' | ||
| import { useRouter } from 'next/navigation' | ||
| import { useForm } from 'react-hook-form' | ||
|
|
||
| import { useContentPostCreateMutation } from '../../common/graphql/mutations/ContentPostCreate' | ||
| import ContentFeedImage from '../ContentFeedImage' | ||
| import { | ||
| CONTENT_POST_CREATE_FORM_VALIDATION, | ||
| DEFAULT_CONTENT_POST_CREATE_FORM_VALUES, | ||
| } from './constants' | ||
| import { ButtonContainer, HeaderContainer, RootContainer } from './styled' | ||
| import { ContentPostCreateForm, NewContentPostProps, UploadableContentPostFiles } from './types' | ||
|
|
||
| const NewContentPost: FC<NewContentPostProps> = () => { | ||
| const router = useRouter() | ||
| const { sendToast } = useNotification() | ||
| const [commitMutation, isMutationInFlight] = useContentPostCreateMutation() | ||
|
|
||
| const formReturn = useForm({ | ||
| defaultValues: DEFAULT_CONTENT_POST_CREATE_FORM_VALUES, | ||
| resolver: zodResolver(CONTENT_POST_CREATE_FORM_VALIDATION), | ||
| mode: 'onBlur', | ||
| }) | ||
|
|
||
| const { | ||
| control, | ||
| handleSubmit, | ||
| reset, | ||
| formState: { isDirty, isValid }, | ||
| } = formReturn | ||
|
|
||
| const onSubmit = handleSubmit((data: ContentPostCreateForm) => { | ||
| const uploadables: UploadableContentPostFiles = {} | ||
|
|
||
| if (data.images) { | ||
| data.images.forEach((image, index) => { | ||
| uploadables[`images.${index}`] = image as File | ||
| }) | ||
| } | ||
|
|
||
davidbermudez-tsl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| commitMutation({ | ||
| variables: { | ||
| input: { | ||
| content: data.content, | ||
| }, | ||
| }, | ||
| uploadables, | ||
| onCompleted(response) { | ||
| const errors = response.contentPostCreate?.errors | ||
| if (errors) { | ||
| sendToast('Something went wrong', { type: 'error' }) | ||
| setFormRelayErrors(formReturn, errors) | ||
| } else { | ||
| reset({ content: '' }) | ||
| sendToast('Post Created Successfully', { type: 'success' }) | ||
| router.push(`/posts/${response.contentPostCreate?.contentPost?.node?.id}`) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| const onCancel = useCallback(() => { | ||
| router.push('/posts') | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, [router]) | ||
|
|
||
| return ( | ||
| <RootContainer> | ||
| <form onSubmit={onSubmit}> | ||
| <HeaderContainer> | ||
| <Typography component="h4" variant="h4"> | ||
| New Post | ||
| </Typography> | ||
| <ButtonContainer> | ||
| <Button variant="outlined" color="inherit" onClick={onCancel} disableRipple> | ||
| Cancel | ||
| </Button> | ||
| <LoadingButton | ||
| color="inherit" | ||
| type="submit" | ||
| loading={isMutationInFlight} | ||
| disabled={!isDirty || !isValid || isMutationInFlight} | ||
| sx={{ maxWidth: 'fit-content', justifySelf: 'end' }} | ||
| > | ||
| Publish | ||
| </LoadingButton> | ||
| </ButtonContainer> | ||
| </HeaderContainer> | ||
| <ContentFeedImage form={formReturn} /> | ||
davidbermudez-tsl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <Box> | ||
| <TextField | ||
| name="content" | ||
| type="text" | ||
| placeholder="What is on your mind?" | ||
| multiline | ||
| rows={4} | ||
| control={control} | ||
| /> | ||
| </Box> | ||
| </form> | ||
| </RootContainer> | ||
| ) | ||
| } | ||
|
|
||
| export default NewContentPost | ||
24 changes: 24 additions & 0 deletions
24
packages/components/modules/content-feed/web/NewContentPost/styled.tsx
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { Box, styled } from '@mui/material' | ||
|
|
||
| export const RootContainer = styled(Box)(() => ({ | ||
| display: 'flex', | ||
| width: '600px', | ||
| alignSelf: 'center', | ||
| flexDirection: 'column', | ||
| })) | ||
|
|
||
| export const HeaderContainer = styled(Box)(() => ({ | ||
| display: 'flex', | ||
| width: '100%', | ||
| alignSelf: 'center', | ||
| flexDirection: 'row', | ||
| justifyContent: 'space-between', | ||
| marginBottom: '32px', | ||
| })) | ||
|
|
||
| export const ButtonContainer = styled(Box)(() => ({ | ||
| display: 'flex', | ||
| width: 'fit-content', | ||
| flexDirection: 'row', | ||
| gap: '10px', | ||
| })) |
10 changes: 10 additions & 0 deletions
10
packages/components/modules/content-feed/web/NewContentPost/types.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export interface ContentPostCreateForm { | ||
| content: string | ||
| images?: File[] | ||
| } | ||
|
|
||
| export type UploadableContentPostFiles = { | ||
| [key: `images.${number}`]: File | Blob | ||
| } | ||
|
|
||
| export type NewContentPostProps = Record<string, never> |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // exports content feed components | ||
|
|
||
| export { default as ContentFeed } from './ContentFeed' | ||
| export type * from './ContentFeed/types' | ||
|
|
||
| export { default as ContentFeedImage } from './ContentFeedImage' | ||
| export type * from './ContentFeedImage/types' | ||
|
|
||
| export { default as NewContentPost } from './NewContentPost' | ||
| export type * from './NewContentPost/types' |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.