Skip to content
Snippets Groups Projects
AddSolutionForm.js 8.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • import './Forms.css';
    
    
    Rafael László's avatar
    Rafael László committed
    import {
      Button,
    
      Dimmer,
      Divider,
    
    Rafael László's avatar
    Rafael László committed
      Form,
      Header,
    
    Rafael László's avatar
    Rafael László committed
      Loader,
    
      Modal,
      Segment,
      TextArea,
    
    Rafael László's avatar
    Rafael László committed
    } from 'semantic-ui-react';
    
    import React, { Component } from 'react';
    
    Rafael László's avatar
    Rafael László committed
      addDocument,
    
      addSolution,
    
    Rafael László's avatar
    Rafael László committed
      clearWrite,
    
      getDocuments,
      getSolutions,
    
      writeSolution,
      writeSolutionFile,
    
    } from '../../actions/homework';
    
    Rafael László's avatar
    Rafael László committed
    import ConfirmModal from './ConfirmModal';
    
    import { connect } from 'react-redux';
    
    Rafael László's avatar
    Rafael László committed
    import { customMessage } from '../pages/Homework';
    
    
    const allowedFileTypes = [
      'image/jpeg',
      'image/png',
    
      'application/zip',
      'application/x-zip',
    
    const maxFileSize = 50;
    
    
    class AddSolutionForm extends Component {
      constructor(props) {
        super(props);
        this.state = {
          showModal: false,
    
    Rafael László's avatar
    Rafael László committed
          loading: false,
    
        };
      }
    
      render() {
    
    Rafael László's avatar
    Rafael László committed
        const { name, description, file } = this.props.newSolution;
    
        const task = this.props.taskid;
    
    Rafael László's avatar
    Rafael László committed
        const thisTaskSolution = this.props.homeworks.solutions.filter(
          (solution) => solution.task === task
        );
        const thisTaskDocument = this.props.homeworks.documents.filter(
          (document) => document.solution === thisTaskSolution[0]?.id
        );
    
    Rafael László's avatar
    Rafael László committed
        const lastName = thisTaskDocument[0]?.name;
        const lastDesc = thisTaskDocument[0]?.description;
        const lastFile = thisTaskDocument[0]?.file_url;
    
        const corrected = false;
        const accepted = false;
    
        const sentences = this.props.taskdesc.split('\n');
    
        const note = '';
    
        const disabledText = 'A határidő lejárt, további beadás nem lehetséges.';
    
        return (
          <Modal
            open={this.state.showModal}
    
            closeOnDimmerClick
            onClose={() => this.setState({ showModal: false })}
    
    Rafael László's avatar
    Rafael László committed
            trigger={
    
    Rafael László's avatar
    Rafael László committed
                type="button"
                id="task"
    
    Rafael László's avatar
    Rafael László committed
                onClick={() => {
    
                  this.setState({ showModal: true });
                }}
    
    Rafael László's avatar
    Rafael László committed
                <Icon name="external" />
    
                {this.props.tasktitle}
    
    Rafael László's avatar
    Rafael László committed
            }
    
    Rafael László's avatar
    Rafael László committed
              {this.props.multiple ? 'Másik megoldás' : 'Megoldás'} beadása a(z)
              {this.props.tasktitle} nevű feladathoz:
    
            <Modal.Content>
    
              <Modal.Description style={{ marginBottom: '2em' }}>
    
    Rafael László's avatar
    Rafael László committed
                <Header as="h5">Feladat leírása:</Header>
                {sentences.map((s) => (
                  <p key={Math.random()}>{s}</p>
                ))}
    
    Rafael László's avatar
    Rafael László committed
              {this.props.disabled ? (
                <div>
                  {lastName ? (
                    <div style={{ paddingBottom: '1em' }}>
                      <div style={{ marginBottom: '1em', fontWeight: 'bold' }}>
                        Legutóbbi megoldásod:
                      </div>
                      <Segment attached="top">
                        <h5 style={{ paddingBottom: '0.4em' }}>Cím:</h5>
                        {lastName}
                      </Segment>
                      <Segment attached>
                        <h5 style={{ paddingBottom: '0.4em' }}>Leírás:</h5>
                        {lastDesc}
                      </Segment>
                      <Segment attached="bottom">
                        <h5>Beadott fájl:</h5>
                        {lastFile ? (
                          <a href={lastFile} rel="noreferrer noopener" download>
                            Fájl letöltése
                          </a>
                        ) : (
                          <span>-</span>
                        )}
                      </Segment>
                    </div>
                  ) : (
                    customMessage(
                      disabledText,
                      undefined,
                      undefined,
                      this.props.disabled
                    )
                  )}
                </div>
              ) : (
                <Form>
                  {lastName ? (
                    <div style={{ paddingBottom: '1em' }}>
                      <div style={{ fontWeight: 'bold' }}>
                        Legutóbbi megoldásod:
                      </div>
                      <Segment attached="top">
                        <h5 style={{ paddingBottom: '0.4em' }}>Cím:</h5>
                        {lastName}
                      </Segment>
                      <Segment attached>
                        <h5 style={{ paddingBottom: '0.4em' }}>Leírás:</h5>
                        {lastDesc}
                      </Segment>
                      <Segment attached="bottom">
                        <h5>Beadott fájl:</h5>
                        {lastFile ? (
                          <a href={lastFile} rel="noreferrer noopener" download>
                            Fájl letöltése
                          </a>
                        ) : (
                          <span>-</span>
                        )}
                      </Segment>
                    </div>
                  ) : null}
                  <Divider />
                  <Form.Field
                    control={Input}
                    label="Megoldás címe:"
                    name="name"
                    onChange={(e) => this.props.writeSolution(e)}
                    value={name}
                    placeholder="Adj meg egy címet a beadandó megoldásodnak..."
                  />
                  <Form.Field
                    control={TextArea}
                    label="Megoldás leírása:"
                    name="description"
                    onChange={(e) => this.props.writeSolution(e)}
                    value={description}
                    placeholder="Add meg a megoldás leírását..."
                  />
                  <Form.Field>
                    <label>
                      Fájl (Megengedett fájltípusok: png, jpeg, jpg, zip. Maximum 50
                      MB.):
                    </label>
                    <Input
                      type="file"
    
                      accept={allowedFileTypes.join(',')}
    
    Rafael László's avatar
    Rafael László committed
                      onChange={(e) => this.props.writeSolutionFile(e)}
    
    Rafael László's avatar
    Rafael László committed
                    />
    
    Rafael László's avatar
    Rafael László committed
                  </Form.Field>
                </Form>
              )}
    
            </Modal.Content>
            <Modal.Actions>
              <Button
                inverted
    
    Rafael László's avatar
    Rafael László committed
                color="red"
    
                onClick={() => {
                  this.setState({ showModal: false });
    
    Rafael László's avatar
    Rafael László committed
                <Icon name="remove" /> Mégse
    
              </Button>
    
    Rafael László's avatar
    Rafael László committed
              {this.props.multiple ? (
                <ConfirmModal
                  button={
                    <Button
                      disabled={
                        !name ||
                        !description ||
                        (!file
                          ? false
                          : !allowedFileTypes.includes(file.type) ||
                            file.size > maxFileSize * 1024 ** 2)
                      }
                      inverted
                      color="green"
                    >
                      <Icon name="checkmark" />
                      Beadás
                      {this.state.loading ? (
                        <Dimmer active>
                          <Loader size="massive" />
                        </Dimmer>
                      ) : null}
                    </Button>
                  }
                  text="beadod az új megoldást, ami felülírja az előzőt"
                  onAccept={() => {
                    this.setState({
                      loading: true,
                    });
                    this.props
                      .addSolution({
                        task,
                        accepted,
                        corrected,
                        note,
                        name,
                        description,
                        file,
                      })
                      .then(
                        () => {
                          this.setState({
                            loading: false,
                            showModal: false,
                          });
                          this.props.clearWrite();
                        },
                        () => {
                          this.setState({
                            loading: false,
                          });
                          alert('Sikertelen feltöltés!');
    
    Rafael László's avatar
    Rafael László committed
                      );
                  }}
                />
              ) : (
                <Button
                  inverted
                  color="green"
                  disabled={
                    !name ||
                    !description ||
                    (!file
                      ? false
                      : !allowedFileTypes.includes(file.type) ||
                        file.size > maxFileSize * 1024 ** 2)
                  }
                  onClick={() => {
                    this.props.addSolution({
                      task,
                      accepted,
                      corrected,
                      note,
                      name,
                      description,
                      file,
                    });
                    this.setState({ showModal: false });
                    this.props.clearWrite();
                  }}
                >
                  <Icon name="checkmark" /> Beadás
                </Button>
              )}
    
            </Modal.Actions>
          </Modal>
        );
      }
    }
    
    
    Rafael László's avatar
    Rafael László committed
    const mapStateToProps = ({ newSolution, homeworks, user }) => ({
      newSolution,
      homeworks,
      user,
    });
    
    
    export default connect(mapStateToProps, {
      addSolution,
      writeSolution,
      writeSolutionFile,
      addDocument,
    
      getDocuments,
    
      getSolutions,
    
    })(AddSolutionForm);