Skip to content
Snippets Groups Projects
Project_edit.vue 12.7 KiB
Newer Older
<template>
  <div class="fourteen wide column">
    <form id="form-project-edit" class="ui form">
      <!--      <div v-if="errors" class="ui error message"> // todo: spécifier les erreurs possibles à afficher
        {{ form.non_field_errors }}

        <span v-for="hidden_field in form.hidden_fields" :key="hidden_field">
          {{ hidden_field.errors }}
          {{ hidden_field }}
        </span>
      </div> -->
      <!--  <i @click="goBack" class="close icon"></i> -->

      <h1>
        <span v-if="action === 'update'"
          >Édition du projet "{{ project2edit.title }}"</span
        ><!-- // todo : [pour UPDATE] récupérer project à éditer -->
        <span v-else-if="action === 'create'">Création d'un projet</span>
      </h1>

      <div class="ui horizontal divider">INFORMATIONS</div>

      <div class="two fields">
        <div class="required field">
          <label for="title">Titre</label>
          <!-- <small>{{ form.title.help_text }}</small
          > --><!-- | safe  // ? utile ?  -->
          <input
            type="text"
            required
            maxlength="128"
            name="title"
            id="title"
            v-model="project2edit.title"
          <!-- {{ form.title.errors }} // ? des erreurs possibles ? -->
        </div>
        <div class="field">
          <label>Illustration du projet</label>
          <img
            v-if="project2edit.thumbnail"
            class="ui small image"
            id="form-input-file-logo"
            :src="project2edit.thumbnail"
            v-if="instance_thumbnail_url"
            class="ui small image"
            id="form-input-file-logo"
            :src="instance_thumbnail_url"
          <label
            @click.prevent="selectImg"
            class="ui icon button"
            ><!-- // todo : send image to the backend and display it after -->
            <i class="file icon"></i>
              project2edit.thumbnail
                ? [...project2edit.thumbnail.split("/")].pop()
                : "Sélectionner une image ..."
            <!-- <span class="label">{{
              thumbnail_value ? thumbnail_value : "Sélectionner une image ..."
            }}</span> -->
          </label>
          <input
            @change="processImgData"
            class="file-selection"
            type="file"
            accept="image/jpeg, image/png"
            style="display: none"
          <!-- {{ form.thumbnail.errors }} -->
        </div>
      </div>
      <div class="field">
        <label for="description">Description</label>
          v-model="project2edit.description"
          name="description"
        <!-- {{ form.description.errors }} -->
      </div>

      <div class="ui horizontal divider">PARAMÈTRES</div>

      <div class="four fields">
        <div class="field">
          <label for="archive_feature">Délai avant archivage</label>
          <div class="ui right labeled input">
            <input
              type="number"
              min="0"
              style="padding: 1px 2px"
              name="archive_feature"
              id="archive_feature"
              v-model="project2edit.archive_feature"
            />
            <div class="ui label">jour(s)</div>
          </div>
          <!-- {{ form.archive_feature.errors }} -->
        </div>
        <div class="field">
          <label for="delete_feature">Délai avant suppression</label>
          <div class="ui right labeled input">
            <input
              type="number"
              min="0"
              style="padding: 1px 2px"
              name="delete_feature"
              id="delete_feature"
              v-model="project2edit.delete_feature"
            />
            <div class="ui label">jour(s)</div>
          </div>
          <!-- {{ form.delete_feature.errors }} -->
        </div>
        <div class="required field">
          <label for="access_level_pub_feature"
            >Visibilité des signalements publiés</label
          >
          <!--           <div class="ui selection dropdown">
            <input
              type="hidden"
              :name="form.access_level_pub_feature.html_name"
              :id="form.access_level_pub_feature.id_for_label"
              v-model="form.access_level_pub_feature.value"
            />
            <div class="default text"></div>
            <i class="dropdown icon"></i>
            <div class="menu">
              <div
                class="item"
                v-for="(x, y) in form.access_level_pub_feature.field.choices"
                :key="y"
                :data-value="
                  x +
                  (form.access_level_pub_feature.value === x ? selected : '')
              </div>
            </div>
          </div> -->
          <Dropdown
            :options="form.access_level_pub_feature.field.choices"
            :selected="project2edit.access_level_pub_feature"
            :selection.sync="project2edit.access_level_pub_feature"
            :text="access_level_pub_feature_selected"
            :options="optionsAccessPub"
            selection
            v-model="access_level_pub_feature_selected"
          >
          <!-- {{ form.access_level_pub_feature.errors }} -->
        </div>
        <div class="required field">
          <label for="access_level_arch_feature">
            Visibilité des signalements archivés
          </label>
          <Dropdown
            :options="form.access_level_arch_feature.field.choices"
            :selected="project2edit.access_level_arch_feature"
            :selection.sync="project2edit.access_level_arch_feature"
          />
          <!-- <div class="ui selection dropdown">
            <input
              type="hidden"
              name="access_level_arch_feature"
              id="access_level_arch_feature"
              v-model="project2edit.access_level_arch_feature"
            />
            <div class="default text"></div>
            <i class="dropdown icon"></i>
            <div class="menu">
              {% for x,y in form.access_level_arch_feature.field.choices %}
               <div class="item" data-value="{{ x }}" {% if form.access_level_arch_feature.value == x %} selected{% endif %}>{{ y }}</div>
              {% endfor %}
            </div>
          </div> -->
          <!--  {{ form.access_level_arch_feature.errors }} -->
        </div>
      </div>

      <div class="field">
        <div class="ui checkbox">
          <input
            type="checkbox"
            :checked="project2edit.moderation"
            name="moderation"
            id="moderation"
          <label for="moderation">Modération</label>
        </div>
        <!-- {{ form.moderation.errors }} -->
      </div>

      <div class="field">
        <div class="ui checkbox">
          <input
            type="checkbox"
            :checked="project2edit.is_project_type"
            name="is_project_type"
            id="is_project_type"
          <label for="is_project_type">Est un projet type</label>
        </div>
        <!-- {{ form.is_project_type.errors }} -->
      </div>

      <div class="ui divider"></div>

      <!-- {% for hidden_field in form.hidden_fields %} -->
      <!-- pour passer le slug du projet parent entre GET et POST // ? marche en span ? -->
      <!-- <span v-for="hidden_field in form.hidden_fields" :key="hidden_field">{{
        hidden_field
      }}</span> -->
      <button @click.prevent="postForm" class="ui teal icon button">
        <i class="white save icon"></i> Enregistrer les changements
      </button>
    </form>
  </div>
