direct login
This commit is contained in:
@@ -7,7 +7,9 @@ import { useTranslation } from 'react-i18next'
|
||||
import { ItemServiceType } from '../pages/service/types/ServiecTypes'
|
||||
import moment from 'moment-jalaali'
|
||||
import { NumberFormat } from '../config/func'
|
||||
import { useGetDmenuSubscription } from '../pages/service/hooks/useServiceData'
|
||||
import { useDirectLogin, useGetDmenuSubscription } from '../pages/service/hooks/useServiceData'
|
||||
import { toast } from './Toast'
|
||||
import { ErrorType } from '../helpers/types'
|
||||
|
||||
type Props = {
|
||||
item: ItemServiceType,
|
||||
@@ -30,6 +32,7 @@ const ServiceItem: FC<Props> = (props: Props) => {
|
||||
|
||||
const isDmenu = item?.slug === 'Dmenu'
|
||||
const { data: dmenuSubscription, isPending, isError } = useGetDmenuSubscription(isChecked && isDmenu && props.isLinkPanel ? props.subscriptionId! : '',)
|
||||
const { mutate: directLogin } = useDirectLogin()
|
||||
|
||||
const getRemainingDays = (endDate: string): number => {
|
||||
const now = moment()
|
||||
@@ -59,11 +62,31 @@ const ServiceItem: FC<Props> = (props: Props) => {
|
||||
return `${NumberFormat(days)} روز باقی مانده`
|
||||
}
|
||||
|
||||
const handleDirectLogin = () => {
|
||||
if (!isDmenu) {
|
||||
window.open(item.link, '_blank')
|
||||
return
|
||||
}
|
||||
if (!props.subscriptionId) {
|
||||
return
|
||||
}
|
||||
directLogin(props.subscriptionId, {
|
||||
onSuccess: (data) => {
|
||||
window.open(item.link + `?token=${data?.data?.tokens?.accessToken?.token}&refreshToken=${data?.data?.tokens?.refreshToken?.token}`, '_blank')
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast(error.response?.data?.error?.message[0], 'error')
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (isChecked && isDmenu && props.isLinkPanel) {
|
||||
if (dmenuSubscription && !isPending) {
|
||||
if (item.link) {
|
||||
window.open(item.link, '_blank')
|
||||
// window.open(item.link, '_blank')
|
||||
handleDirectLogin()
|
||||
}
|
||||
setIsChecked(false)
|
||||
} else if (isError && !isPending) {
|
||||
@@ -84,7 +107,8 @@ const ServiceItem: FC<Props> = (props: Props) => {
|
||||
if (isDmenu && props.subscriptionId) {
|
||||
setIsChecked(true)
|
||||
} else {
|
||||
window.open(item.link, '_blank')
|
||||
// window.open(item.link, '_blank')
|
||||
handleDirectLogin()
|
||||
}
|
||||
} else {
|
||||
navigate(Pages.services.detail + item.slug)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { type FC } from 'react'
|
||||
import { useParams } from 'react-router-dom'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useCreateDmenuRestaurant, useGetSubscription } from './hooks/useServiceData'
|
||||
import { useCreateDmenuRestaurant, useDirectLogin, useGetSubscription } from './hooks/useServiceData'
|
||||
import { useFormik } from 'formik'
|
||||
import { DmenuCreateRestaurantType } from './types/ServiecTypes'
|
||||
import * as Yup from 'yup'
|
||||
@@ -13,11 +13,32 @@ import { TickCircle } from 'iconsax-react'
|
||||
import { Helmet } from 'react-helmet-async'
|
||||
|
||||
export const ManageRestaurant: FC = () => {
|
||||
|
||||
const { t } = useTranslation('global')
|
||||
const { subscriptionId } = useParams()
|
||||
const createDmenuRestaurant = useCreateDmenuRestaurant()
|
||||
const getSubscription = useGetSubscription(subscriptionId!)
|
||||
const getSubscription = useGetSubscription(subscriptionId || '')
|
||||
const { mutate: directLogin } = useDirectLogin()
|
||||
|
||||
const handleDirectLogin = () => {
|
||||
if (!subscriptionId) {
|
||||
toast(t('errors.required') || 'شناسه اشتراک یافت نشد', 'error')
|
||||
return
|
||||
}
|
||||
|
||||
directLogin(subscriptionId, {
|
||||
onSuccess: (data) => {
|
||||
const serviceLink = getSubscription?.data?.data?.userSubscription?.plan?.service?.link
|
||||
if (serviceLink && data?.data?.tokens?.accessToken?.token && data?.data?.tokens?.refreshToken?.token) {
|
||||
window.location.href = `${serviceLink}?token=${data.data.tokens.accessToken.token}&refreshToken=${data.data.tokens.refreshToken.token}`
|
||||
} else {
|
||||
toast('خطا در دریافت اطلاعات سرویس', 'error')
|
||||
}
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast(error.response?.data?.error?.message?.[0] || 'خطا در ورود مستقیم', 'error')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const formik = useFormik<DmenuCreateRestaurantType>({
|
||||
initialValues: {
|
||||
@@ -27,6 +48,7 @@ export const ManageRestaurant: FC = () => {
|
||||
phone: '',
|
||||
userSubscriptionId: subscriptionId || ''
|
||||
},
|
||||
enableReinitialize: true,
|
||||
validationSchema: Yup.object({
|
||||
name: Yup.string().required(t('errors.required')),
|
||||
slug: Yup.string().required(t('errors.required')),
|
||||
@@ -44,12 +66,12 @@ export const ManageRestaurant: FC = () => {
|
||||
}
|
||||
createDmenuRestaurant.mutate(submitValues, {
|
||||
onSuccess: () => {
|
||||
toast(t('success'), 'success')
|
||||
toast(t('success') || 'با موفقیت انجام شد', 'success')
|
||||
formik.resetForm()
|
||||
window.location.href = getSubscription?.data?.data?.userSubscription?.plan?.service?.link as string
|
||||
handleDirectLogin()
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast(error.response?.data?.error?.message[0], 'error')
|
||||
toast(error.response?.data?.error?.message?.[0] || 'خطا در ایجاد رستوران', 'error')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -84,3 +84,10 @@ export const useGetSubscription = (subscriptionId: string) => {
|
||||
enabled: !!subscriptionId,
|
||||
});
|
||||
};
|
||||
|
||||
export const useDirectLogin = () => {
|
||||
return useMutation({
|
||||
mutationFn: api.directLogin,
|
||||
mutationKey: ["direct-login"],
|
||||
});
|
||||
};
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
ExtentSubscriptionResponseType,
|
||||
DmenuCreateRestaurantType,
|
||||
GetSubscriptionResponseType,
|
||||
DirectLoginResponseType,
|
||||
} from "../types/ServiecTypes";
|
||||
|
||||
export const getServiceSuggestedDanak = async () => {
|
||||
@@ -85,3 +86,12 @@ export const getSubscription = async (
|
||||
const { data } = await axios.get(`/subscriptions/user/${subscriptionId}`);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const directLogin = async (
|
||||
userSubscriptionId: string
|
||||
): Promise<DirectLoginResponseType> => {
|
||||
const { data } = await axios.post(
|
||||
`/dmenu/auth/direct-login/${userSubscriptionId}`
|
||||
);
|
||||
return data;
|
||||
};
|
||||
|
||||
@@ -368,3 +368,33 @@ export type GetSubscriptionResponseType = {
|
||||
userSubscription: UserSubscriptionWithDetailsType;
|
||||
};
|
||||
};
|
||||
|
||||
export type DirectLoginResponseType = {
|
||||
statusCode: number;
|
||||
success: boolean;
|
||||
data: {
|
||||
tokens: {
|
||||
accessToken: {
|
||||
token: string;
|
||||
expire: number;
|
||||
};
|
||||
refreshToken: {
|
||||
token: string;
|
||||
expire: number;
|
||||
};
|
||||
};
|
||||
admin: {
|
||||
id: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
phone: string;
|
||||
role: string;
|
||||
permissions: string[];
|
||||
restaurant: {
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user