2020-05-19 05:02:56 -04:00
|
|
|
import App from '../base'
|
2021-09-14 04:55:09 -04:00
|
|
|
import '../vendor/libs/passfield'
|
2020-05-19 05:02:56 -04:00
|
|
|
App.directive('asyncForm', ($http, validateCaptcha, validateCaptchaV3) => ({
|
|
|
|
controller: [
|
|
|
|
'$scope',
|
2021-03-17 09:29:43 -04:00
|
|
|
'$location',
|
2021-04-14 09:17:21 -04:00
|
|
|
function ($scope, $location) {
|
2020-05-19 05:02:56 -04:00
|
|
|
this.getEmail = () => $scope.email
|
2021-03-17 09:29:43 -04:00
|
|
|
this.getEmailFromQuery = () =>
|
|
|
|
$location.search().email || $location.search().new_email
|
2020-05-19 05:02:56 -04:00
|
|
|
return this
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
],
|
2021-03-17 09:29:43 -04:00
|
|
|
link(scope, element, attrs, ctrl) {
|
2020-05-19 05:02:56 -04:00
|
|
|
let response
|
|
|
|
const formName = attrs.asyncForm
|
|
|
|
|
|
|
|
scope[attrs.name].response = response = {}
|
|
|
|
scope[attrs.name].inflight = false
|
2021-03-26 05:53:01 -04:00
|
|
|
scope.email =
|
|
|
|
scope.email ||
|
|
|
|
scope.usersEmail ||
|
|
|
|
ctrl.getEmailFromQuery() ||
|
|
|
|
attrs.newEmail
|
2020-05-19 05:02:56 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
const validateCaptchaIfEnabled = function (callback) {
|
2021-07-15 05:18:42 -04:00
|
|
|
scope.$applyAsync(() => {
|
|
|
|
scope[attrs.name].inflight = true
|
|
|
|
})
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (attrs.captchaActionName) {
|
|
|
|
validateCaptchaV3(attrs.captchaActionName)
|
|
|
|
}
|
|
|
|
if (attrs.captcha != null) {
|
2020-09-15 08:49:02 -04:00
|
|
|
validateCaptcha(callback)
|
2020-05-19 05:02:56 -04:00
|
|
|
} else {
|
2020-09-15 08:49:02 -04:00
|
|
|
callback()
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 05:18:42 -04:00
|
|
|
const _submitRequest = function (grecaptchaResponse) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const formData = {}
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const data of Array.from(element.serializeArray())) {
|
2020-05-19 05:02:56 -04:00
|
|
|
formData[data.name] = data.value
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
|
2020-09-15 08:49:02 -04:00
|
|
|
if (grecaptchaResponse) {
|
2020-05-19 05:02:56 -04:00
|
|
|
formData['g-recaptcha-response'] = grecaptchaResponse
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
// clear the response object which may be referenced downstream
|
|
|
|
Object.keys(response).forEach(field => delete response[field])
|
|
|
|
|
|
|
|
// for asyncForm prevent automatic redirect to /login if
|
|
|
|
// authentication fails, we will handle it ourselves
|
|
|
|
const httpRequestFn = _httpRequestFn(element.attr('method'))
|
|
|
|
return httpRequestFn(element.attr('action'), formData, {
|
2021-04-27 03:52:58 -04:00
|
|
|
disableAutoLoginRedirect: true,
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
.then(function (httpResponse) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const { data, headers } = httpResponse
|
|
|
|
scope[attrs.name].inflight = false
|
|
|
|
response.success = true
|
|
|
|
response.error = false
|
|
|
|
|
|
|
|
const onSuccessHandler = scope[attrs.onSuccess]
|
|
|
|
if (onSuccessHandler) {
|
|
|
|
onSuccessHandler(httpResponse)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-15 08:49:02 -04:00
|
|
|
if (data.redir) {
|
2020-05-19 05:02:56 -04:00
|
|
|
ga('send', 'event', formName, 'success')
|
|
|
|
return (window.location = data.redir)
|
2020-09-15 08:49:02 -04:00
|
|
|
} else if (data.message) {
|
2020-05-19 05:02:56 -04:00
|
|
|
response.message = data.message
|
|
|
|
|
|
|
|
if (data.message.type === 'error') {
|
|
|
|
response.success = false
|
|
|
|
response.error = true
|
|
|
|
return ga('send', 'event', formName, 'failure', data.message)
|
|
|
|
} else {
|
|
|
|
return ga('send', 'event', formName, 'success')
|
|
|
|
}
|
|
|
|
} else if (scope.$eval(attrs.asyncFormDownloadResponse)) {
|
|
|
|
const blob = new Blob([data], {
|
2021-04-27 03:52:58 -04:00
|
|
|
type: headers('Content-Type'),
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
location.href = URL.createObjectURL(blob) // Trigger file save
|
|
|
|
}
|
2018-12-20 06:50:58 -05:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
.catch(function (httpResponse) {
|
2020-09-15 08:49:02 -04:00
|
|
|
const { data, status } = httpResponse
|
2020-05-19 05:02:56 -04:00
|
|
|
scope[attrs.name].inflight = false
|
|
|
|
response.success = false
|
|
|
|
response.error = true
|
|
|
|
response.status = status
|
|
|
|
response.data = data
|
|
|
|
|
|
|
|
const onErrorHandler = scope[attrs.onError]
|
|
|
|
if (onErrorHandler) {
|
|
|
|
onErrorHandler(httpResponse)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-15 08:49:02 -04:00
|
|
|
let responseMessage
|
|
|
|
if (data.message && data.message.text) {
|
|
|
|
responseMessage = data.message.text
|
|
|
|
} else {
|
|
|
|
responseMessage = data.message
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status === 400) {
|
2020-05-19 05:02:56 -04:00
|
|
|
// Bad Request
|
|
|
|
response.message = {
|
2020-09-15 08:49:02 -04:00
|
|
|
text:
|
|
|
|
responseMessage ||
|
|
|
|
'Invalid Request. Please correct the data and try again.',
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'error',
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
} else if (status === 403) {
|
|
|
|
// Forbidden
|
|
|
|
response.message = {
|
|
|
|
text:
|
2020-09-15 08:49:02 -04:00
|
|
|
responseMessage ||
|
2020-05-19 05:02:56 -04:00
|
|
|
'Session error. Please check you have cookies enabled. If the problem persists, try clearing your cache and cookies.',
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'error',
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
} else if (status === 429) {
|
|
|
|
response.message = {
|
2020-09-15 08:49:02 -04:00
|
|
|
text:
|
|
|
|
responseMessage ||
|
|
|
|
'Too many attempts. Please wait for a while and try again.',
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'error',
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
} else {
|
|
|
|
response.message = {
|
|
|
|
text:
|
2020-09-15 08:49:02 -04:00
|
|
|
responseMessage ||
|
2020-05-19 05:02:56 -04:00
|
|
|
'Something went wrong talking to the server :(. Please try again.',
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'error',
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:49:02 -04:00
|
|
|
ga('send', 'event', formName, 'failure', data.message)
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const submit = () =>
|
2021-07-15 05:18:42 -04:00
|
|
|
validateCaptchaIfEnabled(response => _submitRequest(response))
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const _httpRequestFn = (method = 'post') => {
|
|
|
|
const $HTTP_FNS = {
|
|
|
|
post: $http.post,
|
2021-04-27 03:52:58 -04:00
|
|
|
get: $http.get,
|
2018-12-20 06:50:58 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return $HTTP_FNS[method.toLowerCase()]
|
|
|
|
}
|
2018-12-20 06:50:58 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
element.on('submit', function (e) {
|
2020-05-19 05:02:56 -04:00
|
|
|
e.preventDefault()
|
2020-09-15 08:49:02 -04:00
|
|
|
submit()
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (attrs.autoSubmit) {
|
2020-09-15 08:49:02 -04:00
|
|
|
submit()
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
}))
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
App.directive('formMessages', () => ({
|
|
|
|
restrict: 'E',
|
|
|
|
template: `\
|
2018-11-05 05:06:39 -05:00
|
|
|
<div class="alert" ng-class="{
|
2020-05-19 05:02:56 -04:00
|
|
|
'alert-danger': form.response.message.type == 'error',
|
|
|
|
'alert-success': form.response.message.type != 'error'
|
2018-12-14 15:56:36 -05:00
|
|
|
}" ng-show="!!form.response.message" ng-bind-html="form.response.message.text">
|
2018-11-05 05:06:39 -05:00
|
|
|
</div>
|
|
|
|
<div ng-transclude></div>\
|
|
|
|
`,
|
2020-05-19 05:02:56 -04:00
|
|
|
transclude: true,
|
|
|
|
scope: {
|
2021-04-27 03:52:58 -04:00
|
|
|
form: '=for',
|
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
}))
|