Skip to content

Commit

Permalink
fixed updating offer data
Browse files Browse the repository at this point in the history
  • Loading branch information
dudchakk committed Oct 16, 2024
1 parent 468b855 commit 1107990
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
16 changes: 15 additions & 1 deletion src/components/complete-profile/CompleteProfileBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import { styles } from '~/components/complete-profile/CompleteProfileBlock.style
import useAxios from '~/hooks/use-axios'
import { OfferService } from '~/services/offer-service'
import { defaultResponse } from '~/pages/my-offers/MyOffers.constants'
import { useDrawer } from '~/hooks/use-drawer'
import AppDrawer from '~/components/app-drawer/AppDrawer'
import CreateOffer from '~/containers/offer-page/create-offer/CreateOffer'

interface CompleteProfileBlockProps {
profileItems: ProfileItemType[]
Expand All @@ -42,6 +45,10 @@ const CompleteProfileBlock: FC<CompleteProfileBlockProps> = ({
const homePage = useMatch(guestRoutes[userRole as UserRole].path)
const [isOpen, setIsOpen] = useState(false)

const { openDrawer, closeDrawer, isOpen: isDrawerOpen } = useDrawer()
const [isOfferCreated, setIsOfferCreated] = useState(false)
const handleOpenDrawer = () => openDrawer()

useEffect(() => {
if (openAccordion) {
setIsOpen(true)
Expand All @@ -53,7 +60,7 @@ const CompleteProfileBlock: FC<CompleteProfileBlockProps> = ({
OfferService.getUsersOffers({
id: userId
}),
[userId]
[userId, isOfferCreated]

Check warning on line 63 in src/components/complete-profile/CompleteProfileBlock.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useCallback has an unnecessary dependency: 'isOfferCreated'. Either exclude it or remove the dependency array
)

const { response } = useAxios({
Expand Down Expand Up @@ -99,6 +106,7 @@ const CompleteProfileBlock: FC<CompleteProfileBlockProps> = ({
() =>
profileItems.map((item) => (
<ProfileItem
handleOpenDrawer={handleOpenDrawer}
isFilled={checkProfileData.includes(item)}
item={item}
key={item.id}
Expand Down Expand Up @@ -151,6 +159,12 @@ const CompleteProfileBlock: FC<CompleteProfileBlockProps> = ({
</AccordionSummary>
<AccordionDetails sx={styles.profileItems}>
{profileList}
<AppDrawer onClose={closeDrawer} open={isDrawerOpen}>
<CreateOffer
closeDrawer={closeDrawer}
updateOffer={setIsOfferCreated}
/>
</AppDrawer>
</AccordionDetails>
</Accordion>
)
Expand Down
17 changes: 4 additions & 13 deletions src/components/profile-item/ProfileItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,25 @@ import { styles } from '~/components/profile-item/ProfileItem.styles'
import useBreakpoints from '~/hooks/use-breakpoints'
import { ProfileItemType } from '~/components/profile-item/complete-profile.constants'
import { UserRoleEnum } from '~/types'
import CreateOffer from '~/containers/offer-page/create-offer/CreateOffer'
import { useDrawer } from '~/hooks/use-drawer'
import AppDrawer from '~/components/app-drawer/AppDrawer'

interface ProfileItemProps {
item: ProfileItemType
isFilled?: boolean
userRole: UserRoleEnum | ''
handleOpenDrawer?: () => void | undefined
}

const ProfileItem = ({
item,
userRole,
isFilled = false
isFilled = false,
handleOpenDrawer = undefined
}: ProfileItemProps) => {
const { t } = useTranslation()
const { isMobile } = useBreakpoints()
const { id, icon } = item
const navigate = useNavigate()

const { openDrawer, closeDrawer, isOpen } = useDrawer()
const handleOpenDrawer = () => openDrawer()

const isClickable = !isFilled && item.id !== 'schedule'
const isOffer = item.id === 'offer'

Expand All @@ -39,7 +35,7 @@ const ProfileItem = ({
navigate(`${item.path}#${item.id}`)
}
if (isOffer) {
handleOpenDrawer()
handleOpenDrawer!()
}
}

Expand All @@ -64,11 +60,6 @@ const ProfileItem = ({
</Typography>
</Box>
</Box>
{isOffer && (
<AppDrawer onClose={closeDrawer} open={isOpen}>
<CreateOffer closeDrawer={closeDrawer} />
</AppDrawer>
)}
</Box>
{isFilled && (
<CheckIcon
Expand Down
16 changes: 13 additions & 3 deletions src/containers/offer-page/create-offer/CreateOffer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useCallback } from 'react'
import { FC, useCallback, Dispatch, SetStateAction } from 'react'

import { OfferService } from '~/services/offer-service'
import CreateOrEditOffer from '~/containers/offer-page/create-or-edit-offer/CreateOrEditOffer'
Expand All @@ -8,16 +8,26 @@ import { CreateOrUpdateOfferData } from '~/types'

interface CreateOfferProps {
closeDrawer: () => void
updateOffer?: Dispatch<SetStateAction<boolean>> | undefined
}

const CreateOffer: FC<CreateOfferProps> = ({ closeDrawer }) => {
const CreateOffer: FC<CreateOfferProps> = ({
closeDrawer,
updateOffer = undefined
}) => {
const postOffer = useCallback(
(data: CreateOrUpdateOfferData) =>
OfferService.createOffer({ ...data, FAQ: findFullObjects(data.FAQ) }),
[]
)

return <CreateOrEditOffer closeDrawer={closeDrawer} service={postOffer} />
return (
<CreateOrEditOffer
closeDrawer={closeDrawer}
service={postOffer}
updateOffer={updateOffer}
/>
)
}

export default CreateOffer
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useEffect } from 'react'
import { FC, useEffect, Dispatch, SetStateAction } from 'react'
import { useTranslation } from 'react-i18next'
import { useNavigate, useLocation } from 'react-router-dom'
import LeakAddSharpIcon from '@mui/icons-material/LeakAddSharp'
Expand Down Expand Up @@ -38,15 +38,17 @@ import { openAlert } from '~/redux/features/snackbarSlice'
import { getErrorKey } from '~/utils/get-error-key'

interface CreateOrUpdateOfferProps {
existingOffer: Offer | null
existingOffer?: Offer | null
closeDrawer: () => void
service: ServiceFunction<Offer | null, CreateOrUpdateOfferData>
updateOffer?: Dispatch<SetStateAction<boolean>> | undefined
}

const CreateOrEditOffer: FC<CreateOrUpdateOfferProps> = ({
existingOffer = null,
closeDrawer,
service
service,
updateOffer = undefined
}) => {
const { userRole } = useAppSelector((state) => state.appMain)
const { setNeedConfirmation } = useConfirm()
Expand Down Expand Up @@ -77,6 +79,7 @@ const CreateOrEditOffer: FC<CreateOrUpdateOfferProps> = ({
closeDrawer()
if (hash == '#offer') {
navigate(`${authRoutes.myProfile.path}#complete`)
updateOffer!(true)
} else {
navigate(
createUrlPath(
Expand Down

0 comments on commit 1107990

Please sign in to comment.