<commit_before>import m from 'mithril';
import _ from 'underscore';
import h from '../h';
import contributionVM from '../vms/contribution-vm';
import userVM from '../vms/user-vm';
import loadMoreBtn from './load-more-btn';

const I18nScope = _.partial(h.i18nScope, 'payment.state');

const userContributedBox = {
    controller(args) {
        const toggleDelivery = (projectId, contribution) => {
            userVM.toggleDelivery(projectId, contribution).then(() => {
                const lastStatus = contribution.reward_sent_at ? 'delivered' : 'undelivered';
                contribution.delivery_status = contribution.delivery_status == 'received' ? lastStatus : 'received'; //so we don't have to reload the page
            });
        };
        return {
            toggleAnonymous: userVM.toggleAnonymous,
            toggleDelivery: toggleDelivery
        };
    },
    view(ctrl, args) {
        let collection = args.collection,
            pagination = args.pagination,
            title = args.title;
        const statusText = (contribution) => {
            switch (contribution.delivery_status) {
                case 'delivered':
                    return m('span.fontsize-smallest.badge.badge-success',
                        'Enviada'
                    );
                case 'received':
                    return m('span.fontsize-smallest.badge.badge-success',
                        'Recebida'
                    );
                case 'undelivered':
                    return m('span.fontsize-smallest.badge.badge-light',
                        'Não enviada'
                    );
                case 'error':
                    return m('span.fontsize-smallest.badge.badge-attention',
                        'Erro no envio'
                    );
            };
        };

        return (!_.isEmpty(collection) ? m('.section-one-column.u-marginbottom-30', [
            m('.fontsize-large.fontweight-semibold.u-marginbottom-30.u-text-center',
                title
            ),
            m('.w-row.w-hidden-small.w-hidden-tiny.card.card-secondary', [
                m('.w-col.w-col-3',
                    m('.fontsize-small.fontweight-semibold',
                        'Projetos que apoiei'
                    )
                ),
                m('.w-col.w-col-2',
                    m('.fontsize-small.fontweight-semibold',
                        'Valor do apoio'
                    )
                ),
                m('.w-col.w-col-3',
                    m('.fontsize-small.fontweight-semibold',
                        'Status do apoio'
                    )
                ),
                m('.w-col.w-col-4',
                    m('.fontsize-small.fontweight-semibold',
                        'Recompensa'
                    )
                )
            ]),

            _.map(collection, (contribution) => {
                return m('.w-row.card', [
                    m('.w-col.w-col-3',
                        m('.w-row', [
                            m('.w-col.w-col-4.u-marginbottom-10',
                                m(`a[href='/${contribution.permalink}']`,
                                    m(`img.thumb-project.u-radius[alt='${contribution.project_name}'][src='${contribution.project_image}'][width='50']`)
                                )
                            ),
                            m('.w-col.w-col-8',
                                m('.fontsize-small.fontweight-semibold',
                                    m(`a.alt-link[href='/${contribution.permalink}']`,
                                        contribution.project_name
                                    )
                                )
                            )
                        ])
                    ),
                    m('.w-col.w-col-2.u-marginbottom-10',
                        m('.fontsize-base.inline-block', [
                            m('span.w-hidden-main.w-hidden-medium.fontweight-semibold',
                                'Valor do apoio'
                            ),
                            ` R$ ${contribution.value}`
                        ])
                    ),
                    m('.w-col.w-col-3.u-marginbottom-10', [
                        m('.w-hidden-main.w-hidden-medium.fontsize-smallest.fontweight-semibold',
                            'Status'
                        ),

                        m('.fontsize-smaller.fontweight-semibold', [
                            m('.lineheight-tighter'),
                            m(`span.fa.fa-circle.fontsize-smallest.${contribution.state == 'paid' ? 'text-success' : contribution.state == 'pending' ? 'text-waiting' : 'text-error'}`,
                                m.trust('&nbsp;')
                            ),
                            I18n.t(contribution.state, I18nScope({
                                date: h.momentify(contribution[contribution.state + '_at'])
                            }))
                        ]),
                        m('.fontsize-smallest',
                            (contribution.installments > 1 ? (`${contribution.installments} x R$ ${contribution.installment_value} `) : ''),
                            (contribution.payment_method == 'BoletoBancario' ? 'Boleto Bancário' : 'Cartão de Crédito')
                        ),

                        (contributionVM.canShowReceipt(contribution) ?
                            m(`a.btn.btn-inline.btn-small.u-margintop-10.btn-terciary[href='https://www.catarse.me/pt/projects/${contribution.project_id}/contributions/${contribution.contribution_id}/receipt'][target='__blank']`,
                                'Ver recibo'
                            ) : ''),

                        (contributionVM.canShowSlip(contribution) ?
                            m(`a.btn.btn-inline.btn-small.u-margintop-10[href='${contribution.gateway_data['boleto_url']}'][target='__blank']`,
                                'Imprimir boleto'
                            ) : ''),

                        (contributionVM.canGenerateSlip(contribution) ?
                            m(`a.btn.btn-inline.btn-small.u-margintop-10[href='https://www.catarse.me/pt/projects/${contribution.project_id}/contributions/${contribution.contribution_id}/second_slip'][target='__blank']`,
                                'Gerar 2a via'
                            ) : ''),

                        m('.w-checkbox.fontsize-smallest.fontcolor-secondary.u-margintop-10', [
                            m(`input.w-checkbox-input[id='anonymous'][name='anonymous'][type='checkbox']${contribution.anonymous ? '[checked=\'checked\']' : ''}[value='1']`, {
                                onclick: () => ctrl.toggleAnonymous(contribution.project_id, contribution)
                            }),
                            m('label.w-form-label',
                                'Quero que meu apoio não seja público'
                            )
                        ])
                    ]),
                    m('.w-col.w-col-3',
                        m('.fontsize-smallest', [
                            m('span.w-hidden-main.w-hidden-medium.fontweight-semibold',
                                'Recompensa'
                            ),
                            (contribution.reward_id ? m.trust(h.simpleFormat(contribution.reward_description)) : ' Não selecionou recompensa')

                        ]),

                        m('.fontsize-smallest.lineheight-looser', [
                            m('span.fontweight-semibold',
                                'Estimativa de entrega: '
                            ),
                            h.momentify(contribution.deliver_at, 'MMMM/YYYY')
                        ]),
                        m('.fontsize-smallest', [
                            m('span.fontweight-semibold',
                                'Status da entrega:'
                            ),
                            m.trust('&nbsp;'),
                            statusText(contribution)
                        ])
                    ),
                    m('.u-text-center.w-col.w-col-1', {
                        onclick: () => ctrl.toggleDelivery(contribution.project_id, contribution)
                    }, [
                        m('.fontsize-smallest',
                            m(`a.checkbox-big${contribution.delivery_status == 'received' ? '.checkbox--selected.fa.fa-check.fa-lg' : ''}`,
                                ''
                            )
                        ),
                        m('.fontcolor-secondary.fontsize-smallest.lineheight-looser',
                            'Recebi!'
                        )
                    ])

                ]);
            }),
            m('.w-row.u-marginbottom-40.u-margintop-30', [
                m(loadMoreBtn, {
                    collection: pagination,
                    cssClass: '.w-col-push-5'
                })
            ])
        ]) : m('div', ''));
    }
};

