From 99792e8610942d952dfe097069f4adb8af17ad86 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 15 Jun 2021 17:34:12 +0200 Subject: [PATCH 01/57] WIP: migrate machine card to react --- .../components/machines/machine-card.tsx | 16 +++++++++++ .../javascript/controllers/machines.js.erb | 4 +-- app/frontend/src/javascript/models/machine.ts | 28 +++++++++++++++++++ .../machines/training_reservation_modal.html | 2 +- app/models/user.rb | 10 +++++-- app/views/api/machines/index.json.jbuilder | 6 ++-- app/views/api/machines/show.json.jbuilder | 10 +++---- 7 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 app/frontend/src/javascript/components/machines/machine-card.tsx create mode 100644 app/frontend/src/javascript/models/machine.ts diff --git a/app/frontend/src/javascript/components/machines/machine-card.tsx b/app/frontend/src/javascript/components/machines/machine-card.tsx new file mode 100644 index 000000000..411c05b85 --- /dev/null +++ b/app/frontend/src/javascript/components/machines/machine-card.tsx @@ -0,0 +1,16 @@ +import React from 'react'; +import { Machine } from '../../models/machine'; + +interface MachineCardProps { + machine: Machine +} + +/** + * This component is a box showing the picture of the given machine and two buttons: one to start the reservation process + * and another to redirect the user to the machine description page. + */ +const MachineCard: React.FC = ({ machine }) => { + return ( +
+ ) +} diff --git a/app/frontend/src/javascript/controllers/machines.js.erb b/app/frontend/src/javascript/controllers/machines.js.erb index 068c7688f..f01cc0af9 100644 --- a/app/frontend/src/javascript/controllers/machines.js.erb +++ b/app/frontend/src/javascript/controllers/machines.js.erb @@ -112,14 +112,14 @@ const _reserveMachine = function (machine, e) { return machine = _this.Machine.get({ id: machine.id }, function () { // if the currently logged'in user has completed the training for this machine, or this machine does not require // a prior training, just redirect him to the machine's booking page - if (machine.current_user_is_training || (machine.trainings.length === 0)) { + if (machine.current_user_is_trained || (machine.trainings.length === 0)) { return _this.$state.go('app.logged.machines_reserve', { id: machine.slug }); } else { // otherwise, if a user is authenticated ... if (_this.$scope.isAuthenticated()) { // ... and have booked a training for this machine, tell him that he must wait for an admin to validate // the training before he can book the reservation - if (machine.current_user_training_reservation) { + if (machine.current_user_next_training_reservation) { return _this.$uibModal.open({ templateUrl: '/machines/training_reservation_modal.html', controller: ['$scope', '$uibModalInstance', function ($scope, $uibModalInstance) { diff --git a/app/frontend/src/javascript/models/machine.ts b/app/frontend/src/javascript/models/machine.ts new file mode 100644 index 000000000..18d13aad9 --- /dev/null +++ b/app/frontend/src/javascript/models/machine.ts @@ -0,0 +1,28 @@ +import { Reservation } from './reservation'; + +export interface Machine { + id: number, + name: string, + description?: string, + spec?: string, + disabled: boolean, + slug: string, + machine_image: string, + machine_files_attributes?: Array<{ + id: number, + attachment: string, + attachment_url: string + }>, + trainings?: Array<{ + id: number, + name: string, + disabled: boolean, + }>, + current_user_is_trained?: boolean, + current_user_next_training_reservation?: Reservation, + machine_projects?: Array<{ + id: number, + name: string, + slug: string, + }> +} diff --git a/app/frontend/templates/machines/training_reservation_modal.html b/app/frontend/templates/machines/training_reservation_modal.html index 6b986a511..baf549ad7 100644 --- a/app/frontend/templates/machines/training_reservation_modal.html +++ b/app/frontend/templates/machines/training_reservation_modal.html @@ -3,7 +3,7 @@