|
1 | 1 | import { useState, useEffect, ChangeEvent, useRef } from 'react'; |
| 2 | +import { useRouter } from 'next/router'; |
2 | 3 | import classNames from './UserSearchField.module.scss'; |
3 | 4 | import { useGetAllUsersQuery } from '@/app/services/usersApi'; |
4 | 5 | import { useLazyGetLogsQuery } from '@/app/services/logsApi'; |
| 6 | +import { logs } from '@/constants/calendar'; |
5 | 7 | import { userDataType } from '@/interfaces/user.type'; |
6 | 8 | import { useOutsideAlerter } from '@/hooks/useOutsideAlerter'; |
7 | 9 |
|
@@ -31,6 +33,9 @@ type CalendarData = { |
31 | 33 | }; |
32 | 34 |
|
33 | 35 | const SearchField = ({ onSearchTextSubmitted, loading }: SearchFieldProps) => { |
| 36 | + const router = useRouter(); |
| 37 | + const isDevMode = router.query.dev === 'true'; |
| 38 | + |
34 | 39 | const handleOutsideClick = () => { |
35 | 40 | setDisplayList([]); |
36 | 41 | }; |
@@ -66,7 +71,15 @@ const SearchField = ({ onSearchTextSubmitted, loading }: SearchFieldProps) => { |
66 | 71 | return; |
67 | 72 | } |
68 | 73 |
|
69 | | - // Fetch logs for OOO data only |
| 74 | + // Feature flag: Use different data sources based on dev mode |
| 75 | + if (!isDevMode) { |
| 76 | + // Non-dev mode: Use mock data from constants (original behavior) |
| 77 | + const userData = data.find((item: any) => item.userId === user.id); |
| 78 | + onSearchTextSubmitted(user, userData ? [userData] : []); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // Dev mode: Fetch real OOO data from logs API |
70 | 83 | const logsResult = await triggerGetLogs({ |
71 | 84 | username: user.username || undefined, |
72 | 85 | type: 'REQUEST_CREATED', |
@@ -95,16 +108,25 @@ const SearchField = ({ onSearchTextSubmitted, loading }: SearchFieldProps) => { |
95 | 108 | onSearchTextSubmitted(user, mapped); |
96 | 109 | }; |
97 | 110 |
|
98 | | - const { data: userData, isLoading } = useGetAllUsersQuery(); |
| 111 | + const { data: userData, isError, isLoading } = useGetAllUsersQuery(); |
99 | 112 | const [usersList, setUsersList] = useState<userDataType[]>([]); |
100 | 113 | const [displayList, setDisplayList] = useState<userDataType[]>([]); |
| 114 | + const [data, setData] = useState([]); |
101 | 115 |
|
102 | 116 | useEffect(() => { |
103 | 117 | if (userData?.users) { |
104 | 118 | const users: userDataType[] = userData.users; |
105 | 119 | const filteredUsers: userDataType[] = users.filter( |
106 | 120 | (user: userDataType) => !user.incompleteUserDetails |
107 | 121 | ); |
| 122 | + const logData: any = filteredUsers.map((user: userDataType) => { |
| 123 | + const log = logs[Math.floor(Math.random() * 4)]; |
| 124 | + return { |
| 125 | + data: log, |
| 126 | + userId: user.id, |
| 127 | + }; |
| 128 | + }); |
| 129 | + setData(logData); |
108 | 130 | setUsersList(filteredUsers); |
109 | 131 | } |
110 | 132 | }, [isLoading, userData]); |
@@ -167,10 +189,7 @@ const SearchField = ({ onSearchTextSubmitted, loading }: SearchFieldProps) => { |
167 | 189 | <button |
168 | 190 | className={classNames.userSearchSubmitButton} |
169 | 191 | disabled={ |
170 | | - loading || |
171 | | - isLogsFetching || |
172 | | - !(searchText ?? '').trim() || |
173 | | - !isValidUsername() |
| 192 | + loading || !(searchText ?? '').trim() || !isValidUsername() |
174 | 193 | } |
175 | 194 | > |
176 | 195 | Submit |
|
0 commit comments