|
795 | 795 | this.password_strength_checker = new window.WU_PasswordStrength({ |
796 | 796 | pass1: pass1_el, |
797 | 797 | result: jQuery('#pass-strength-result'), |
798 | | - minStrength: 3, |
799 | 798 | onValidityChange(isValid) { |
800 | 799 |
|
801 | 800 | that.valid_password = isValid; |
|
813 | 812 | }, 500), |
814 | 813 | check_user_exists(field_type, value) { |
815 | 814 |
|
| 815 | + // Don't let other field checks interfere with an active email prompt |
| 816 | + if (this.show_login_prompt && this.login_prompt_field === 'email' && field_type !== 'email') { |
| 817 | + return; |
| 818 | + } |
| 819 | + |
816 | 820 | // Don't check if value is too short |
817 | 821 | if (! value || value.length < 3) { |
818 | 822 |
|
819 | | - this.show_login_prompt = false; |
| 823 | + if (this.login_prompt_field === field_type) { |
| 824 | + this.show_login_prompt = false; |
| 825 | + this.remove_field_error(field_type === 'email' ? 'email_address' : 'username'); |
| 826 | + } |
820 | 827 |
|
821 | 828 | return; |
822 | 829 |
|
|
840 | 847 | that.show_login_prompt = true; |
841 | 848 | that.login_prompt_field = field_type; |
842 | 849 |
|
843 | | - } else { |
| 850 | + that.add_field_error(field_type === 'email' ? 'email_address' : 'username', wu_checkout.i18n.email_exists); |
| 851 | + |
| 852 | + } else if (that.login_prompt_field === field_type) { |
844 | 853 |
|
845 | 854 | that.show_login_prompt = false; |
| 855 | + that.remove_field_error(field_type === 'email' ? 'email_address' : 'username'); |
846 | 856 |
|
847 | 857 | } |
848 | 858 |
|
849 | 859 | }, function(error) { |
850 | 860 |
|
851 | 861 | that.checking_user_exists = false; |
852 | | - that.show_login_prompt = false; |
| 862 | + |
| 863 | + if (that.login_prompt_field === field_type) { |
| 864 | + that.show_login_prompt = false; |
| 865 | + that.remove_field_error(field_type === 'email' ? 'email_address' : 'username'); |
| 866 | + } |
853 | 867 |
|
854 | 868 | }); |
855 | 869 |
|
|
914 | 928 |
|
915 | 929 | return false; |
916 | 930 |
|
| 931 | + }, |
| 932 | + add_field_error(field_code, message) { |
| 933 | + |
| 934 | + this.remove_field_error(field_code); |
| 935 | + |
| 936 | + this.errors.push({ |
| 937 | + code: field_code, |
| 938 | + message, |
| 939 | + }); |
| 940 | + |
| 941 | + }, |
| 942 | + remove_field_error(field_code) { |
| 943 | + |
| 944 | + this.errors = this.errors.filter(function(e) { |
| 945 | + |
| 946 | + return e.code !== field_code; |
| 947 | + |
| 948 | + }); |
| 949 | + |
917 | 950 | }, |
918 | 951 | dismiss_login_prompt() { |
919 | 952 |
|
|
929 | 962 | // Setup handlers for both email and username field types |
930 | 963 | [ 'email', 'username' ].forEach(function(fieldType) { |
931 | 964 |
|
| 965 | + const loginPromptContainer = document.getElementById('wu-inline-login-prompt-' + fieldType); |
| 966 | + |
| 967 | + if (! loginPromptContainer) { |
| 968 | + return; |
| 969 | + } |
| 970 | + |
| 971 | + // Only attach handlers once per container |
| 972 | + if (loginPromptContainer.dataset.wuHandlersAttached) { |
| 973 | + return; |
| 974 | + } |
| 975 | + |
| 976 | + loginPromptContainer.dataset.wuHandlersAttached = '1'; |
| 977 | + |
932 | 978 | const passwordField = document.getElementById('wu-inline-login-password-' + fieldType); |
933 | 979 | const submitButton = document.getElementById('wu-inline-login-submit-' + fieldType); |
934 | 980 |
|
935 | 981 | if (! passwordField || ! submitButton) { |
936 | 982 | return; |
937 | 983 | } |
938 | 984 |
|
939 | | - const dismissButton = document.getElementById('wu-dismiss-login-prompt-' + fieldType); |
940 | 985 | const errorDiv = document.getElementById('wu-login-error-' + fieldType); |
941 | | - const loginPromptContainer = document.getElementById('wu-inline-login-prompt-' + fieldType); |
942 | 986 |
|
943 | | - // Remove any existing listeners to avoid duplicates |
944 | | - const newSubmitButton = submitButton.cloneNode(true); |
945 | | - submitButton.parentNode.replaceChild(newSubmitButton, submitButton); |
| 987 | + function showError(message) { |
| 988 | + |
| 989 | + errorDiv.textContent = message; |
| 990 | + errorDiv.classList.remove('wu-hidden'); |
| 991 | + |
| 992 | + } |
| 993 | + |
| 994 | + function hideError() { |
| 995 | + |
| 996 | + errorDiv.classList.add('wu-hidden'); |
| 997 | + |
| 998 | + } |
946 | 999 |
|
947 | | - const newPasswordField = passwordField.cloneNode(true); |
948 | | - passwordField.parentNode.replaceChild(newPasswordField, passwordField); |
949 | 1000 | function handleError(error) { |
950 | 1001 |
|
951 | | - newSubmitButton.disabled = false; |
952 | | - newSubmitButton.textContent = wu_checkout.i18n.sign_in || 'Sign in'; |
| 1002 | + submitButton.disabled = false; |
| 1003 | + submitButton.textContent = wu_checkout.i18n.sign_in || 'Sign in'; |
953 | 1004 |
|
954 | 1005 | if (error.data && error.data.message) { |
955 | 1006 |
|
956 | | - errorDiv.textContent = error.data.message; |
| 1007 | + showError(error.data.message); |
957 | 1008 |
|
958 | 1009 | } else { |
959 | 1010 |
|
960 | | - errorDiv.textContent = wu_checkout.i18n.login_failed || 'Login failed. Please try again.'; |
| 1011 | + showError(wu_checkout.i18n.login_failed || 'Login failed. Please try again.'); |
961 | 1012 |
|
962 | 1013 | } |
963 | 1014 |
|
964 | | - errorDiv.style.display = 'block'; |
965 | | - |
966 | 1015 | } |
967 | 1016 |
|
968 | 1017 | function handleLogin(e) { |
|
971 | 1020 | e.stopPropagation(); |
972 | 1021 | e.stopImmediatePropagation(); |
973 | 1022 |
|
974 | | - const password = newPasswordField.value; |
| 1023 | + const password = passwordField.value; |
975 | 1024 |
|
976 | 1025 | if (! password) { |
977 | 1026 |
|
978 | | - errorDiv.textContent = wu_checkout.i18n.password_required || 'Password is required'; |
979 | | - errorDiv.style.display = 'block'; |
| 1027 | + showError(wu_checkout.i18n.password_required || 'Password is required'); |
980 | 1028 |
|
981 | 1029 | return false; |
982 | 1030 |
|
983 | 1031 | } |
984 | 1032 |
|
985 | | - newSubmitButton.disabled = true; |
986 | | - newSubmitButton.innerHTML = '<span class="spinner is-active wu-inline-block" style="float: none; width: 16px; height: 16px; margin: 0 4px 0 0;"></span>' + (wu_checkout.i18n.logging_in || 'Logging in...'); |
987 | | - errorDiv.style.display = 'none'; |
| 1033 | + submitButton.disabled = true; |
| 1034 | + submitButton.innerHTML = '<span class="spinner is-active wu-inline-block" style="float: none; width: 16px; height: 16px; margin: 0 4px 0 0;"></span>' + (wu_checkout.i18n.logging_in || 'Logging in...'); |
| 1035 | + hideError(); |
988 | 1036 |
|
989 | 1037 | const username_or_email = fieldType === 'email' ? that.email_address : that.username; |
990 | 1038 |
|
|
1015 | 1063 | } |
1016 | 1064 |
|
1017 | 1065 | // Stop all events from bubbling out of the login prompt |
1018 | | - if (loginPromptContainer) { |
1019 | | - |
1020 | | - loginPromptContainer.addEventListener('click', function(e) { |
| 1066 | + loginPromptContainer.addEventListener('click', function(e) { |
1021 | 1067 |
|
1022 | | - e.stopPropagation(); |
1023 | | - |
1024 | | - }); |
| 1068 | + e.stopPropagation(); |
1025 | 1069 |
|
1026 | | - loginPromptContainer.addEventListener('keydown', function(e) { |
| 1070 | + }); |
1027 | 1071 |
|
1028 | | - e.stopPropagation(); |
| 1072 | + loginPromptContainer.addEventListener('keydown', function(e) { |
1029 | 1073 |
|
1030 | | - }); |
| 1074 | + e.stopPropagation(); |
1031 | 1075 |
|
1032 | | - loginPromptContainer.addEventListener('keyup', function(e) { |
| 1076 | + }); |
1033 | 1077 |
|
1034 | | - e.stopPropagation(); |
| 1078 | + loginPromptContainer.addEventListener('keyup', function(e) { |
1035 | 1079 |
|
1036 | | - }); |
| 1080 | + e.stopPropagation(); |
1037 | 1081 |
|
1038 | | - } |
| 1082 | + }); |
1039 | 1083 |
|
1040 | | - newSubmitButton.addEventListener('click', handleLogin); |
| 1084 | + submitButton.addEventListener('click', handleLogin); |
1041 | 1085 |
|
1042 | | - newPasswordField.addEventListener('keydown', function(e) { |
| 1086 | + passwordField.addEventListener('keydown', function(e) { |
1043 | 1087 |
|
1044 | 1088 | if (e.key === 'Enter') { |
1045 | 1089 |
|
|
1049 | 1093 |
|
1050 | 1094 | }); |
1051 | 1095 |
|
1052 | | - if (dismissButton) { |
1053 | | - |
1054 | | - dismissButton.addEventListener('click', function(e) { |
1055 | | - |
1056 | | - e.preventDefault(); |
1057 | | - e.stopPropagation(); |
1058 | | - that.show_login_prompt = false; |
1059 | | - that.inline_login_password = ''; |
1060 | | - newPasswordField.value = ''; |
1061 | | - |
1062 | | - }); |
1063 | | - |
1064 | | - } |
1065 | | - |
1066 | 1096 | }); |
1067 | 1097 |
|
1068 | 1098 | }, |
|
1078 | 1108 | // Setup inline login handlers if prompt is visible |
1079 | 1109 | this.setup_inline_login_handlers(); |
1080 | 1110 |
|
| 1111 | + // Re-initialize password strength if field appeared after mount |
| 1112 | + if (! this.password_strength_checker && jQuery('#field-password').length) { |
| 1113 | + this.init_password_strength(); |
| 1114 | + } |
| 1115 | + |
1081 | 1116 | }); |
1082 | 1117 |
|
1083 | 1118 | }, |
|
1159 | 1194 |
|
1160 | 1195 | }, |
1161 | 1196 | watch: { |
| 1197 | + email_address: _.debounce(function(new_value) { |
| 1198 | + |
| 1199 | + this.check_user_exists('email', new_value); |
| 1200 | + |
| 1201 | + }, 500), |
1162 | 1202 | products(new_value, old_value) { |
1163 | 1203 |
|
1164 | 1204 | this.on_change_product(new_value, old_value); |
|
0 commit comments