<commit_before>import { useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import { c } from 'ttag';
import { privatizeMember, updateName, updateQuota, updateRole, updateVPN } from '@proton/shared/lib/api/members';
import { GIGA, MEMBER_PRIVATE, MEMBER_ROLE, MEMBER_SUBSCRIBER } from '@proton/shared/lib/constants';
import { FormModal, Row, Field, Label, Input, Toggle, ConfirmModal, Alert } from '../../components';
import { useApi, useNotifications, useEventManager, useOrganization, useLoading, useModals } from '../../hooks';

import MemberStorageSelector, { getStorageRange } from './MemberStorageSelector';
import MemberVPNSelector, { getVPNRange } from './MemberVPNSelector';
import Addresses from '../addresses/Addresses';

const EditMemberModal = ({ onClose, member, ...rest }) => {
    const [organization] = useOrganization();
    const { call } = useEventManager();
    const { createModal } = useModals();

    const initialModel = useMemo(
        () => ({
            name: member.Name,
            storage: member.MaxSpace,
            vpn: member.MaxVPN,
            private: member.Private,
            admin: member.Role === MEMBER_ROLE.ORGANIZATION_OWNER,
        }),
        [member]
    );

    const [model, updateModel] = useState(initialModel);

    const [loading, withLoading] = useLoading();
    const { createNotification } = useNotifications();
    const api = useApi();

    const hasVPN = Boolean(organization.MaxVPN);
    const canMakePrivate = member.Private === MEMBER_PRIVATE.READABLE;
    const canMakeAdmin = !member.Self && member.Role === MEMBER_ROLE.ORGANIZATION_MEMBER;
    const canRevokeAdmin = !member.Self && member.Role === MEMBER_ROLE.ORGANIZATION_OWNER;

    const updatePartialModel = (partial) => {
        updateModel({ ...model, ...partial });
    };

    const handleChangeName = ({ target: { value } }) => updatePartialModel({ name: value });
    const handleChangeStorage = (storage) => updatePartialModel({ storage });
    const handleChangeVPN = (vpn) => updatePartialModel({ vpn });
    const handleChangePrivate = ({ target: { checked } }) => updatePartialModel({ private: checked });
    const handleChangeAdmin = ({ target: { checked } }) => updatePartialModel({ admin: checked });

    const handleSubmit = async () => {
        await api(updateName(member.ID, model.name));

        await api(updateQuota(member.ID, +model.storage));

        if (hasVPN) {
            await api(updateVPN(member.ID, +model.vpn));
        }

        if (canMakePrivate && model.private && model.private !== initialModel.Private) {
            await api(privatizeMember(member.ID));
        }

        if (canMakeAdmin && model.admin && model.admin !== initialModel.Admin) {
            await api(updateRole(member.ID, MEMBER_ROLE.ORGANIZATION_OWNER));
        }

        if (canRevokeAdmin && !model.admin && model.admin !== initialModel.Admin) {
            try {
                await new Promise((resolve, reject) => {
                    createModal(
                        <ConfirmModal
                            onClose={reject}
                            onConfirm={() => resolve(undefined)}
                            title={c('Title').t`Change role`}
                        >
                            <Alert className="mb1">
                                {member.Subscriber === MEMBER_SUBSCRIBER.PAYER
                                    ? c('Info')
                                          .t`This user is currently responsible for payments for your organization. By demoting this member, you will become responsible for payments for your organization.`
                                    : c('Info')
                                          .t`Are you sure you want to remove administrative privileges from this user?`}
                            </Alert>
                        </ConfirmModal><commit_msg>Check for diff before updating member name & quota<commit_after>import { useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import { c } from 'ttag';
import { privatizeMember, updateName, updateQuota, updateRole, updateVPN } from '@proton/shared/lib/api/members';
import { GIGA, MEMBER_PRIVATE, MEMBER_ROLE, MEMBER_SUBSCRIBER } from '@proton/shared/lib/constants';
import { FormModal, Row, Field, Label, Input, Toggle, ConfirmModal, Alert } from '../../components';
import { useApi, useNotifications, useEventManager, useOrganization, useLoading, useModals } from '../../hooks';

import MemberStorageSelector, { getStorageRange } from './MemberStorageSelector';
import MemberVPNSelector, { getVPNRange } from './MemberVPNSelector';
import Addresses from '../addresses/Addresses';

const EditMemberModal = ({ onClose, member, ...rest }) => {
    const [organization] = useOrganization();
    const { call } = useEventManager();
    const { createModal } = useModals();

    const initialModel = useMemo(
        () => ({
            name: member.Name,
            storage: member.MaxSpace,
            vpn: member.MaxVPN,
            private: member.Private,
            admin: member.Role === MEMBER_ROLE.ORGANIZATION_OWNER,
        }),
        [member]
    );

    const [model, updateModel] = useState(initialModel);

    const [loading, withLoading] = useLoading();
    const { createNotification } = useNotifications();
    const api = useApi();

    const hasVPN = Boolean(organization.MaxVPN);
    const canMakePrivate = member.Private === MEMBER_PRIVATE.READABLE;
    const canMakeAdmin = !member.Self && member.Role === MEMBER_ROLE.ORGANIZATION_MEMBER;
    const canRevokeAdmin = !member.Self && member.Role === MEMBER_ROLE.ORGANIZATION_OWNER;

    const updatePartialModel = (partial) => {
        updateModel({ ...model, ...partial });
    };

    const handleChangeName = ({ target: { value } }) => updatePartialModel({ name: value });
    const handleChangeStorage = (storage) => updatePartialModel({ storage });
    const handleChangeVPN = (vpn) => updatePartialModel({ vpn });
    const handleChangePrivate = ({ target: { checked } }) => updatePartialModel({ private: checked });
    const handleChangeAdmin = ({ target: { checked } }) => updatePartialModel({ admin: checked });

    const handleSubmit = async () => {
        if (initialModel.name !== model.name) {
            await api(updateName(member.ID, model.name));
        }

        if (initialModel.storage !== model.storage) {
            await api(updateQuota(member.ID, +model.storage));
        }

        if (hasVPN) {
            await api(updateVPN(member.ID, +model.vpn));
        }

        if (canMakePrivate && model.private && model.private !== initialModel.private) {
            await api(privatizeMember(member.ID));
        }

        if (canMakeAdmin && model.admin && model.admin !== initialModel.Admin) {
            await api(updateRole(member.ID, MEMBER_ROLE.ORGANIZATION_OWNER));
        }

        if (canRevokeAdmin && !model.admin && model.admin !== initialModel.Admin) {
            try {
                await new Promise((resolve, reject) => {
                    createModal(
                        <ConfirmModal
                            onClose={reject}
                            onConfirm={() => resolve(undefined)}
                            title={c('Title').t`Change role`}
                        >
                            <Alert className="mb1">
                                {member.Subscriber === MEMBER_SUBSCRIBER.PAYER
                                    ? c('Info')
                                          .t`This user is currently responsible for payments for your organization. By demoting this member, you will become responsible for payments for your organization.`
                                    : c('Info')
                                          .t`Are you sure you want to remove administrative privileges from this user?`}
                            </Alert>
                        </ConfirmModal>
{'fraction_non_alphanumeric': 0.12165725047080979, 'fraction_numerical': 0.00025109855618330194, 'mean_word_length': 2.2795389048991352, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6611431', 'n_tokens_mistral': 2091, 'n_tokens_neox': 2053, 'n_words': 508}