<commit_before>import React, { useState } from 'react'
import { Upload, Icon, Alert } from 'antd'
import { Route } from 'react-router-dom'
import {isEqual} from 'lodash'
import { diff } from 'deep-object-diff'
import { useTranslation } from 'react-i18next'
import { config } from '../../../../utils/api'

const Uploader = ({ document, documentId, onNewDocumentUploading, onNewDocumentUploaded, onDocumentUpdated }) => {
  const { t } = useTranslation()
  const [error, setError] = useState('')
  const beforeUpload = (file) => {
    const isLt10M = file.size / 1000000 < 10
    if (!isLt10M) {
      setError(t('The uploaded file is too big'))
    } else {
      setError('')
    }
    return isLt10M
  }
  return (
    <Route
    path="/projects/:id"
    component={({ match: {params} }) => {
        let uploadStarted = false
        const data = documentId
          ? {field_id: `rsr_projectdocument.document.${documentId}`}
          : {field_id: `rsr_projectdocument.document.${params.id}_new-0`}
        return (
          <div>
          {error && <Alert type="error" message={error} style={{ marginBottom: 15 }} />}
          <Upload.Dragger
            name="file"
            listType="picture"
            action={`/rest/v1/project/${params.id}/upload_file/?format=json`}
            withCredentials
            headers={config.headers}
            data={data}
            defaultFileList={(document && document !== true) ? [{ uid: '-1', thumbUrl: document, url: document, status: 'done', name: document.split('/').reduce((acc, value) => value) }] : []}
            beforeUpload={beforeUpload}
            onChange={({ file }) => {
              if(file.status === 'uploading' && !documentId && !uploadStarted){
                uploadStarted = true
                if(onNewDocumentUploading) onNewDocumentUploading()
              }
            }}
            onSuccess={(e) => {
              if(!documentId){
                if(e.rel_objects && e.rel_objects[0] && e.rel_objects[0].new_id){
                  if(onNewDocumentUploaded) onNewDocumentUploaded(e.rel_objects[0].new_id, e.changes[0][1])
                }<commit_msg>[#3920] Use /rest/v1/project_document end-point to upload files<commit_after>import React, { useState } from 'react'
import { Upload, Icon, Alert } from 'antd'
import { Route } from 'react-router-dom'
import {isEqual} from 'lodash'
import { diff } from 'deep-object-diff'
import { useTranslation } from 'react-i18next'
import { config } from '../../../../utils/api'

const Uploader = ({ document, documentId, onNewDocumentUploading, onNewDocumentUploaded, onDocumentUpdated }) => {
  const { t } = useTranslation()
  const [error, setError] = useState('')
  const beforeUpload = (file) => {
    const isLt10M = file.size / 1000000 < 10
    if (!isLt10M) {
      setError(t('The uploaded file is too big'))
    } else {
      setError('')
    }
    return isLt10M
  }
  return (
    <Route
    path="/projects/:id"
    component={({ match: {params} }) => {
        let uploadStarted = false
        return (
          <div>
          {error && <Alert type="error" message={error} style={{ marginBottom: 15 }} />}
          <Upload.Dragger
            name="document"
            listType="picture"
            method="PATCH"
            action={`/rest/v1/project_document/${documentId}/?format=json`}
            withCredentials
            headers={config.headers}
            defaultFileList={(document && document !== true) ? [{ uid: '-1', thumbUrl: document, url: document, status: 'done', name: document.split('/').reduce((acc, value) => value) }] : []}
            beforeUpload={beforeUpload}
            onChange={({ file }) => {
              if(file.status === 'uploading' && !documentId && !uploadStarted){
                uploadStarted = true
                if(onNewDocumentUploading) onNewDocumentUploading()
              }
            }}
            onSuccess={(e) => {
              if(!documentId){
                if(e.rel_objects && e.rel_objects[0] && e.rel_objects[0].new_id){
                  if(onNewDocumentUploaded) onNewDocumentUploaded(e.rel_objects[0].new_id, e.changes[0][1])
                }
{'fraction_non_alphanumeric': 0.15225744476464936, 'fraction_numerical': 0.013928914505283382, 'mean_word_length': 2.3944580277098613, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '4624610', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1232, 'n_words': 261}