Skip to content

Commit

Permalink
fix(sonar bugs): replacing promise for fetching images
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhigarg-bmw committed Aug 7, 2023
1 parent 18447d9 commit d78900d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
38 changes: 29 additions & 9 deletions src/components/pages/AdminBoardDetail/BoardContentDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,42 @@ import BoardPrivacy from './components/BoardPrivacy'
import BoardRoles from './components/BoardRoles'
import './AdminBoardDetail.scss'
import BoardTechnicalUserSetup from './components/BoardTechnicalUserSetup'
import { fetchImageWithToken } from 'services/ImageService'

export default function BoardContentDetails({ item }: { item: AppDetails }) {
const { t } = useTranslation()
const navigate = useNavigate()
const [images, setImages] = useState<any>()
const [images, setImages] = useState<any>([])
console.log('images', images)

useEffect(() => {
if (item) {
const newPromies = CommonService.fetchLeadPictures(item.images, item.id)
Promise.all(newPromies)
.then((result) => {
setImages(result.flat())
})
.catch((err) => {
console.log(err)
})
item.images?.map((image: any) => {
console.log('image', image)
let url = CommonService.getImageURL(image, item.id)
return fetchImageWithToken(url)
.then((buffer) => {
const urlObj = {
url: URL.createObjectURL(
new Blob([buffer], { type: 'image/png' })
),
text: '',
}
console.log('urlObj', urlObj)
setImages((oldArray: any) => [...oldArray, urlObj])
})
.catch((err) => {
console.log(err)
})
})
// const newPromies = CommonService.fetchLeadPictures(item.images, item.id)
// Promise.all(newPromies)
// .then((result) => {
// setImages(result.flat())
// })
// .catch((err) => {
// console.log(err)
// })
}
}, [item])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default function BoardHeader({ item }: AppDetailHeaderProps) {
<Typography variant="h2" sx={{ mb: 1.5, mt: 1.5 }}>
{item.title}
</Typography>
<Grid md={8}>
<Grid md={8} item>
{[CardDetails.LANGUAGE, CardDetails.USECASE, CardDetails.PRICE].map(
(field) => (
<div style={{ display: 'flex', marginBottom: '5px' }} key={field}>
Expand Down
15 changes: 15 additions & 0 deletions src/services/CommonService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ const fetchLeadPictures = (images: string[], appId: string) => {
return newPromies
}

const getImageURL = (image: any, appId: string) => {
let url = ''
if (!image.documentId) {
url = `${getApiBase()}/api/apps/${appId}/appDocuments/${isValidPictureId(
image
)}`
} else {
url = `${getApiBase()}/api/apps/${appId}/appDocuments/${isValidPictureId(
image.documentId
)}`
}
return url
}

const isValidPictureId = (id: string) => {
return id === '00000000-0000-0000-0000-000000000000'
? '00000000-0000-0000-0000-000000000001'
Expand Down Expand Up @@ -156,6 +170,7 @@ const CommonService = {
fetchLeadPictures,
getRoleDescription,
getCompanyRoleUpdateData,
getImageURL,
}

export default CommonService

0 comments on commit d78900d

Please sign in to comment.