Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<NuxtLoadingIndicator> and useLarafetch / $larafetch #36

Open
mulky-sulaiman opened this issue Jul 15, 2024 · 2 comments
Open

<NuxtLoadingIndicator> and useLarafetch / $larafetch #36

mulky-sulaiman opened this issue Jul 15, 2024 · 2 comments

Comments

@mulky-sulaiman
Copy link

Hello @amrnn90 ,

I'm trying to make the NuxtLoadingIndicator to be work with useLarafecth / $larafetch, so I can get the laoding progress shown for each fetch request to the Breeze API.

Due to the nature of NuxtLoadingIndicator itself, seems like the solution for now is to add hook call for fetch options to the event of
onRequest and onResponse, as being mentioned in this example
nuxt/nuxt#14221 (comment)

Been tried to add such options in useLaraftch / $larafetch to no avail, So if you can direct me to where should I add those options to useLarafecth / $larafetch, that would be helpful.

Pardon for any unclear explanation or misconception, since I'm very noob for this frontend Vue/Nuxt things 🙏

@ironytr
Copy link

ironytr commented Oct 2, 2024

Hello @amrnn90 ,

I'm trying to make the NuxtLoadingIndicator to be work with useLarafecth / $larafetch, so I can get the laoding progress shown for each fetch request to the Breeze API.

Due to the nature of NuxtLoadingIndicator itself, seems like the solution for now is to add hook call for fetch options to the event of onRequest and onResponse, as being mentioned in this example nuxt/nuxt#14221 (comment)

Been tried to add such options in useLaraftch / $larafetch to no avail, So if you can direct me to where should I add those options to useLarafecth / $larafetch, that would be helpful.

Pardon for any unclear explanation or misconception, since I'm very noob for this frontend Vue/Nuxt things 🙏

yo bro my self noob too but this somehow works:

// plugins/loading.client.ts
export default defineNuxtPlugin(async (nuxtApp) => {
  // const router = useRouter()
  const { progress, isLoading, start, finish, clear } = useLoadingIndicator({
    duration: 2000,
    throttle: 0,
    // This is how progress is calculated by default
    estimatedProgress: (duration, elapsed) => (2 / Math.PI * 100) * Math.atan(elapsed / duration * 100 / 50)
  })


  nuxtApp.hook('page:start', () => {
    console.log('im a start')
    start()
    })
  

  nuxtApp.hook('page:finish', () => {
    console.log('im a finish')
    finish()
  })

  // router.beforeEach(() => {
    
  // })

  // router.afterEach(() => {
    
  // })

})
utils/$larafetch.ts

export const $larafetch = $fetch.create({
  credentials: "include",
  async onRequest({ request, options }) {
    const { backendUrl, frontendUrl } = useRuntimeConfig().public;
    const event = typeof useEvent === "function" ? useEvent() : null;

    const nuxtApp = useNuxtApp();

    if (import.meta.client) {
      nuxtApp.callHook("page:start");

      console.log(useCookie("XSRF-TOKEN").value, "useCookie(CSRF_COOKIE).value")
    }
........



//create onResponse func
  async onResponse({ response }) {

    const nuxtApp = useNuxtApp();

    if (import.meta.client) {
      nuxtApp.callHook("page:finish");

    }


    const { showSuccessMessage, showInfoMessage, showWarnMessage, showErrorMessage } = useMessages()

    const status = response.status;
    if ([201].includes(status)) {
      showSuccessMessage("Başarılı");

    }
    return;

  },

@mulky-sulaiman
Copy link
Author

thank's man, i'll give it a try soon !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants