Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
products_lesson_front
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Pavel Mishakov
products_lesson_front
Commits
51c74b07
Commit
51c74b07
authored
Mar 24, 2023
by
Pavel Mishakov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
start lesson 90
parent
38cbf3b1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
2 deletions
+32
-2
ProductItem.tsx
src/components/ProductItem/ProductItem.tsx
+31
-1
vite.config.ts
vite.config.ts
+1
-1
No files found.
src/components/ProductItem/ProductItem.tsx
View file @
51c74b07
import
React
,
{
FunctionComponent
,
ReactElement
}
from
"react"
;
import
React
,
{
FunctionComponent
,
MutableRefObject
,
ReactElement
,
useRef
}
from
"react"
;
import
IProductItemProps
from
"./IProductItemProps"
;
import
styles
from
'./ProductItem.module.css'
import
defaultImage
from
'../../assets/images/default.jpg'
...
...
@@ -8,9 +8,36 @@ import { deleteProductById } from "../../store/products/products.slice";
const
ProductItem
:
FunctionComponent
<
IProductItemProps
>
=
(
props
):
ReactElement
=>
{
const
dispatch
:
AppDispatch
=
useDispatch
()
const
anchorRef
:
MutableRefObject
<
HTMLAnchorElement
|
null
>
=
useRef
(
null
)
const
deleteHandler
=
():
void
=>
{
dispatch
(
deleteProductById
(
props
.
product
.
_id
))
}
const
download
=
(
url
:
string
,
name
:
string
)
=>
{
// const [fetching, setFetching] = useState(false);
// const [error, setError] = useState(false);
if
(
!
url
)
{
throw
new
Error
(
"Resource URL not provided! You need to provide one"
);
}
//setFetching(true);
fetch
(
url
)
.
then
(
response
=>
response
.
blob
())
.
then
(
blob
=>
{
//setFetching(false);
const
blobURL
=
URL
.
createObjectURL
(
blob
);
const
a
=
document
.
createElement
(
"a"
);
a
.
href
=
blobURL
;
// a.style = "display: none";
if
(
name
&&
name
.
length
)
a
.
download
=
name
;
document
.
body
.
appendChild
(
a
);
a
.
click
();
})
// .catch(() => setError(true));
.
catch
((
e
)
=>
console
.
log
(
e
))
};
return
(
<
div
className=
{
styles
.
ProductItem
}
>
<
div
className=
{
styles
.
ProductItem__imageFrame
}
>
...
...
@@ -23,6 +50,9 @@ const ProductItem: FunctionComponent<IProductItemProps> = (props): ReactElement
}
}
/>
</
div
>
<
div
>
<
a
onClick=
{
()
=>
download
(
`${import.meta.env.VITE_BASE_URL}uploads/${props.product.image}`
,
props
.
product
.
image
)
}
ref=
{
anchorRef
}
download
>
Download image
</
a
>
</
div
>
<
h3
>
{
props
.
product
.
product
}
</
h3
>
<
p
>
{
props
.
product
.
description
}
</
p
>
<
p
>
{
props
.
product
.
price
}
</
p
>
...
...
vite.config.ts
View file @
51c74b07
...
...
@@ -5,6 +5,6 @@ import react from '@vitejs/plugin-react'
export
default
defineConfig
({
plugins
:
[
react
()],
server
:
{
port
:
300
0
port
:
300
3
}
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment