-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Queries got separated in custom hooks
- Loading branch information
Showing
8 changed files
with
211 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useEffect } from "react"; | ||
import AsyncStorage from "@react-native-async-storage/async-storage"; | ||
import { useSelector, useDispatch } from "react-redux"; | ||
import { jwtDecode } from "jwt-decode"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { getUserChats } from "../http/chat"; | ||
import { userActions } from "../redux/userSlice"; | ||
|
||
const useChatHistory = () => { | ||
const dispatch = useDispatch(); | ||
const chatHistory = useSelector((state) => state.user.chatHistory); | ||
|
||
const { | ||
data: chatHistoryData, | ||
isPending, | ||
isError, | ||
error, | ||
refetch, | ||
} = useQuery({ | ||
queryKey: ["getHistory"], | ||
queryFn: async () => { | ||
const token = await AsyncStorage.getItem("token"); | ||
const decodedToken = jwtDecode(token); | ||
const userChats = await getUserChats({ | ||
token: token, | ||
userId: decodedToken.sub, | ||
}); | ||
return userChats; | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
refetch(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (chatHistoryData) { | ||
dispatch(userActions.setChatHistory(chatHistoryData)); | ||
} | ||
}, [chatHistoryData]); | ||
|
||
return { | ||
chatHistory, | ||
isPending, | ||
isError, | ||
error, | ||
refetchChatHistory: refetch, | ||
}; | ||
}; | ||
|
||
export default useChatHistory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { userActions } from "../redux/userSlice"; | ||
import { continueChat } from "../http/chat"; | ||
import { uiActions } from "../redux/uiSlice"; | ||
const useContinueChatMutation = () => { | ||
const dispatch = useDispatch(); | ||
const selectedChat = useSelector((state) => state.user.selectedChat); | ||
|
||
const { | ||
mutate: keepChatting, | ||
isPending: isChatting, | ||
isError: isChatError, | ||
error: chatError, | ||
} = useMutation({ | ||
mutationKey: "continue", | ||
mutationFn: continueChat, | ||
onMutate: () => { | ||
dispatch(uiActions.setIsThinking(true)); | ||
dispatch(uiActions.setInput("")); | ||
}, | ||
onSuccess: (response) => { | ||
const newChatMessage = { | ||
role: "bot", | ||
content: response.data.response, | ||
}; | ||
dispatch( | ||
userActions.selectChat({ | ||
...selectedChat, | ||
content: [...(selectedChat.content || []), newChatMessage], | ||
}) | ||
); | ||
dispatch(uiActions.setIsThinking(false)); | ||
}, | ||
onError: (error) => {}, | ||
}); | ||
|
||
return { | ||
keepChatting, | ||
isChatting, | ||
isChatError, | ||
chatError, | ||
}; | ||
}; | ||
|
||
export default useContinueChatMutation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { newChat } from "../http/chat"; | ||
import { uiActions } from "../redux/uiSlice"; | ||
import { userActions } from "../redux/userSlice"; | ||
const useChatMutation = () => { | ||
const dispatch = useDispatch(); | ||
const selectedChat = useSelector((state) => state.user.selectedChat); | ||
|
||
const { | ||
mutate: initialMessage, | ||
isPending, | ||
isError, | ||
error, | ||
} = useMutation({ | ||
mutationFn: newChat, | ||
onMutate: () => { | ||
dispatch(uiActions.setIsThinking(true)); | ||
dispatch(uiActions.setInput("")); | ||
}, | ||
onSuccess: (response) => { | ||
const newChatMessage = { | ||
role: "bot", | ||
content: response.data.response, | ||
}; | ||
dispatch( | ||
userActions.selectChat({ | ||
id: response.data.chatId, | ||
title: response.data.title, | ||
content: [...(selectedChat.content || []), newChatMessage], | ||
}) | ||
); | ||
dispatch(uiActions.setInput("")); | ||
dispatch(uiActions.setIsThinking(false)); | ||
}, | ||
}); | ||
|
||
return { | ||
initialMessage, | ||
isPending, | ||
isError, | ||
error, | ||
}; | ||
}; | ||
|
||
export default useChatMutation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import AsyncStorage from "@react-native-async-storage/async-storage"; | ||
import { useNavigation } from "@react-navigation/native"; | ||
import { useDispatch } from "react-redux"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { getChat } from "../http/chat"; | ||
import { userActions } from "../redux/userSlice"; | ||
|
||
const useSelectChat = () => { | ||
const dispatch = useDispatch(); | ||
const navigation = useNavigation(); | ||
|
||
const { mutate } = useMutation({ | ||
mutationFn: getChat, | ||
onSuccess: (response) => { | ||
const { requests, responses } = response.data.chat; | ||
const minLength = Math.min(requests.length, responses.length); | ||
let combinedArray = []; | ||
for (let i = 0; i < minLength; i++) { | ||
combinedArray.push({ content: requests[i], role: "user" }); | ||
combinedArray.push({ content: responses[i], role: "bot" }); | ||
} | ||
|
||
for (let i = minLength; i < requests.length; i++) { | ||
combinedArray.push({ content: requests[i], role: "user" }); | ||
} | ||
|
||
for (let i = minLength; i < responses.length; i++) { | ||
combinedArray.push({ content: responses[i], role: "bot" }); | ||
} | ||
dispatch( | ||
userActions.selectChat({ | ||
id: response.data.id, | ||
title: response.data.title, | ||
content: combinedArray, | ||
}) | ||
); | ||
navigation.navigate("Home"); | ||
}, | ||
}); | ||
|
||
const selectChat = async (chat) => { | ||
const token = await AsyncStorage.getItem("token"); | ||
if (token) { | ||
mutate({ token, chatId: chat.id }); | ||
} else { | ||
navigation.navigate("LandingPage"); | ||
} | ||
}; | ||
|
||
return selectChat; | ||
}; | ||
|
||
export default useSelectChat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters