mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
44 lines
1.6 KiB
CoffeeScript
44 lines
1.6 KiB
CoffeeScript
angular.module('Darkswarm').controller "CheckoutCtrl", ($scope, localStorageService, Checkout, CurrentUser, CurrentHub, $http) ->
|
|
$scope.Checkout = Checkout
|
|
$scope.submitted = false
|
|
|
|
# Bind to local storage
|
|
$scope.fieldsToBind = ["bill_address", "email", "payment_method_id", "shipping_method_id", "ship_address"]
|
|
prefix = "order_#{Checkout.order.id}#{CurrentUser.id or ""}#{CurrentHub.hub.id}"
|
|
|
|
for field in $scope.fieldsToBind
|
|
localStorageService.bind $scope, "Checkout.order.#{field}", Checkout.order[field], "#{prefix}_#{field}"
|
|
|
|
localStorageService.bind $scope, "Checkout.ship_address_same_as_billing", true, "#{prefix}_sameasbilling"
|
|
localStorageService.bind $scope, "Checkout.default_bill_address", false, "#{prefix}_defaultasbilladdress"
|
|
localStorageService.bind $scope, "Checkout.default_ship_address", false, "#{prefix}_defaultasshipaddress"
|
|
|
|
$scope.order = Checkout.order # Ordering is important
|
|
$scope.secrets = Checkout.secrets
|
|
|
|
$scope.enabled = !!CurrentUser.id?
|
|
|
|
$scope.purchase = (event, form) ->
|
|
event.preventDefault()
|
|
$scope.formdata = form
|
|
$scope.submitted = true
|
|
|
|
if CurrentUser.id
|
|
$scope.validateForm(form)
|
|
else
|
|
$scope.ensureUserIsGuest()
|
|
|
|
$scope.validateForm = ->
|
|
if $scope.formdata.$valid
|
|
$scope.Checkout.purchase()
|
|
else
|
|
$scope.$broadcast 'purchaseFormInvalid', $scope.formdata
|
|
|
|
$scope.ensureUserIsGuest = (callback = null) ->
|
|
$http.post("/user/registered_email", {email: $scope.order.email})
|
|
.then (response)->
|
|
window.CableReady.perform(response.data)
|
|
.catch ->
|
|
$scope.validateForm() if $scope.submitted
|
|
callback() if callback
|