tum 2 年之前
父節點
當前提交
4871346084
共有 1 個文件被更改,包括 52 次插入33 次删除
  1. 52 33
      src/views/SignupPage.vue

+ 52 - 33
src/views/SignupPage.vue

@@ -11,37 +11,14 @@
11 11
       </ion-toolbar>
12 12
     </ion-header>
13 13
     <ion-content class='ion-padding'>
14
-      <ion-list>
15
-        <ion-item>
14
+
16 15
           <ion-input label="Username"  ref="username" label-placement="floating" fill="outline" placeholder="Example, john"
17
-                                                                                                error-text="Help me" 
18 16
                                                                                                 ></ion-input>
19
-        </ion-item>
20
-        <ion-item>
21 17
           <ion-input label="Email" label-placement="floating" ref='email' fill="outline" type="email" placeholder="Example, hello@gmail.com"></ion-input>
22
-        </ion-item>
23
-        <ion-item>
24 18
           <ion-input label="Password" label-placement='floating' fill='outline' ref="password" type="password" placeholder="Password"></ion-input>
25
-        </ion-item>
26
-        <ion-item>
27 19
           <ion-input label="Password Confirmation" label-placement='floating' fill='outline' ref="password2" type="password" placeholder="Password Confirmation"></ion-input>
28
-        </ion-item>
29
-      </ion-list>
30
-      <div>
31
-      <ion-button @click="signup" class=' ion-margin-top'>Signup</ion-button>
32
-      </div>
33
-      <br>
34
-       <ion-input
35
-    ref="input"
36
-    type="email"
37
-    fill="solid"
38
-    label="Email"
39
-    label-placement="floating"
40
-    helper-text="Enter a valid email"
41
-    error-text="Invalid email"
42
-    @ionInput="validate"
43
-    @ionBlur="markTouched"
44
-  ></ion-input>
20
+          <ion-text color="danger" v-if="formError">{{ formError }}</ion-text>
21
+          <ion-button @click="signup" class=' ion-margin-top'>Signup</ion-button>
45 22
     </ion-content>
46 23
   </ion-page>
47 24
 </template>
@@ -49,7 +26,7 @@
49 26
 <script setup lang="ts">
50 27
 import { IonPage, IonHeader, IonToolbar, IonTitle, IonContent, IonButton, onIonViewWillEnter, IonList, IonSearchbar, IonLabel,
51 28
 IonItem, IonBackButton, IonProgressBar, IonButtons, IonInfiniteScroll,
52
-    IonInfiniteScrollContent, InfiniteScrollCustomEvent, IonInput } from '@ionic/vue';
29
+    IonInfiniteScrollContent, InfiniteScrollCustomEvent, IonInput, toastController, useIonRouter } from '@ionic/vue';
53 30
 import { defineComponent, onMounted } from 'vue';
54 31
 import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
55 32
 import { searchMat, callUrl,restSignup } from '@/composable/settings';
@@ -67,24 +44,54 @@ const password = ref(null)
67 44
 const password2 = ref(null)
68 45
 const email = ref(null)
69 46
 const input = ref(null)
47
+const ionRouter = useIonRouter();
48
+const formError = ref(null)
70 49
 
71 50
 const signup = async () => { 
72
-  username.value.$el.errorText = "No way"
73
-  username.value.$el.classList.remove('ion-valid')
74
-  username.value.$el.classList.remove('ion-invalid')
75
-  username.value.$el.classList.add('ion-invalid')
76
-  username.value.$el.classList.add('ion-touched');
51
+  clearError(username)
52
+  clearError(email)
53
+  clearError(password)
54
+  clearError(password2)
55
+  formError.value = null
77 56
   try {
78 57
     console.log("username = ", username.value.$el.value)
79 58
     const data = await restSignup(username.value.$el.value, email.value.$el.value, password.value.$el.value, password2.value.$el.value)
80 59
     console.log(data)
60
+    await presentToast("Sign up successful", "top")
61
+    ionRouter.navigate('/tabs/tab4', 'forward', 'replace');
81 62
   }catch( err ) {
82 63
     console.log("-- error --")
83 64
     console.log(err)
84
-    username.value.$el.classList.add('ion-invalid')
65
+    for(const [k,v] of Object.entries(err.response.data))  {
66
+      if( k == "username" ){
67
+        showError(username, v[0])
68
+      }
69
+      if( k == "email" ){
70
+        showError(email, v[0])
71
+      }
72
+      if( k == "password1" ){
73
+        showError(password, v[0])
74
+      }
75
+      if( k == "password2" ){
76
+        showError(password2, v[0])
77
+      }
78
+      if( k == "non_field_errors" ) {
79
+        formError.value = v[0]
80
+      }
81
+    }
85 82
   }
86 83
 }
87 84
 
85
+const showError = (obj, error) => {
86
+  obj.value.$el.errorText = error
87
+  obj.value.$el.classList.add('ion-invalid')
88
+  obj.value.$el.classList.add('ion-touched');
89
+}
90
+const clearError = (obj) => {
91
+  obj.value.$el.classList.remove("ion-valid")
92
+  obj.value.$el.classList.remove("ion-invalid")
93
+}
94
+
88 95
 const validateEmail = (email) => {
89 96
         return email.match(
90 97
           /^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
@@ -110,4 +117,16 @@ const markTouched = () => {
110 117
 const markTouched2 = () => {
111 118
   username.value.$el.classList.add('ion-touched');
112 119
 }
120
+const presentToast = async (msg, position: 'top' | 'middle' | 'bottom') => {
121
+  console.log("present toast")
122
+  const toast = await toastController.create({
123
+    message: msg,
124
+    duration: 3000,
125
+    position: position,
126
+    positionAnchor: "header",
127
+    color: "success"
128
+  });
129
+
130
+  await toast.present();
131
+}
113 132
 </script>