From 4a77b2b1b712c594064df71415c3fb1ad6a31df5 Mon Sep 17 00:00:00 2001
From: Chocobozzz <me@florianbigard.com>
Date: Fri, 11 Jan 2019 14:07:14 +0100
Subject: [PATCH] Fix resend confirmation component

---
 js/src/components/Account/Login.vue           |  4 +--
 .../components/Account/ResendConfirmation.vue | 35 +++++++++++--------
 .../components/Account/SendPasswordReset.vue  |  4 +--
 js/src/graphql/auth.ts                        |  6 ++++
 js/src/utils/validators.ts                    |  2 +-
 test/support/factory.ex                       |  5 ++-
 6 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/js/src/components/Account/Login.vue b/js/src/components/Account/Login.vue
index f457fda6f..731d28668 100644
--- a/js/src/components/Account/Login.vue
+++ b/js/src/components/Account/Login.vue
@@ -64,7 +64,7 @@
   import RegisterAvatar from './RegisterAvatar.vue';
   import { Component, Prop, Vue } from 'vue-property-decorator';
   import { LOGIN } from '@/graphql/auth';
-  import { validateEmail, validateRequiredField } from '@/utils/validators';
+  import { validateEmailField, validateRequiredField } from '@/utils/validators';
   import { saveUserData } from '@/utils/auth';
   import { ILogin } from '@/types/login.model'
 
@@ -94,7 +94,7 @@
     };
     rules = {
       required: validateRequiredField,
-      email: validateEmail
+      email: validateEmailField
     };
     user: any;
 
diff --git a/js/src/components/Account/ResendConfirmation.vue b/js/src/components/Account/ResendConfirmation.vue
index 119f90182..886d28096 100644
--- a/js/src/components/Account/ResendConfirmation.vue
+++ b/js/src/components/Account/ResendConfirmation.vue
@@ -32,6 +32,8 @@
 
 <script lang="ts">
   import { Component, Prop, Vue } from 'vue-property-decorator';
+  import { validateEmailField, validateRequiredField } from '@/utils/validators';
+  import { RESEND_CONFIRMATION_EMAIL } from '@/graphql/auth';
 
   @Component
   export default class ResendConfirmation extends Vue {
@@ -49,29 +51,32 @@
       },
     };
     rules = {
-      required: value => !!value || 'Required.',
-      email: (value) => {
-        const pattern = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
-        return pattern.test(value) || 'Invalid e-mail.';
-      },
+      required: validateRequiredField,
+      email: validateEmailField,
     };
 
     mounted() {
       this.credentials.email = this.email;
     }
 
-    resendConfirmationAction(e) {
+    async resendConfirmationAction(e) {
       e.preventDefault();
+      this.error = false;
 
-      // FIXME: implement fetchStory
-      // fetchStory('/users/resend', this.$store, { method: 'POST', body: JSON.stringify(this.credentials) }).then(() => {
-      //   this.validationSent = true;
-      // }).catch((err) => {
-      //   Promise.resolve(err).then(() => {
-      //     this.error = true;
-      //     this.validationSent = true;
-      //   });
-      // });
+      try {
+        await this.$apollo.mutate({
+          mutation: RESEND_CONFIRMATION_EMAIL,
+          variables: {
+            email: this.credentials.email,
+          },
+        });
+
+      } catch (err) {
+        console.error(err);
+        this.error = true;
+      } finally {
+        this.validationSent = true;
+      }
     }
   };
 </script>
diff --git a/js/src/components/Account/SendPasswordReset.vue b/js/src/components/Account/SendPasswordReset.vue
index 5ae40b6f8..b71fef5e4 100644
--- a/js/src/components/Account/SendPasswordReset.vue
+++ b/js/src/components/Account/SendPasswordReset.vue
@@ -32,7 +32,7 @@
 
 <script lang="ts">
   import { Component, Prop, Vue } from 'vue-property-decorator';
-  import { validateEmail, validateRequiredField } from '@/utils/validators';
+  import { validateEmailField, validateRequiredField } from '@/utils/validators';
   import { SEND_RESET_PASSWORD } from '@/graphql/auth';
 
   @Component
@@ -53,7 +53,7 @@
 
     rules = {
       required: validateRequiredField,
-      email: validateEmail,
+      email: validateEmailField,
     };
 
     mounted() {
diff --git a/js/src/graphql/auth.ts b/js/src/graphql/auth.ts
index 1609d88df..8b757ee59 100644
--- a/js/src/graphql/auth.ts
+++ b/js/src/graphql/auth.ts
@@ -27,3 +27,9 @@ mutation ResetPassword($token: String!, $password: String!) {
   },
 }
 `;
+
+export const RESEND_CONFIRMATION_EMAIL = gql`
+mutation ResendConfirmationEmail($email: String!) {
+  resendConfirmationEmail(email: $email)
+}
+`;
diff --git a/js/src/utils/validators.ts b/js/src/utils/validators.ts
index 4d1af463f..fbd2a25b6 100644
--- a/js/src/utils/validators.ts
+++ b/js/src/utils/validators.ts
@@ -1,4 +1,4 @@
-export function validateEmail(value: string) {
+export function validateEmailField(value: string) {
   return value.includes('@') || 'Invalid e-mail.';
 }
 
diff --git a/test/support/factory.ex b/test/support/factory.ex
index 2ac1f4153..321b43d7f 100644
--- a/test/support/factory.ex
+++ b/test/support/factory.ex
@@ -9,7 +9,10 @@ defmodule Mobilizon.Factory do
     %Mobilizon.Actors.User{
       password_hash: "Jane Smith",
       email: sequence(:email, &"email-#{&1}@example.com"),
-      role: 0
+      role: 0,
+      confirmed_at: DateTime.utc_now(),
+      confirmation_sent_at: nil,
+      confirmation_token: nil
     }
   end