From 75fc1f125a32082becf0c3a20781f9afc0bfd5cf Mon Sep 17 00:00:00 2001
From: Thomas Citharel <tcit@tcit.fr>
Date: Fri, 9 Oct 2020 16:07:13 +0200
Subject: [PATCH] Show a proper error message on creating/updating an event

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
---
 js/src/views/Event/Edit.vue           | 13 ++++++++++++-
 lib/graphql/resolvers/event.ex        |  2 +-
 test/graphql/resolvers/event_test.exs |  2 +-
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/js/src/views/Event/Edit.vue b/js/src/views/Event/Edit.vue
index 816a9171f..2ad2d6f80 100644
--- a/js/src/views/Event/Edit.vue
+++ b/js/src/views/Event/Edit.vue
@@ -588,6 +588,7 @@ export default class EditEvent extends Vue {
     } catch (err) {
       this.saving = false;
       console.error(err);
+      this.handleError(err);
     }
   }
 
@@ -615,7 +616,7 @@ export default class EditEvent extends Vue {
       });
     } catch (err) {
       this.saving = false;
-      console.error(err);
+      this.handleError(err);
     }
   }
 
@@ -627,6 +628,16 @@ export default class EditEvent extends Vue {
       : this.$i18n.t("The event has been updated")) as string;
   }
 
+  private handleError(err: any) {
+    console.error(err);
+
+    if (err.graphQLErrors !== undefined) {
+      err.graphQLErrors.forEach(({ message }: { message: string }) => {
+        this.$notifier.error(message);
+      });
+    }
+  }
+
   /**
    * Put in cache the updated or created event.
    * If the event is not a draft anymore, also put in cache the participation
diff --git a/lib/graphql/resolvers/event.ex b/lib/graphql/resolvers/event.ex
index 36d978c9a..16e5c3479 100644
--- a/lib/graphql/resolvers/event.ex
+++ b/lib/graphql/resolvers/event.ex
@@ -219,7 +219,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
   def update_event(
         _parent,
         %{event_id: event_id} = args,
-        %{context: %{current_user: user}} = _resolution
+        %{context: %{current_user: %User{} = user}} = _resolution
       ) do
     # See https://github.com/absinthe-graphql/absinthe/issues/490
     with args <- Map.put(args, :options, args[:options] || %{}),
diff --git a/test/graphql/resolvers/event_test.exs b/test/graphql/resolvers/event_test.exs
index d7299de15..6f75e70d5 100644
--- a/test/graphql/resolvers/event_test.exs
+++ b/test/graphql/resolvers/event_test.exs
@@ -666,7 +666,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
       assert hd(json_response(res, 200)["errors"])["message"] == "Event not found"
     end
 
-    test "update_event/3 should check the user is an administrator", %{
+    test "update_event/3 should check the user is the organizer", %{
       conn: conn,
       actor: _actor,
       user: user