<commit_before>/*
 This file is a part of libertysoil.org website
 Copyright (C) 2016  Loki Education (Social Enterprise)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import React from 'react';
import _ from 'lodash';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { browserHistory } from 'react-router';
import Helmet from 'react-helmet';

import {
  Page,
  PageMain,
  PageBody,
  PageContent
} from '../components/page';
import NotFound from './not-found';
import HeaderLogo from '../components/header-logo';
import Breadcrumbs from '../components/breadcrumbs/breadcrumbs';
import Header from '../components/header';
import Footer from '../components/footer';
import { API_HOST } from '../config';
import ApiClient from '../api/client';
import { URL_NAMES, getUrl } from '../utils/urlGenerator';
import Sidebar from '../components/sidebar';
import SidebarAlt from '../components/sidebarAlt';
import EditPost from '../components/edit-post';
import AddedTags from '../components/post/added-tags';

import { defaultSelector } from '../selectors';
import { ActionsTrigger } from '../triggers';
import {
  addPost,
  resetEditPostForm,
  updateEditPostForm
} from '../actions/posts';


class PostEditPage extends React.Component {
  static displayName = 'PostEditPage';

  static async fetchData(params, store, client) {
    const noSchoolsLoaded = store.getState().get('schools').isEmpty();
    const trigger = new ActionsTrigger(client, store.dispatch);
    let schoolsPromise;

    if (noSchoolsLoaded) {
      schoolsPromise = trigger.loadSchools();
    }

    try {
      const post = await client.postInfo(params.uuid);
      store.dispatch(addPost(post));
    } catch (e) {
      store.dispatch(addPost({ error: true, id: params.uuid, user: {} }));

      return 404;
    }

    if (noSchoolsLoaded) {
      await schoolsPromise;
    }

    await trigger.loadUserRecentTags();

    return 200;
  }

  _handleSubmit = () => {
    browserHistory.push(getUrl(URL_NAMES.POST, { uuid: this.props.params.uuid }));
  };

  _handleDelete = () => {
    browserHistory.push('/');
  };

  render() {
    const {
      current_user
    } = this.props;
    const postId = this.props.params.uuid;

    if (!(postId in this.props.posts)) {
      // not loaded yet
      return null;<commit_msg>EditPost: make top anchor to be initial scroll state<commit_after>/*
 This file is a part of libertysoil.org website
 Copyright (C) 2016  Loki Education (Social Enterprise)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import React from 'react';
import _ from 'lodash';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { browserHistory } from 'react-router';
import Helmet from 'react-helmet';

import {
  Page,
  PageMain,
  PageBody,
  PageContent
} from '../components/page';
import NotFound from './not-found';
import HeaderLogo from '../components/header-logo';
import Breadcrumbs from '../components/breadcrumbs/breadcrumbs';
import Header from '../components/header';
import Footer from '../components/footer';
import { API_HOST } from '../config';
import ApiClient from '../api/client';
import { URL_NAMES, getUrl } from '../utils/urlGenerator';
import Sidebar from '../components/sidebar';
import SidebarAlt from '../components/sidebarAlt';
import EditPost from '../components/edit-post';
import AddedTags from '../components/post/added-tags';

import { defaultSelector } from '../selectors';
import { ActionsTrigger } from '../triggers';
import {
  addPost,
  resetEditPostForm,
  updateEditPostForm
} from '../actions/posts';


class PostEditPage extends React.Component {
  static displayName = 'PostEditPage';

  static async fetchData(params, store, client) {
    const noSchoolsLoaded = store.getState().get('schools').isEmpty();
    const trigger = new ActionsTrigger(client, store.dispatch);
    let schoolsPromise;

    if (noSchoolsLoaded) {
      schoolsPromise = trigger.loadSchools();
    }

    try {
      const post = await client.postInfo(params.uuid);
      store.dispatch(addPost(post));
    } catch (e) {
      store.dispatch(addPost({ error: true, id: params.uuid, user: {} }));

      return 404;
    }

    if (noSchoolsLoaded) {
      await schoolsPromise;
    }

    await trigger.loadUserRecentTags();

    return 200;
  }

  componentDidMount() {
    if (window) {
      // make top anchor to be default
      window.scrollTo(0, 0);
    }
  }

  _handleSubmit = () => {
    browserHistory.push(getUrl(URL_NAMES.POST, { uuid: this.props.params.uuid }));
  };

  _handleDelete = () => {
    browserHistory.push('/');
  };

  render() {
    const {
      current_user
    } = this.props;
    const postId = this.props.params.uuid;

    if (!(postId in this.props.posts)) {
      // not loaded yet
      return null;
{'fraction_non_alphanumeric': 0.1155762205247426, 'fraction_numerical': 0.003985386914646297, 'mean_word_length': 3.5251690458302027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5966422', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1665, 'n_words': 584}