export default userContributedBox;<commit_msg>only show received button to paid contributions<commit_after>import m from 'mithril';
import _ from 'underscore';
import h from '../h';
import contributionVM from '../vms/contribution-vm';
import userVM from '../vms/user-vm';
import loadMoreBtn from './load-more-btn';

const I18nScope = _.partial(h.i18nScope, 'payment.state');

const userContributedBox = {
    controller(args) {
        const toggleDelivery = (projectId, contribution) => {
                userVM.toggleDelivery(projectId, contribution).then(() => {
                    const lastStatus = contribution.reward_sent_at ? 'delivered' : 'undelivered';
                    contribution.delivery_status = contribution.delivery_status == 'received' ? lastStatus : 'received'; //so we don't have to reload the page
                });
            },
            canReceiveReward = (contribution) => (contribution.state == 'paid' && contribution.reward_id && contribution.project_state != 'failed');
        return {
            canReceiveReward: canReceiveReward,
            toggleAnonymous: userVM.toggleAnonymous,
            toggleDelivery: toggleDelivery
        };
    },
    view(ctrl, args) {
        let collection = args.collection,
            pagination = args.pagination,
            title = args.title;
        const statusText = (contribution) => {
            switch (contribution.delivery_status) {
                case 'delivered':
                    return m('span.fontsize-smallest.badge.badge-success',
                        'Enviada'
                    );
                case 'received':
                    return m('span.fontsize-smallest.badge.badge-success',
                        'Recebida'
                    );
                case 'undelivered':
                    return m('span.fontsize-smallest.badge.badge-light',
                        'Não enviada'
                    );
                case 'error':
                    return m('span.fontsize-smallest.badge.badge-attention',
                        'Erro no envio'
                    );
            };
        };

        return (!_.isEmpty(collection) ? m('.section-one-column.u-marginbottom-30', [
            m('.fontsize-large.fontweight-semibold.u-marginbottom-30.u-text-center',
                title
            ),
            m('.w-row.w-hidden-small.w-hidden-tiny.card.card-secondary', [
                m('.w-col.w-col-3',
                    m('.fontsize-small.fontweight-semibold',
                        'Projetos que apoiei'
                    )
                ),
                m('.w-col.w-col-2',
                    m('.fontsize-small.fontweight-semibold',
                        'Valor do apoio'
                    )
                ),
                m('.w-col.w-col-3',
                    m('.fontsize-small.fontweight-semibold',
                        'Status do apoio'
                    )
                ),
                m('.w-col.w-col-4',
                    m('.fontsize-small.fontweight-semibold',
                        'Recompensa'
                    )
                )
            ]),

            _.map(collection, (contribution) => {
                return m('.w-row.card', [
                    m('.w-col.w-col-3',
                        m('.w-row', [
                            m('.w-col.w-col-4.u-marginbottom-10',
                                m(`a[href='/${contribution.permalink}']`,
                                    m(`img.thumb-project.u-radius[alt='${contribution.project_name}'][src='${contribution.project_image}'][width='50']`)
                                )
                            ),
                            m('.w-col.w-col-8',
                                m('.fontsize-small.fontweight-semibold',
                                    m(`a.alt-link[href='/${contribution.permalink}']`,
                                        contribution.project_name
                                    )
                                )
                            )
                        ])
                    ),
                    m('.w-col.w-col-2.u-marginbottom-10',
                        m('.fontsize-base.inline-block', [
                            m('span.w-hidden-main.w-hidden-medium.fontweight-semibold',
                                'Valor do apoio'
                            ),
                            ` R$ ${contribution.value}`
                        ])
                    ),
                    m('.w-col.w-col-3.u-marginbottom-10', [
                        m('.w-hidden-main.w-hidden-medium.fontsize-smallest.fontweight-semibold',
                            'Status'
                        ),

                        m('.fontsize-smaller.fontweight-semibold', [
                            m('.lineheight-tighter'),
                            m(`span.fa.fa-circle.fontsize-smallest.${contribution.state == 'paid' ? 'text-success' : contribution.state == 'pending' ? 'text-waiting' : 'text-error'}`,
                                m.trust('&nbsp;')
                            ),
                            I18n.t(contribution.state, I18nScope({
                                date: h.momentify(contribution[contribution.state + '_at'])
                            }))
                        ]),
                        m('.fontsize-smallest',
                            (contribution.installments > 1 ? (`${contribution.installments} x R$ ${contribution.installment_value} `) : ''),
                            (contribution.payment_method == 'BoletoBancario' ? 'Boleto Bancário' : 'Cartão de Crédito')
                        ),

                        (contributionVM.canShowReceipt(contribution) ?
                            m(`a.btn.btn-inline.btn-small.u-margintop-10.btn-terciary[href='https://www.catarse.me/pt/projects/${contribution.project_id}/contributions/${contribution.contribution_id}/receipt'][target='__blank']`,
                                'Ver recibo'
                            ) : ''),

                        (contributionVM.canShowSlip(contribution) ?
                            m(`a.btn.btn-inline.btn-small.u-margintop-10[href='${contribution.gateway_data['boleto_url']}'][target='__blank']`,
                                'Imprimir boleto'
                            ) : ''),

                        (contributionVM.canGenerateSlip(contribution) ?
                            m(`a.btn.btn-inline.btn-small.u-margintop-10[href='https://www.catarse.me/pt/projects/${contribution.project_id}/contributions/${contribution.contribution_id}/second_slip'][target='__blank']`,
                                'Gerar 2a via'
                            ) : ''),

                        m('.w-checkbox.fontsize-smallest.fontcolor-secondary.u-margintop-10', [
                            m(`input.w-checkbox-input[id='anonymous'][name='anonymous'][type='checkbox']${contribution.anonymous ? '[checked=\'checked\']' : ''}[value='1']`, {
                                onclick: () => ctrl.toggleAnonymous(contribution.project_id, contribution)
                            }),
                            m('label.w-form-label',
                                'Quero que meu apoio não seja público'
                            )
                        ])
                    ]),
                    m('.w-col.w-col-3',
                        m('.fontsize-smallest', [
                            m('span.w-hidden-main.w-hidden-medium.fontweight-semibold',
                                'Recompensa'
                            ),
                            (contribution.reward_id ? m.trust(h.simpleFormat(contribution.reward_description)) : ' Não selecionou recompensa')

                        ]),

                        m('.fontsize-smallest.lineheight-looser', [
                            m('span.fontweight-semibold',
                                'Estimativa de entrega: '
                            ),
                            h.momentify(contribution.deliver_at, 'MMMM/YYYY')
                        ]),
                        m('.fontsize-smallest', [
                            m('span.fontweight-semibold',
                                'Status da entrega:'
                            ),
                            m.trust('&nbsp;'),
                            statusText(contribution)
                        ])
                    ),
                    (ctrl.canReceiveReward(contribution) ?
                        m('.u-text-center.w-col.w-col-1', {
                            onclick: () => ctrl.toggleDelivery(contribution.project_id, contribution)
                        }, [
                            m('.fontsize-smallest',
                                m(`a.checkbox-big${contribution.delivery_status == 'received' ? '.checkbox--selected.fa.fa-check.fa-lg' : ''}`,
                                    ''
                                )
                            ),
                            m('.fontcolor-secondary.fontsize-smallest.lineheight-looser',
                                'Recebi!'
                            )
                        ]) : '')

                ]);
            }),
            m('.w-row.u-marginbottom-40.u-margintop-30', [
                m(loadMoreBtn, {
                    collection: pagination,
                    cssClass: '.w-col-push-5'
                })
            ])
        ]) : m('div', ''));
    }
};

export default userContributedBox;
{'fraction_non_alphanumeric': 0.12720008638375985, 'fraction_numerical': 0.005075045891372422, 'mean_word_length': 1.0350472423643156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2987697', 'n_tokens_mistral': 5010, 'n_tokens_neox': 4612, 'n_words': 503}