Skip to content

Data doesn't get updated while refreshing nor WriteQuery #272

Closed
@ScreamZ

Description

@ScreamZ

Hello,

First of all, I'm not sure I'm doing it correctly, but here is my statement.

This is a « pretty simple » use case where I want to register an user and get the state updated with his data.

<template>
  <div class="UserMenu">
    <div class="ButtonWrapper">
      <Button :color="primaryColor">{{ $t('header.becomeCoach') }}</Button>
    </div>
    <div class="ButtonWrapper" v-if="!user">
      <Button :color="primaryColor" @click="displayLogin">{{ $t('header.connect') }}</Button>
      <div style="display: flex; justifyContent: center">
        <LoginModal @onRegister="register" :register-error="registerErrorMsg" />
      </div>
    </div>
    <div v-else>
      <Button class="Connected-Button" @click="showMenu = !showMenu">
        <img src="https://placeimg.com/40/40/people" />
        <span>&nbsp;{{ user.firstname }}&nbsp;</span>
        <icon :name="showMenu ? 'chevron-up' : 'chevron-down'" />
      </Button>
      <div v-if="showMenu" class="Connected-Menu">
        <div>Email: {{ user.email }}</div>
        <a href="">Mon profil</a>
        <a href="" @click="logout">Se deconnecter</a>
      </div>
    </div>

  </div>
</template>

<script lang="ts">
import gql from "graphql-tag";
import Vue from "vue";
import LoginModal from "../../components/Home/LoginModal.vue";
import Button from "../Common/Button.vue";

import { colors } from "../../services/variables";

export default Vue.extend({
  apollo: {
    user: {
      fetchPolicy: "cache-and-network",
      query: gql`
        {
          user: me {
            _id
            email
            firstname
          }
        }`,
    },
  },
  components: { Button, LoginModal },
  data() {
    return {
      primaryColor: colors.primary,
      registerErrorMsg: null,
      showMenu: false,
      user: null,
    };
  },
  methods: {
    displayLogin() {
      this.$modal.show("login-modal");
    },
    logout(e: Event) {
      e.preventDefault();
      const request = new XMLHttpRequest();
      request.open("POST", "http://localhost:3001/logout");
      request.withCredentials = true;
      request.send();

      // TODO: CALL client.resetStore()
      this.user = null;
    },
    register(values) {
      const request = new XMLHttpRequest();
      request.open("POST", "http://localhost:3001/register");
      request.setRequestHeader("Content-Type", "application/json");
      request.withCredentials = true;

      request.addEventListener("load", (event) => {
        const { status, response } = request;
        if (status === 401) {
          this.registerErrorMsg = this.$t("home.accountModal.validation.emailAlreadyTaken");
          return;
        }
        this.registerErrorMsg = null;
        this.$modal.hide("login-modal");
        this.$apollo.queries.user.refetch();

        // TODO: this could be counter solution
        // this.user = JSON.parse(response).user;
      });

      request.send(JSON.stringify(values));
    },
  },
});
</script>

As you can see I'm using this.$apollo.queries.user.refetch(); in order to refetch the user metadata after setting the jwt as a cookie while returning the response from the XHR.

The request is correctly outgoing and return the correct result from Network tab, but randomly, I don't get my user data updated. If I refresh the tab, it update and work properly.

I also tried to use $forceUpdate() nothing changed. Also setTimeout to avoid race condition…

Am I doing wrong ? Am I thinking in a bad way ?

Thanks for help,
Best regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions