Skip to content
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

Fix(sonar bugs): replacing promises for fetching images #198

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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