import UPDATE_PROJECT_SHARED_STATE_MUTATION from '@/graphql/gql/mutations/updateProjectSharedState.gql';
import PROJECT_QUERY from '@/graphql/gql/queries/projectQuery.gql';

export default {
  methods: {
    updateProjectShareState(id, shared) {
      this.$apollo.mutate({
        mutation: UPDATE_PROJECT_SHARED_STATE_MUTATION,
        variables: {
          input: {
            id: id,
            shared,
          },
        },
        update(store, {data: {updateProjectSharedState: {shared, errors}}}) {
          if (!errors) {
            const query = PROJECT_QUERY;
            const variables = {
              id: id,
            };
            const data = store.readQuery({query, variables});

            if (data) {
              data.project.final = shared;
              store.writeQuery({query, variables, data});
            }
          }
        },
      });
    }
  }
};
