How to use the react-components.useApi function in react-components

To help you get started, we’ve selected a few react-components examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ProtonMail / react-components / models / subscriptionModel.js View on Github external
export const useSubscription = () => {
    const [user] = useUser();
    const api = useApi();

    return useCachedAsyncResult(
        SubscriptionModel.key,
        () => {
            if (user.isPaid) {
                return SubscriptionModel.get(api);
            }
            return Promise.resolve(FREE_SUBSCRIPTION);
        },
        []
    ); // Don't need to depend on the user changes since the subscription is updated through the event manager
};
github ProtonMail / react-components / containers / sessions / SessionsSection.js View on Github external
const SessionsSection = () => {
    const { createNotification } = useNotifications();
    const api = useApi();
    const authentication = useAuthentication();
    const [loading, withLoading] = useLoading();
    const [table, setTable] = useState({ sessions: [], total: 0 });
    const { page, list, onNext, onPrevious, onSelect } = usePagination(table.sessions);
    const { createModal } = useModals();

    const fetchSessions = async () => {
        const { Sessions } = await api(querySessions());
        setTable({ sessions: Sessions.reverse(), total: Sessions.length }); // Most recent, first
    };

    const handleRevoke = async (UID) => {
        await api(UID ? revokeSession(UID) : revokeOtherSessions());
        await fetchSessions();
        createNotification({ text: UID ? c('Success').t`Session revoked` : c('Success').t`Sessions revoked` });
    };
github ProtonMail / react-components / containers / payments / subscription / NewSubscriptionModal.js View on Github external
cycle = DEFAULT_CYCLE,
    currency = DEFAULT_CURRENCY,
    coupon,
    planIDs = {},
    onClose,
    ...rest
}) => {
    const TITLE = {
        [SUBSCRIPTION_STEPS.NETWORK_ERROR]: c('Title').t`Network error`,
        [SUBSCRIPTION_STEPS.CUSTOMIZATION]: c('Title').t`Plan customization`,
        [SUBSCRIPTION_STEPS.PAYMENT]: c('Title').t`Checkout`,
        [SUBSCRIPTION_STEPS.UPGRADE]: <div>{c('Title').t`Processing...`}</div>,
        [SUBSCRIPTION_STEPS.THANKS]: <div>{c('Title').t`Thank you!`}</div>
    };

    const api = useApi();
    const [user] = useUser();
    const { call } = useEventManager();
    const { createModal } = useModals();
    const { createNotification } = useNotifications();
    const [vpnCountries, loadingVpnCountries] = useVPNCountries();
    const [plans, loadingPlans] = usePlans();
    const [organization, loadingOrganization] = useOrganization();
    const [loading, withLoading] = useLoading();
    const [loadingCheck, withLoadingCheck] = useLoading();
    const [checkResult, setCheckResult] = useState({});
    const { Credit = 0 } = checkResult;
    const { Code: couponCode } = checkResult.Coupon || {}; // Coupon can be null
    const creditsRemaining = (user.Credit + Credit) / 100;
    const [model, setModel] = useState({
        cycle,
        currency,
github ProtonMail / react-components / containers / account / NewsCheckboxes.js View on Github external
const NewsCheckboxes = () => {
    const [{ News } = {}] = useUserSettings();
    const { createNotification } = useNotifications();
    const { call } = useEventManager();
    const api = useApi();
    const [loading, withLoading] = useLoading();

    const update = async (mask) => {
        await api(updateNews(toggleBit(News, mask)));
        await call();
        createNotification({ text: c('Info').t`Emailing preference updated` });
    };

    const handleChange = (mask) => () => withLoading(update(mask));

    const checkboxes = [
        { id: 'announcements', flag: ANNOUNCEMENTS, text: c('Label for news').t`Major announcements (2-3 per year)` },
        { id: 'features', flag: FEATURES, text: c('Label for news').t`Major features (3-4 per year)` },
        { id: 'business', flag: BUSINESS, text: c('Label for news').t`Proton Business (4-5 per year)` },
        { id: 'newsletter', flag: NEWSLETTER, text: c('Label for news').t`Proton newsletter (5-6 per year)` },
        { id: 'beta', flag: BETA, text: c('Label for news').t`Proton Beta (10-12 per year)` }
github ProtonMail / react-components / models / organizationModel.js View on Github external
export const useOrganization = () => {
    const [user] = useUser();
    const api = useApi();

    return useCachedAsyncResult(
        OrganizationModel.key,
        () => {
            if (user.isPaid) {
                return OrganizationModel.get(api);
            }
            return Promise.resolve(FREE_ORGANIZATION);
        },
        []
    );
};
github ProtonMail / proton-contacts / src / app / hooks / useContact.js View on Github external
const useContact = (contactID) => {
    const cache = useContext(ContactProviderContext);
    const api = useApi();

    const miss = useCallback(() => {
        return api(getContact(contactID)).then(({ Contact }) => Contact);
    }, []);

    return useCachedModelResult(cache, contactID, miss);
};
github ProtonMail / react-components / containers / general / AutoSaveContactsToggle.js View on Github external
const AutoSaveContactsToggle = ({ autoSaveContacts, ...rest }) =&gt; {
    const api = useApi();
    const [loading, withLoading] = useLoading();
    const { createNotification } = useNotifications();
    const { call } = useEventManager();

    const handleChange = async ({ target }) =&gt; {
        await api(updateAutoSaveContacts(+target.checked));
        await call();
        createNotification({ text: c('Success').t`Preference saved` });
    };

    return (
         withLoading(handleChange(event))}
            {...rest}
github ProtonMail / react-components / containers / notification / DailyNotificationsToggle.js View on Github external
const DailyNotificationsToggle = ({ id }) =&gt; {
    const { call } = useEventManager();
    const api = useApi();
    const [{ Email }] = useUserSettings();
    const [loading, withLoading] = useLoading();

    const handleChange = async (checked) =&gt; {
        await api(updateNotifyEmail(checked));
        await call();
    };

    return (
         withLoading(handleChange(+checked))}
        /&gt;
    );
github ProtonMail / react-components / containers / login / LoginForm.js View on Github external
const LoginForm = ({ onLogin, ignoreUnlock = false, needHelp }) => {
    const { createNotification } = useNotifications();
    const { createModal } = useModals();
    const cacheRef = useRef();
    const api = useApi();

    const [loading, withLoading] = useLoading();
    const [form, setForm] = useState(FORM.LOGIN);
    const [username, setUsername] = useState('');
    const [password, setPassword] = useState('');
    const [totp, setTotp] = useState('');
    const [keyPassword, setKeyPassword] = useState('');

    /**
     * Finalize login can be called without a key password in these cases:
     * 1) The admin panel
     * 2) Users who have no keys but are in 2-password mode
     * @param {String} [keyPassword]
     * @return {Promise}
     */
    const finalizeLogin = async (keyPassword) => {
github ProtonMail / react-components / containers / login / LoginContainer.js View on Github external
const LoginContainer = ({ onLogin, needHelp, ignoreUnlock = false }) => {
    const { createNotification } = useNotifications();
    const cacheRef = useRef();
    const api = useApi();

    const [loading, withLoading] = useLoading();
    const [form, setForm] = useState(FORM.LOGIN);
    const [username, setUsername] = useState('');
    const [password, setPassword] = useState('');
    const [totp, setTotp] = useState('');
    const [keyPassword, setKeyPassword] = useState('');

    /**
     * Finalize login can be called without a key password in these cases:
     * 1) The admin panel
     * 2) Users who have no keys but are in 2-password mode
     * @param {String} [keyPassword]
     * @return {Promise}
     */
    const finalizeLogin = async (keyPassword) => {

react-components

React components used by Khan Academy

MIT
Latest version published 7 years ago

Package Health Score

48 / 100
Full package analysis