index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <!--
  2. Administration de la connexion Opentalent / HelloAsso
  3. -->
  4. <template>
  5. <LayoutContainer>
  6. <v-card>
  7. <v-row>
  8. <v-col cols="12" md="4" class="d-flex justify-center">
  9. <v-img src="/images/logos/Logo-HelloAsso.svg" class="logo" />
  10. </v-col>
  11. <v-col cols="12" md="8" class="presentation">
  12. {{ $t('helloasso_presentation') }}
  13. </v-col>
  14. </v-row>
  15. <v-row>
  16. <v-col cols="12" class="d-flex justify-center align-center w-100 mt-6">
  17. <v-progress-circular
  18. v-if="
  19. statusHelloAssoProfile === FETCHING_STATUS.PENDING ||
  20. unlinkingPending
  21. "
  22. indeterminate
  23. size="32"
  24. />
  25. <UiButtonHelloAssoConnect
  26. v-else-if="!helloAssoProfile || !helloAssoProfile.token"
  27. @click="onHelloAssoConnectClicked"
  28. />
  29. <div v-else class="d-flex flex-column align-center">
  30. <v-row>
  31. <v-icon icon="fas fa-check" color="success" class="mr-3" />
  32. {{ $t('your_helloasso_account_is_linked') }}
  33. </v-row>
  34. <v-row>
  35. <v-btn class="theme-warning mt-4" @click="onUnlinkAccountClick">
  36. {{ $t('unlink_your_helloasso_account') }}
  37. </v-btn>
  38. </v-row>
  39. </div>
  40. </v-col>
  41. </v-row>
  42. </v-card>
  43. </LayoutContainer>
  44. </template>
  45. <script setup lang="ts">
  46. import AuthUrl from '~/models/HelloAsso/AuthUrl'
  47. import HelloAssoProfile from '~/models/HelloAsso/HelloAssoProfile'
  48. import { useEntityManager } from '~/composables/data/useEntityManager'
  49. import { useEntityFetch } from '~/composables/data/useEntityFetch'
  50. import { FETCHING_STATUS } from '~/types/enum/data'
  51. import UnlinkRequest from '~/models/HelloAsso/UnlinkRequest'
  52. const { em } = useEntityManager()
  53. const organizationProfile = useOrganizationProfileStore()
  54. const onHelloAssoConnectClicked = async () => {
  55. // Important de régénérer une URL avec un nouveau challenge à chaque
  56. // essai (entre autres pour supporter le HMR pendant les tests en local,
  57. // ou en cas d'erreur et de ré-essai)
  58. const authUrl = await em.fetch(AuthUrl)
  59. navigateTo(authUrl.authUrl, {
  60. external: true,
  61. open: {
  62. target: '_blank',
  63. windowFeatures: {
  64. popup: true,
  65. width: 900,
  66. height: 600,
  67. },
  68. },
  69. })
  70. }
  71. onMounted(() => {
  72. window.addEventListener('message', (event) => {
  73. if (event.origin !== window.location.origin) {
  74. return
  75. }
  76. if (!event.data || !event.data.code) {
  77. return
  78. }
  79. onHelloAssoConnected()
  80. })
  81. })
  82. const { fetch } = useEntityFetch()
  83. const { status: statusHelloAssoProfile, refresh: refreshHelloAssoProfile } =
  84. await fetch(HelloAssoProfile)
  85. const helloAssoProfile: ComputedRef<HelloAssoProfile | null> = computed(() => {
  86. if (statusHelloAssoProfile.value !== FETCHING_STATUS.SUCCESS) {
  87. return null
  88. }
  89. return em.find(HelloAssoProfile, 1)
  90. })
  91. const onHelloAssoConnected = async () => {
  92. // On attend 200ms pour laisser en attente du message SSE
  93. await new Promise((r) => setTimeout(r, 200))
  94. if (!helloAssoProfile.value || !helloAssoProfile.value.token) {
  95. // Fallback en cas de défaut de fonctionnement du SSE
  96. console.log('Helloasso connected (fallback SSE)')
  97. await refreshHelloAssoProfile()
  98. }
  99. }
  100. const unlinkingPending: Ref<boolean> = ref(false)
  101. const onUnlinkAccountClick = async () => {
  102. const unlinkRequest = em.newInstance(UnlinkRequest, {
  103. organizationId: organizationProfile.id,
  104. })
  105. unlinkingPending.value = true
  106. try {
  107. await em.persist(unlinkRequest)
  108. await refreshHelloAssoProfile()
  109. } finally {
  110. unlinkingPending.value = false
  111. }
  112. }
  113. </script>
  114. <style scoped lang="scss">
  115. .v-card {
  116. padding: 48px;
  117. max-width: 70%;
  118. margin: 36px auto;
  119. @media (max-width: 600px) {
  120. max-width: 90%;
  121. }
  122. }
  123. .logo {
  124. max-width: 80%;
  125. }
  126. .presentation {
  127. border-left: 3px solid rgb(var(--v-theme-info));
  128. padding: 0 24px;
  129. color: rgb(var(--v-theme-on-neutral));
  130. }
  131. .authDialog {
  132. max-width: 90%;
  133. }
  134. </style>