All files / app/assets/javascripts/vue_shared/components user_callout_dismisser.vue

96.15% Statements 25/26
93.1% Branches 27/29
100% Functions 16/16
96.15% Lines 25/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185        50x                                                                                                                                                       30x                       7x     27x     20x     30x           1x     57x     57x     36x   36x     40x                       65x   65x                       61x         4x 4x   4x 4x                 4x 4x 2x         4x       2x       65x        
<script>
import dismissUserCalloutMutation from '~/graphql_shared/mutations/dismiss_user_callout.mutation.graphql';
import getUserCalloutsQuery from '~/graphql_shared/queries/get_user_callouts.query.graphql';
 
/**
 * A renderless component for querying/dismissing UserCallouts via GraphQL.
 *
 * Simplest example usage:
 *
 *     <user-callout-dismisser feature-name="my_user_callout">
 *       <template #default="{ dismiss, shouldShowCallout }">
 *         <my-callout-component
 *           v-if="shouldShowCallout"
 *           @close="dismiss"
 *         />
 *       </template>
 *     </user-callout-dismisser>
 *
 * If you don't want the asynchronous query to run when the component is
 * created, and know by some other means whether the user callout has already
 * been dismissed, you can use the `skipQuery` prop, and a regular `v-if`
 * directive:
 *
 *     <user-callout-dismisser
 *       v-if="userCalloutIsNotDismissed"
 *       feature-name="my_user_callout"
 *       skip-query
 *     >
 *       <template #default="{ dismiss, shouldShowCallout }">
 *         <my-callout-component
 *           v-if="shouldShowCallout"
 *           @close="dismiss"
 *         />
 *       </template>
 *     </user-callout-dismisser>
 *
 * The component exposes various scoped slot props on the default slot,
 * allowing for granular rendering behaviors based on the state of the initial
 * query and user-initiated mutation:
 *
 *  - dismiss: Function
 *    - Triggers mutation to dismiss the user callout.
 *  - isAnonUser: boolean
 *    - Whether the current user is anonymous or not (i.e., whether or not
 *      they're logged in).
 *  - isDismissed: boolean
 *    - Whether the given user callout has been dismissed or not.
 *  - isLoadingMutation: boolean
 *    - Whether the mutation is loading.
 *  - isLoadingQuery: boolean
 *    - Whether the initial query is loading.
 *  - mutationError: string[] | null
 *    - The mutation's errors, if any; otherwise `null`.
 *  - queryError: Error | null
 *    - The query's error, if any; otherwise `null`.
 *  - shouldShowCallout: boolean
 *    - A combination of the above which should cover 95% of use cases: `true`
 *      if the query has loaded without error, and the user is logged in, and
 *      the callout has not been dismissed yet; `false` otherwise
 *
 * The component emits a `queryResult` event when the GraphQL query
 * completes. The payload is a combination of the ApolloQueryResult object and
 * this component's `slotProps` computed property. This is useful for things
 * like cleaning up/unmounting the component if the callout shouldn't be
 * displayed.
 */
export default {
  name: 'UserCalloutDismisser',
  props: {
    featureName: {
      type: String,
      required: true,
    },
    skipQuery: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      currentUser: null,
      isDismissedLocal: false,
      isLoadingMutation: false,
      mutationError: null,
      queryError: null,
    };
  },
  apollo: {
    currentUser: {
      query: getUserCalloutsQuery,
      update(data) {
        return data?.currentUser;
      },
      result(data) {
        this.$emit('queryResult', { ...data, ...this.slotProps });
      },
      error(err) {
        this.queryError = err;
      },
      skip() {
        return this.skipQuery;
      },
    },
  },
  computed: {
    featureNameEnumValue() {
      return this.featureName.toUpperCase();
    },
    isLoadingQuery() {
      return this.$apollo.queries.currentUser.loading;
    },
    isAnonUser() {
      return !(this.skipQuery || this.queryError || this.isLoadingQuery || this.currentUser);
    },
    isDismissedRemote() {
      const callouts = this.currentUser?.callouts?.nodes ?? [];
 
      return callouts.some(({ featureName }) => featureName === this.featureNameEnumValue);
    },
    isDismissed() {
      return this.isDismissedLocal || this.isDismissedRemote;
    },
    slotProps() {
      const {
        dismiss,
        isAnonUser,
        isDismissed,
        isLoadingMutation,
        isLoadingQuery,
        mutationError,
        queryError,
        shouldShowCallout,
      } = this;
 
      return {
        dismiss,
        isAnonUser,
        isDismissed,
        isLoadingMutation,
        isLoadingQuery,
        mutationError,
        queryError,
        shouldShowCallout,
      };
    },
    shouldShowCallout() {
      return !(this.isLoadingQuery || this.isDismissed || this.queryError || this.isAnonUser);
    },
  },
  methods: {
    async dismiss() {
      this.isLoadingMutation = true;
      this.isDismissedLocal = true;
 
      try {
        const { data } = await this.$apollo.mutate({
          mutation: dismissUserCalloutMutation,
          variables: {
            input: {
              featureName: this.featureName,
            },
          },
        });
 
        const errors = data?.userCalloutCreate?.errors ?? [];
        if (errors.length > 0) {
          this.onDismissalError(errors);
        }
      } catch (err) {
        this.onDismissalError([err.message]);
      } finally {
        this.isLoadingMutation = false;
      }
    },
    onDismissalError(errors) {
      this.mutationError = errors;
    },
  },
  render() {
    return this.$scopedSlots.default(this.slotProps);
  },
};
</script>