You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

  1. import { reactive } from '@nuxtjs/composition-api'
  2. import { TextLabel } from '~/domain/models/tasks/textLabel'
  3. export const useTextLabel = (repository: any, projectId: string) => {
  4. const state = reactive({
  5. labels: [] as TextLabel[],
  6. error: ''
  7. })
  8. const validateText = (text: string) => {
  9. if (state.labels.some((label) => label.text === text)) {
  10. state.error = 'The label already exists.'
  11. return false
  12. }
  13. return true
  14. }
  15. const add = async (exampleId: number, text: string) => {
  16. const textLabel = TextLabel.create(text)
  17. if (!validateText(textLabel.text)) {
  18. return
  19. }
  20. try {
  21. await repository.create(projectId, exampleId, textLabel)
  22. await list(exampleId)
  23. } catch (e: any) {
  24. state.error = e.response.data.detail
  25. }
  26. }
  27. const list = async (exampleId: number) => {
  28. state.labels = await repository.list(projectId, exampleId)
  29. }
  30. const update = async (exampleId: number, labelId: number, text: string) => {
  31. if (!validateText(text)) {
  32. return
  33. }
  34. const label = state.labels.find((label) => label.id === labelId)!
  35. label.updateText(text)
  36. try {
  37. await repository.update(projectId, exampleId, labelId, label)
  38. await list(exampleId)
  39. } catch (e: any) {
  40. state.error = e.response.data.detail
  41. }
  42. }
  43. const remove = async (exampleId: number, labelId: number) => {
  44. await repository.delete(projectId, exampleId, labelId)
  45. state.labels = state.labels.filter((label) => label.id !== labelId)
  46. }
  47. const clear = async (exampleId: number) => {
  48. await repository.clear(projectId, exampleId)
  49. state.labels = []
  50. }
  51. const autoLabel = async (exampleId: number) => {
  52. try {
  53. await repository.autoLabel(projectId, exampleId)
  54. await list(exampleId)
  55. } catch (e: any) {
  56. state.error = e.response.data.detail
  57. }
  58. }
  59. return {
  60. state,
  61. add,
  62. list,
  63. update,
  64. remove,
  65. clear,
  66. autoLabel
  67. }
  68. }