<commit_before>/**
 * window.c.projectReport component
 * Render project report form
 *
 */
import m from 'mithril';
import models from '../models';
import h from '../h';
import postgrest from 'mithril-postgrest';

const projectReport = {
    controller(args) {
        let displayForm = h.toggleProp(false, true),
            sendSuccess = m.prop(false),
            submitDisabled = m.prop(false),
            user = h.getUser() || {},
            email = m.prop(user.email),
            details = m.prop(''),
            reason = m.prop(''),
            l = m.prop(false),
            checkLogin = () => {
                if (user) {
                    displayForm.toggle();
                } else {
                    window.location.href = '/login';
                }
            },
            sendReport = () => {
                submitDisabled(true);
                let loaderOpts = models.projectReport.postOptions({
                    email: email(),
                    details: details(),
                    reason: reason() ,
                    project_id: h.getCurrentProject().project_id
                });
                l = postgrest.loaderWithToken(loaderOpts);

                l.load().then(sendSuccess(true));
                submitDisabled(false);
                return false;
            };

        return {
            checkLogin: checkLogin,
            displayForm: displayForm,
            sendSuccess: sendSuccess,
            submitDisabled: submitDisabled,
            sendReport: sendReport,
            user: user,
            email: email,
            details: details,
            reason: reason
        };
    },

    view(ctrl, args) {
        const user = ctrl.user;
        return m('.card.card-terciary.u-radius',
                    [
                      m('.fontsize-small.u-marginbottom-20',
                        [
                          'Este projeto desrespeita',
                          m.trust('&nbsp;'),
                          m('a.alt-link[href=\'http://suporte.catarse.me/hc/pt-br/articles/202387638\'][target=\'_blank\']',
                            'nossas regras? '
                          )
                        ]
                      ),
                      ctrl.sendSuccess() ?
                       m('.w-form',
                        m('p',
                          'Obrigado! A sua denúncia foi recebida.'
                        )
                      ) :
                      [
                        m('.a.w-button.btn.btn-medium.btn-terciary.btn-inline[href=\'javascript:void(0);\']',{onclick: ctrl.checkLogin},
                        'Denunciar este projeto'
                      ),
                      ctrl.displayForm() ? m('#report-form.u-margintop-30',
                        m('.w-form',
                          m('form', {onsubmit: ctrl.sendReport},
                            [
                              m('.fontsize-small.fontweight-semibold.u-marginbottom-10',
                                'Por que você está denunciando este projeto?'
                              ),
                              m('select.w-select.text-field.positive[required=\'required\']', {onchange: m.withAttr('value', ctrl.reason)},
                                [
                                  m('option[value=\'\']',
                                    'Selecione um motivo'
                                  ),
                                  m('option[value=\'Violação de propriedade intelectual\']',
                                    'Violação de propriedade intelectual'
                                  ),
                                  m('option[value=\'Calúnia, injúria, difamação ou discriminação\']',<commit_msg>Fixes report login check<commit_after>/**
 * window.c.projectReport component
 * Render project report form
 *
 */
import m from 'mithril';
import postgrest from 'mithril-postgrest';
import models from '../models';
import h from '../h';
import projectVM from '../vms/project-vm';

const projectReport = {
    controller(args) {
        let displayForm = h.toggleProp(false, true),
            sendSuccess = m.prop(false),
            submitDisabled = m.prop(false),
            user = h.getUser() || {},
            email = m.prop(user.email),
            details = m.prop(''),
            reason = m.prop(''),
            l = m.prop(false),
            storeReport = 'report',
            project = projectVM.currentProject(),
            hasPendingAction = project && (h.callStoredAction(storeReport) == project.project_id),
            checkLogin = () => {
                if (!_.isEmpty(user)) {
                    displayForm.toggle();
                } else {
                    h.storeAction(storeReport, project.project_id);
                    return h.navigateToDevise();
                }
            },
            sendReport = () => {
                submitDisabled(true);
                let loaderOpts = models.projectReport.postOptions({
                    email: email(),
                    details: details(),
                    reason: reason() ,
                    project_id: project.project_id
                });
                l = postgrest.loaderWithToken(loaderOpts);

                l.load().then(sendSuccess(true));
                submitDisabled(false);
                return false;
            },
            checkScroll = (el, isInit) => {
                if (!isInit && hasPendingAction) {
                    h.animateScrollTo(el);
                }
            };


        if (!_.isEmpty(user) && hasPendingAction) {
            displayForm(true);
        }

        return {
            checkScroll: checkScroll,
            checkLogin: checkLogin,
            displayForm: displayForm,
            sendSuccess: sendSuccess,
            submitDisabled: submitDisabled,
            sendReport: sendReport,
            user: user,
            email: email,
            details: details,
            reason: reason
        };
    },

    view(ctrl, args) {
        const user = ctrl.user;

        return m('.card.card-terciary.u-radius',
                    [
                      m('.fontsize-small.u-marginbottom-20',
                        [
                          'Este projeto desrespeita',
                          m.trust('&nbsp;'),
                          m('a.alt-link[href=\'http://suporte.catarse.me/hc/pt-br/articles/202387638\'][target=\'_blank\']',
                            'nossas regras? '
                          )
                        ]
                      ),
                      ctrl.sendSuccess() ?
                       m('.w-form',
                        m('p',
                          'Obrigado! A sua denúncia foi recebida.'
                        )
                      ) :
                      [
                        m('.a.w-button.btn.btn-medium.btn-terciary.btn-inline[href=\'javascript:void(0);\']',{onclick: ctrl.checkLogin},
                        'Denunciar este projeto'
                      ),
                      ctrl.displayForm() ? m('#report-form.u-margintop-30',
                        m('.w-form',
                          m('form', {onsubmit: ctrl.sendReport, config: ctrl.checkScroll},
                            [
                              m('.fontsize-small.fontweight-semibold.u-marginbottom-10',
                                'Por que você está denunciando este projeto?'
                              ),
                              m('select.w-select.text-field.positive[required=\'required\']', {onchange: m.withAttr('value', ctrl.reason)},
                                [
                                  m('option[value=\'\']',
                                    'Selecione um motivo'
                                  ),
                                  m('option[value=\'Violação de propriedade intelectual\']',
                                    'Violação de propriedade intelectual'
                                  ),
                                  m('option[value=\'Calúnia, injúria, difamação ou discriminação\']',
{'fraction_non_alphanumeric': 0.11608684924747101, 'fraction_numerical': 0.003947693066864051, 'mean_word_length': 1.0415512465373962, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6165698', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1944, 'n_words': 337}