<commit_before>define([
    'preact',
    'htm',
    'md5',
    'kb_common_ts/Auth2',
    'reactComponents/ErrorAlert',
    'reactComponents/Loading',
    './LinkedAccounts',

    'bootstrap',
], (
    preact,
    htm,
    md5,
    {Auth2},
    ErrorAlert,
    Loading,
    AccountEditor
) => {

    const {h, Component} = preact;
    const html = htm.bind(h);

    class LinkedAccountsController extends Component {
        constructor(props) {
            super(props);
            this.state = {
                status: 'NONE'
            };
        }

        componentDidMount() {
            this.loadData();
        }

        async unlinkIdentity(identityId) {
            const auth2 = new Auth2({
                baseUrl: this.props.runtime.config('services.auth.url')
            });
            try {
                await auth2.removeLink(this.currentUserToken, {
                    identityId
                });
                this.props.runtime.notifySuccess(
                    'Successfully unlinked identity',
                    3000
                );
                await this.fetchIdentities();
            } catch (ex) {
                console.error(ex);
                this.props.runtime.notifyError(
                    `Error unlinking: ${ex.message}`
                );
            }
        }

        linkIdentity(providerId) {
            try {
                // TODO: routing back into here.
                const params = {
                    provider: providerId,
                    token: this.currentUserToken
                };
                const action = `${this.props.runtime.config('services.auth.url')}/link/start`;

                this.props.runtime.send('app', 'post-form', {
                    action,
                    params
                });

                // this.auth2.linkStart(this.currentUserToken, {
                //     provider: providerId,
                //     node: this.container
                // });
            } catch (ex) {
                console.error(ex);
                this.props.runtime.notifyError(
                    `Error starting the linking process: ${ex.message}`
                );
            }
        }

        async fetchIdentities() {
            const auth2 = new Auth2({
                baseUrl: this.props.runtime.config('services.auth.url')
            });
            const authToken = this.props.runtime.service('session').getAuthToken();

            const {idents} = await auth2.getMe(authToken);
            this.setState({
                status: 'SUCCESS',
                value: {
                    identities: idents
                }
            });
        }

        async loadData() {
            this.setState({
                status: 'PENDING'
            });

            try {
                await this.fetchIdentities();
            } catch (ex) {
                this.setState({
                    status: 'ERROR',
                    error: {
                        message: ex.message
                    }
                });
            }
        }

        render() {
            switch (this.state.status) {
            case 'NONE':
            case 'PENDING':
                return html`
                    <${Loading} message="Loading..." />
                `;
            case 'SUCCESS':
                return html`
                    <${AccountEditor} 
                        runtime=${this.props.runtime} 
                        identities=${this.state.value.identities}
                        params=${this.props.params} 
                        linkIdentity=${this.linkIdentity.bind(this)}
                        unlinkIdentity=${this.unlinkIdentity.bind(this)}
                    />
                `;
            case 'ERROR':
                return html`
                    <${ErrorAlert} message=${this.state.error.message} />
                `;
            }
        }
    }

    return LinkedAccountsController;
});<commit_msg>initialize current login token [UFI-28]<commit_after>define([
    'preact',
    'htm',
    'md5',
    'kb_common_ts/Auth2',
    'reactComponents/ErrorAlert',
    'reactComponents/Loading',
    './LinkedAccounts',

    'bootstrap',
], (
    preact,
    htm,
    md5,
    {Auth2},
    ErrorAlert,
    Loading,
    AccountEditor
) => {

    const {h, Component} = preact;
    const html = htm.bind(h);

    class LinkedAccountsController extends Component {
        constructor(props) {
            super(props);
            this.currentUserToken = props.runtime.service('session').getAuthToken();
            this.state = {
                status: 'NONE'
            };
        }

        componentDidMount() {
            this.loadData();
        }

        async unlinkIdentity(identityId) {
            const auth2 = new Auth2({
                baseUrl: this.props.runtime.config('services.auth.url')
            });
            try {
                await auth2.removeLink(this.currentUserToken, {
                    identityId
                });
                this.props.runtime.notifySuccess(
                    'Successfully unlinked identity',
                    3000
                );
                await this.fetchIdentities();
            } catch (ex) {
                console.error(ex);
                this.props.runtime.notifyError(
                    `Error unlinking: ${ex.message}`
                );
            }
        }

        linkIdentity(providerId) {
            try {
                // TODO: routing back into here.
                const params = {
                    provider: providerId,
                    token: this.currentUserToken
                };
                const action = `${this.props.runtime.config('services.auth.url')}/link/start`;

                this.props.runtime.send('app', 'post-form', {
                    action,
                    params
                });

                // this.auth2.linkStart(this.currentUserToken, {
                //     provider: providerId,
                //     node: this.container
                // });
            } catch (ex) {
                console.error(ex);
                this.props.runtime.notifyError(
                    `Error starting the linking process: ${ex.message}`
                );
            }
        }

        async fetchIdentities() {
            const auth2 = new Auth2({
                baseUrl: this.props.runtime.config('services.auth.url')
            });
            const authToken = this.props.runtime.service('session').getAuthToken();

            const {idents} = await auth2.getMe(authToken);
            this.setState({
                status: 'SUCCESS',
                value: {
                    identities: idents
                }
            });
        }

        async loadData() {
            this.setState({
                status: 'PENDING'
            });

            try {
                await this.fetchIdentities();
            } catch (ex) {
                this.setState({
                    status: 'ERROR',
                    error: {
                        message: ex.message
                    }
                });
            }
        }

        render() {
            switch (this.state.status) {
            case 'NONE':
            case 'PENDING':
                return html`
                    <${Loading} message="Loading..." />
                `;
            case 'SUCCESS':
                return html`
                    <${AccountEditor} 
                        runtime=${this.props.runtime} 
                        identities=${this.state.value.identities}
                        params=${this.props.params} 
                        linkIdentity=${this.linkIdentity.bind(this)}
                        unlinkIdentity=${this.unlinkIdentity.bind(this)}
                    />
                `;
            case 'ERROR':
                return html`
                    <${ErrorAlert} message=${this.state.error.message} />
                `;
            }
        }
    }

    return LinkedAccountsController;
});
{'fraction_non_alphanumeric': 0.10899888902604617, 'fraction_numerical': 0.003950129613627947, 'mean_word_length': 1.0413202317964223, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1121747', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1894, 'n_words': 328}