@campxdev/shared 1.10.41 → 1.10.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@campxdev/shared",
3
- "version": "1.10.41",
3
+ "version": "1.10.42",
4
4
  "main": "./exports.ts",
5
5
  "scripts": {
6
6
  "start": "react-scripts start",
@@ -1,12 +1,14 @@
1
1
  import { yupResolver } from '@hookform/resolvers/yup'
2
2
  import { Stack } from '@mui/material'
3
+ import { useEffect } from 'react'
3
4
  import { useForm } from 'react-hook-form'
4
- import { useMutation, useQueryClient } from 'react-query'
5
+ import { useMutation, useQuery, useQueryClient } from 'react-query'
5
6
  import { toast } from 'react-toastify'
6
7
  import { useImmer } from 'use-immer'
7
- import { axiosErrorToast } from '../../config/axios'
8
+ import axios, { axiosErrorToast } from '../../config/axios'
8
9
  import ActionButton from '../ActionButton'
9
- import { FormMultiSelect } from '../HookForm'
10
+ import { FormMultiSelect, FormSingleSelect } from '../HookForm'
11
+ import Spinner from '../Spinner'
10
12
  import {
11
13
  editUserProfileSchema,
12
14
  fetchUsers,
@@ -20,20 +22,27 @@ interface UserProps {
20
22
  }[]
21
23
  inputValue: string
22
24
  }
25
+ const getDepartments = () => {
26
+ return axios.get('/hrms/departments').then((res) => res?.data?.data)
27
+ }
23
28
  function UserProfileRelation({ close, application, profiles, data }) {
24
29
  const [state, setState] = useImmer<UserProps>({
25
30
  options: [],
26
31
  inputValue: '',
27
32
  })
28
33
  const queryClient = useQueryClient()
34
+ const { data: departments, isLoading } = useQuery('get-department', () =>
35
+ getDepartments(),
36
+ )
29
37
 
30
- const { control, handleSubmit } = useForm({
38
+ const { control, handleSubmit, watch } = useForm({
31
39
  defaultValues: {
32
40
  profileIds:
33
41
  data?.profiles?.map((profile) => ({
34
42
  label: profile.name,
35
43
  value: profile.id,
36
44
  })) ?? [],
45
+ departmentId: null,
37
46
  },
38
47
  resolver: yupResolver(data ? editUserProfileSchema : userProfileSchema),
39
48
  })
@@ -54,6 +63,15 @@ function UserProfileRelation({ close, application, profiles, data }) {
54
63
  },
55
64
  )
56
65
 
66
+ useEffect(() => {
67
+ if (watch('departmentId')) {
68
+ fetchUsersFn({
69
+ application: application,
70
+ departmentId: watch('departmentId'),
71
+ })
72
+ }
73
+ }, [watch('departmentId')])
74
+
57
75
  const { mutate: fetchUsersFn, isLoading: gettingUsers } = useMutation(
58
76
  fetchUsers,
59
77
  {
@@ -69,10 +87,10 @@ function UserProfileRelation({ close, application, profiles, data }) {
69
87
  )
70
88
  const onSubmit = (formData) => {
71
89
  mutate({
72
- id: data?.id,
90
+ userIds: formData?.userId?.map((userId) => +userId.value) ?? [],
73
91
  application: application,
74
92
  profileIds: formData.profileIds?.map((profile) => profile.value) ?? [],
75
- userId: data ? data.id : +formData.userId?.value,
93
+ ...(data && { userId: data?.id }),
76
94
  })
77
95
  }
78
96
  const onError = (err) => {
@@ -86,22 +104,38 @@ function UserProfileRelation({ close, application, profiles, data }) {
86
104
  setState((s) => {
87
105
  s.inputValue = e.target.value
88
106
  })
89
- if (e.target.value.length > 3) {
107
+ if (e.target.value.length >= 3) {
90
108
  fetchUsersFn({
91
109
  application: application,
92
110
  search: e.target.value,
111
+ departmentId: watch('departmentId') ?? null,
93
112
  })
94
113
  }
95
114
  }
96
115
  }
97
116
 
117
+ if (isLoading) {
118
+ return <Spinner />
119
+ }
120
+
98
121
  return (
99
122
  <>
100
123
  <form onSubmit={handleSubmit(onSubmit, onError)}>
101
124
  <Stack gap={4} sx={{ padding: '10px' }}>
125
+ {!data && (
126
+ <FormSingleSelect
127
+ control={control}
128
+ name="departmentId"
129
+ label="Department"
130
+ options={departments?.map((department) => ({
131
+ label: department.name,
132
+ value: department._id,
133
+ }))}
134
+ />
135
+ )}
136
+
102
137
  {!data && (
103
138
  <FormMultiSelect
104
- multiple={false}
105
139
  label={'User'}
106
140
  name={'userId'}
107
141
  onInputChange={handleInputChange}
@@ -1,5 +1,4 @@
1
1
  import { AxiosRequestConfig } from 'axios'
2
- import { id } from 'date-fns/locale'
3
2
  import * as yup from 'yup'
4
3
  import axios from '../../config/axios'
5
4
  export const defaultFilterObj = {
@@ -12,13 +11,7 @@ export const defaultFilterObj = {
12
11
  }
13
12
 
14
13
  export const userProfileSchema = yup.object().shape({
15
- userId: yup
16
- .object()
17
- .shape({
18
- label: yup.string().required('User is required'),
19
- value: yup.string().required('User is required'),
20
- })
21
- .required('User is required'),
14
+ userId: yup.array().min(1).required('User is required'),
22
15
  profileIds: yup.array().min(1).required('Profile is required'),
23
16
  })
24
17
 
@@ -50,9 +43,9 @@ export const fetchProfiles = (application) => {
50
43
  }
51
44
 
52
45
  interface ApplicationUserProfile {
53
- id?: number
46
+ userId?: number
54
47
  application: string
55
- userId: number
48
+ userIds: number[]
56
49
  profileIds: number[]
57
50
  }
58
51
  export const createApplicationUserProfile = (
@@ -73,8 +66,8 @@ export const updateCreateUserApplicationProfile = (
73
66
  postBody: ApplicationUserProfile,
74
67
  ) => {
75
68
  const config: AxiosRequestConfig = {
76
- method: id ? 'PUT' : 'POST',
77
- url: id
69
+ method: postBody?.userId ? 'PUT' : 'POST',
70
+ url: postBody?.userId
78
71
  ? `/square/profile-permissions/edit-user-permissions`
79
72
  : '/square/profile-permissions/add-user',
80
73
  data: postBody,