</template>

<script>
import Dropdown from "@/components/dropdown.vue";

export default {
  name: "Project_edit",

  data() {
    return {
      project2edit: {
        title: "Vuetification (Copie-23/07/2021 09:19)",
        slug: "6-vuetification-copie-23072021-0919",
        created_on: "23/07/2021",
        updated_on: "29/07/2021",
        description:
          "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
        moderation: true,
        thumbnail: "http://localhost:8000/media/user_1/albinoscom.jpg",
        creator: 1,
        access_level_pub_feature: "Utilisateur anonyme",
        access_level_arch_feature: "Utilisateur anonyme",
        archive_feature: 0,
        delete_feature: 0,
        nb_features: 2,
        nb_published_features: 1,
        nb_comments: 0,
        nb_published_features_comments: 0,
        nb_contributors: 2,
        is_project_type: false,
      },
      // * utile pour front
      thumbnail_value: null,
      instance_thumbnail_url: null,
      access_level_pub_feature_selected: null,
      // * structure de formulaire propre à django, trop complexe pour réactivité dans vue.js
      form: {
        access_level_pub_feature: {
          id_for_label: null,
          field: {
            choices: [
              "Utilisateur anonyme",
              "Utilisateur connecté",
              "Contributeur",
            ],
          },
        },
        access_level_arch_feature: {
          id_for_label: null,
          field: {
            choices: [
              "Utilisateur anonyme",
              "Utilisateur connecté",
              "Contributeur",
            ],
          },
        },
        archive_feature: {
          thumbnail: {
            label: {
              url: null,
            },
          },
        },
        delete_feature: {
          thumbnail: {
            label: {
              url: null,
            },
          },
        },
        instance: {
          title: null,
          thumbnail: {
            label: {
              url: this.instance_thumbnail_url,
            },
          },
        },
        is_project_type: {
          html_name: null,
        },
        description: {
          id_for_label: null,
        },
        moderation: {
          html_name: null,
        },
        title: {
          id_for_label: null,
          help_text: null,
          label: null,
          field: {
            max_length: null,
          },
        },
        thumbnail: {
          label: "Illustration du projet",
          url: null,
          value: this.thumbnail_value,
        },
      },
      errors: null,

  computed: {
    optionsAccessPub() {
      return this.form.access_level_pub_feature.field.choices.map((choice) => {
        return { text: choice, name: choice };
      });
    },
    optionsAccessArchive() {
      return this.form.access_level_pub_feature.field.choices.map((choice) => {
        return { text: choice, name: choice };
      });
    },
  },

  methods: {
    truncate(n, len) {
      var ext = n.substring(n.lastIndexOf(".") + 1, n.length).toLowerCase();
      var filename = n.replace("." + ext, "");
      if (filename.length <= len) {
        return n;
      }
      filename = filename.substr(0, len) + (n.length > len ? "[...]" : "");
      return filename + "." + ext;
    },
    processImgData(e) {
      // * read image file
      const file = e.target.files[0];
      if (file) {
        this.thumbnail_value = file.name;
        // this.thumbnail.value = file.name;
      }

      let reader = new FileReader();
      let _this = this;
      reader.onload = function (e) {
        //_this.form.instance.thumbnail.url = e.target.result;
        _this.instance_thumbnail_url = e.target.result;
      };

      reader.readAsDataURL(file);
      // todo : send file to the back (?) (not in base64)
    },
    selectImg() {
      // * call click on hidden input field
      document.getElementsByClassName("file-selection")[0].click();
    },

    goBack() {
      console.log(this.$router);
      const routerHistory = this.$router.options.routerHistory;
      this.$router.push(routerHistory[routerHistory.length - 1] || "/");
      const data = JSON.stringify(this.project2edit);
      console.log("TEST : ", data)
    }
</script>

<style media="screen">
#form-input-file-logo {
  margin-left: auto;
  margin-right: auto;
}