Files
ColdBreeze-chat-web/src/common/hook/useGoogleAuthConfig.js
T
2022-06-12 15:30:14 +08:00

39 lines
1.0 KiB
JavaScript

import { useState, useEffect } from "react";
// import toast from "react-hot-toast";
import {
useGetGoogleAuthConfigQuery,
useUpdateGoogleAuthConfigMutation
} from "../../app/services/server";
export default function useGoogleAuthConfig() {
const [changed, setChanged] = useState(false);
const [clientId, setClientId] = useState("");
const { data } = useGetGoogleAuthConfigQuery(undefined, {
refetchOnMountOrArgChange: true
});
const [updateGoogleAuthConfig, { isSuccess }] = useUpdateGoogleAuthConfigMutation();
useEffect(() => {
if (data) {
setClientId(data.client_id);
}
}, [data]);
useEffect(() => {
setChanged(isSuccess ? false : data?.client_id !== clientId);
}, [data, clientId, isSuccess]);
const updateClientIdToServer = async () => {
if (!clientId) return;
await updateGoogleAuthConfig({ client_id: clientId });
};
return {
config: data,
changed,
clientId,
updateClientId: setClientId,
updateClientIdToServer,
updateGoogleAuthConfig,
isSuccess
};
}