Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
T
test-webshop
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
Artem
test-webshop
Commits
15453a4d
Commit
15453a4d
authored
May 17, 2020
by
Artem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
last change for cart js for today, I go to sleep
parent
b6aa3bfc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
72 deletions
+53
-72
cart.js
public/js/cart.js
+53
-72
No files found.
public/js/cart.js
View file @
15453a4d
...
...
@@ -58,36 +58,12 @@ class Cart {
this
.
loaderElem
.
classList
.
toggle
(
'loader-show'
);
}
sendRequest
(
method
,
action
,
data
,
onsuccess
,
onerror
)
{
let
request
;
if
(
method
===
'post'
)
{
request
=
axios
.
post
;
}
else
if
(
method
===
'get'
)
{
request
=
axios
.
get
;
}
else
{
try
{
throw
new
Error
(
'Expected get or post request'
);
}
catch
(
e
)
{
console
.
log
(
e
.
name
+
': '
+
e
.
message
);
}
}
request
(
action
,
data
)
.
then
(
(
response
)
=>
{
onsuccess
(
response
);
})
.
catch
(
(
error
)
=>
{
onerror
(
error
);
});
}
updateItemQty
(
event
)
{
if
(
!
event
.
target
.
classList
.
contains
(
'js-qty-input'
))
{
return
;
}
const
qtyInputElem
=
event
.
target
;
const
changedQty
=
qtyInputElem
.
value
;
const
action
=
this
.
updateItemUrl
;
const
productId
=
qtyInputElem
.
dataset
.
productId
;
if
(
!
changedQty
||
(
changedQty
.
indexOf
(
'e'
)
+
1
)
||
changedQty
===
'0'
)
{
...
...
@@ -97,21 +73,22 @@ class Cart {
this
.
toggleLoager
();
const
onsuccess
=
(
response
)
=>
{
this
.
show
();
toastr
.
success
(
'Quantity updated'
);
};
const
onerror
=
(
error
)
=>
{
console
.
log
(
error
);
this
.
toggleLoager
();
toastr
.
error
(
'Error, something went wrong'
);
}
this
.
sendUpdateItemQtyRequest
({
productId
,
changedQty
})
.
then
(
(
response
)
=>
{
this
.
show
();
toastr
.
success
(
'Quantity updated'
);
})
.
catch
(
(
error
)
=>
{
console
.
log
(
error
);
this
.
toggleLoager
();
toastr
.
error
(
'Error, something went wrong'
);
});
};
this
.
sendRequest
(
'post'
,
action
,
{
productId
,
changedQty
},
onsuccess
,
onerror
);
sendUpdateItemQtyRequest
(
data
)
{
return
axios
.
post
(
this
.
updateItemUrl
,
data
);
}
removeItemFromCart
(
event
)
{
if
(
!
event
.
target
.
classList
.
contains
(
'js-remove-btn'
))
{
return
;
...
...
@@ -121,18 +98,20 @@ class Cart {
this
.
toggleLoager
();
const
onsuccess
=
(
response
)
=>
{
this
.
show
();
toastr
.
success
(
'Product deleted'
);
};
const
onerror
=
(
error
)
=>
{
console
.
log
(
error
);
this
.
toggleLoager
();
toastr
.
error
(
'Error, something went wrong'
);
}
this
.
sendRemoveItemRequest
({
productId
})
.
then
(
(
response
)
=>
{
this
.
show
();
toastr
.
success
(
'Product deleted'
);
})
.
catch
(
(
error
)
=>
{
console
.
log
(
error
);
this
.
toggleLoager
();
toastr
.
error
(
'Error, something went wrong'
);
});
}
this
.
sendRequest
(
'post'
,
action
,
{
productId
},
onsuccess
,
onerror
);
sendRemoveItemRequest
(
data
)
{
return
axios
.
post
(
this
.
removeItemUrl
,
data
);
}
events
()
{
...
...
@@ -147,32 +126,34 @@ class Cart {
this
.
loaderElem
.
classList
.
toggle
(
'loader-show'
);
}
const
onsuccess
=
(
response
)
=>
{
const
redirectLink
=
response
.
data
.
redirectLink
;
if
(
redirectLink
)
{
window
.
location
.
replace
(
redirectLink
);
return
;
}
this
.
products
=
response
.
data
.
products
;
this
.
positionsAmount
=
response
.
data
.
positionsAmount
;
this
.
totalPrice
=
response
.
data
.
totalPrice
;
this
.
showProducts
();
this
.
toggleLoager
();
if
(
!
this
.
hasEvents
)
{
this
.
events
();
}
};
const
onerror
=
(
error
)
=>
{
console
.
log
(
error
);
this
.
toggleLoager
();
toastr
.
error
(
'Error, something went wrong'
);
}
this
.
getCurrentProducts
()
.
then
(
(
response
)
=>
{
const
redirectLink
=
response
.
data
.
redirectLink
;
if
(
redirectLink
)
{
window
.
location
.
replace
(
redirectLink
);
return
;
}
this
.
products
=
response
.
data
.
products
;
this
.
positionsAmount
=
response
.
data
.
positionsAmount
;
this
.
totalPrice
=
response
.
data
.
totalPrice
;
this
.
showProducts
();
this
.
toggleLoager
();
if
(
!
this
.
hasEvents
)
{
this
.
events
();
}
})
.
catch
(
(
error
)
=>
{
console
.
log
(
error
);
this
.
toggleLoager
();
toastr
.
error
(
'Error, something went wrong'
);
});
}
this
.
sendRequest
(
'get'
,
this
.
getItemsUrl
,
null
,
onsuccess
,
onerror
);
getCurrentProducts
()
{
return
axios
.
get
(
this
.
getItemsUrl
);
}
}
...
...
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