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

feat: useCall, useList, useDoc #224

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

feat: useCall, useList, useDoc #224

wants to merge 10 commits into from

Conversation

netchampfaris
Copy link
Contributor

@netchampfaris netchampfaris commented Nov 20, 2024

  • New data fetching composables based on @vueuse's useFetch
  • Based on /api/v2

useCall

import { reactive, ref } from 'vue'
import { useCall } from 'frappe-ui'

interface LoginResponse {
  success: boolean
  message: string
}

interface LoginParams {
  usr: string
  pwd: string
}

let loginParams = reactive<LoginParams>({ usr: '', pwd: '' })

let login = useCall<LoginResponse, LoginParams>({
  url: '/api/v2/method/login',
  method: 'POST',
  immediate: false, // don't fetch immediately
  refetch: true, // auto refetch when params change
  params: loginParams,
  transform(data) {
    return { success: data.message === 'Logged In', message: data.message }
  },
  onSuccess: (data) => {
    console.log(data)
  },
  onError: (error) => {
    console.error(error)
  },
})

await login.submit({ usr: 'admin', pwd: 'admin' })

login.data // { success: true, message: 'Logged In' }
login.error
login.loading
login.abort()

useList

import { reactive, ref } from 'vue'
import { useList } from 'frappe-ui'

interface User {
  name: string
  email: string
  full_name: string
}

let query = ref('adm')

let users = useList<User>({
  doctype: 'User',
  fields: ['name', 'email', 'full_name'],
  filters: {
    role: 'System Manager',
    name: ['like', query],
  },
  start: 0,
  limit: 10,
  orderBy: 'name asc',
  refetch: true, // auto refetch when params change
})

users.data // [{ name: '..', email: '..', full_name: '..' }, ..]
users.loading
users.hasNextPage
users.hasPreviousPage
users.next()
users.previous()
users.fetch()
users.abort()

useDoc

import { useDoc } from 'frappe-ui'

interface User {
  name: string,
  email: string,
  first_name: string,
  last_name: string
}

interface UserMethods {
  getFullName: () => string
  updateEmail: (params: { email: string }) => void
}

let user = useDoc<User, UserMethods>({
  doctype: 'User',
  name: 'user1',
  methods: {
    getFullName: 'get_full_name',
    updateEmail: {
      name: 'update_email',
      onSuccess() {
        console.log('email updated')
      }
    }
  }
})

user.getFullName.submit() // typescript is happy
user.getFullName.submit({}) // typescript errors
user.getFullName.data // string | null (inferred from method definition in UserMethods)

user.updateEmail.submit({ email: '[email protected]' }) // typescript is happy
user.updateEmail.submit() // typescript errors
user.updateEmail.data // void | null (inferred from method definition in UserMethods)

@netchampfaris netchampfaris changed the title useDocument, useList, useDoctype feat: useDocument, useList, useDoctype Nov 20, 2024
@netchampfaris netchampfaris marked this pull request as draft November 20, 2024 08:30
@netchampfaris netchampfaris changed the title feat: useDocument, useList, useDoctype feat: useCall, useList, useDoc Dec 16, 2024
@netchampfaris netchampfaris marked this pull request as ready for review December 18, 2024 09:08
@frappe frappe deleted a comment from netlify bot Dec 18, 2024
@netchampfaris netchampfaris force-pushed the use-doctype branch 2 times, most recently from e6fbd43 to fe2512e Compare December 18, 2024 09:21
useCall, useList and useDoc
based on useFetch from vueuse
- setup vitest for unit testing
- setup msw for mocking fetch calls
- add support for baseUrl in useCall
- handle case of no params passed in call.submit
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

Successfully merging this pull request may close these issues.

1 participant