import { CellContext } from '@tanstack/react-table' import { __ } from '@wordpress/i18n' import classNames from 'classnames' import { useCallback, useEffect } from 'react' import apiFetch from '@wordpress/api-fetch' import { addQueryArgs } from '@wordpress/url' import { useDispatch, useSelect } from '@wordpress/data' import warningIcon from '../../../../../../public/images/warning-icon.png' import successIcon from '../../../../../../public/images/succesessful-icon.png' import { store, store_name } from '../../../../../store/backend' import '../GoogleAuth/GoogleAuth.scss' import './IcloudAuth.scss' type AuthData = { isAuthenticated?: boolean needsReconnect?: boolean lastError?: string } type IcloudAuthCellProps = CellContext & { onOpenAuth?: (row: any) => void } export const IcloudAuthCell = ({ cell, onOpenAuth, }: IcloudAuthCellProps) => { const calendarId = cell.row.original?.id const { setGgAuthData } = useDispatch(store) const refreshAuth = useCallback(() => { if (calendarId == null) return apiFetch({ path: addQueryArgs('/wbk/v2/get-calendar-auth-data/', { calendar_id: calendarId, }), }) .then((result: any) => setGgAuthData(calendarId, result)) .catch(() => setGgAuthData(calendarId, {})) }, [calendarId, setGgAuthData]) useEffect(() => { refreshAuth() }, [refreshAuth]) const authData = useSelect( (select: any) => select(store_name).getGgAuthData(calendarId), [calendarId] ) as AuthData | null if (!authData || Object.keys(authData).length === 0) { return (
) } const isAuthenticated = !!authData.isAuthenticated return (
{isAuthenticated ? __('Connected', 'webba-booking-lite') : __('Not connected', 'webba-booking-lite')}
{authData.needsReconnect && (
{__( 'Credentials invalid. Please reconnect.', 'webba-booking-lite' )}
)} {onOpenAuth && ( )}
) }