Skip to content
Snippets Groups Projects
ManageStudentsTrigger.vue 2.5 KiB
Newer Older
<script>
import { DateTime } from "luxon";
import ManageStudentsDialog from "./ManageStudentsDialog.vue";
import documentationPartMixin from "../documentation/documentationPartMixin";
import { touchDocumentation } from "./participationStatus.graphql";
import mutateMixin from "aleksis.core/mixins/mutateMixin.js";

export default {
  name: "ManageStudentsTrigger",
  components: { ManageStudentsDialog },
  mixins: [documentationPartMixin, mutateMixin],
  data() {
    return {
      canOpenParticipation: false,
      timeout: null,
    };
  },
  props: {
    labelKey: {
      type: String,
      required: false,
      default: undefined,
    },
  },
  mounted() {
    const lessonStart = DateTime.fromISO(this.documentation.datetimeStart);
    const now = DateTime.now();
    this.canOpenParticipation = now >= lessonStart;

    if (!this.canOpenParticipation) {
      this.timeout = setTimeout(
        () => (this.canOpenParticipation = true),
        lessonStart.diff(now).toObject().milliseconds,
      );
    }
  },
  beforeDestroy() {
    if (this.timeout) {
      clearTimeout(this.timeout);
    }
  },
  methods: {
    touchDocumentation() {
      this.mutate(
        touchDocumentation,
        {
          documentationId: this.documentation.id,
        },
        (storedDocumentations, incoming) => {
          // ID may be different now
          return storedDocumentations.map((doc) =>
            doc.id === this.documentation.id
              ? Object.assign(doc, incoming, { oldId: doc.id })
              : doc,
          );
        },
      );
    },
  },
  computed: {
    showLabel() {
      return !!this.labelKey || !this.canOpenParticipation;
    },
    innerLabelKey() {
      if (this.documentation.futureNoticeParticipationStatus) {
        return "alsijil.coursebook.notes.future";
      }
      return this.labelKey;
    },
  },
  <manage-students-dialog
    v-bind="documentationPartProps"
    @update="() => null"
    :loading-indicator="loading"
    <template #activator="{ attrs, on }">
      <v-chip
        dense
        color="primary"
        outlined
        :disabled="!canOpenParticipation || loading"
        @click="touchDocumentation"
        <v-icon :left="showLabel">mdi-account-edit-outline</v-icon>
        <template v-if="showLabel">
          {{ $t(innerLabelKey) }}
      </v-chip>
    </template>
  </manage-students-dialog>
</template>

<style scoped></style>