1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

(ui) refactor space form

This commit is contained in:
Sylvain 2022-11-08 16:20:34 +01:00
parent 3d88266fe6
commit 8df60a8712
16 changed files with 228 additions and 265 deletions

View File

@ -1,6 +1,7 @@
import apiClient from './clients/api-client';
import { AxiosResponse } from 'axios';
import { Space } from '../models/space';
import ApiLib from '../lib/api';
export default class SpaceAPI {
static async index (): Promise<Array<Space>> {
@ -12,4 +13,24 @@ export default class SpaceAPI {
const res: AxiosResponse<Space> = await apiClient.get(`/api/spaces/${id}`);
return res?.data;
}
static async create (space: Space): Promise<Space> {
const data = ApiLib.serializeAttachments(space, 'space', ['space_files_attributes', 'space_image_attributes']);
const res: AxiosResponse<Space> = await apiClient.post('/api/spaces', data, {
headers: {
'Content-Type': 'multipart/form-data'
}
});
return res?.data;
}
static async update (space: Space): Promise<Space> {
const data = ApiLib.serializeAttachments(space, 'space', ['space_files_attributes', 'space_image_attributes']);
const res: AxiosResponse<Space> = await apiClient.put(`/api/spaces/${space.id}`, data, {
headers: {
'Content-Type': 'multipart/form-data'
}
});
return res?.data;
}
}

View File

@ -0,0 +1,111 @@
import React from 'react';
import { SubmitHandler, useForm, useWatch } from 'react-hook-form';
import SpaceAPI from '../../api/space';
import { useTranslation } from 'react-i18next';
import { FormInput } from '../form/form-input';
import { FormImageUpload } from '../form/form-image-upload';
import { IApplication } from '../../models/application';
import { Loader } from '../base/loader';
import { react2angular } from 'react2angular';
import { ErrorBoundary } from '../base/error-boundary';
import { FormRichText } from '../form/form-rich-text';
import { FormSwitch } from '../form/form-switch';
import { FormMultiFileUpload } from '../form/form-multi-file-upload';
import { FabButton } from '../base/fab-button';
import { Space } from '../../models/space';
declare const Application: IApplication;
interface SpaceFormProps {
action: 'create' | 'update',
space?: Space,
onError: (message: string) => void,
onSuccess: (message: string) => void,
}
/**
* Form to edit or create spaces
*/
export const SpaceForm: React.FC<SpaceFormProps> = ({ action, space, onError, onSuccess }) => {
const { handleSubmit, register, control, setValue, formState } = useForm<Space>({ defaultValues: { ...space } });
const output = useWatch<Space>({ control });
const { t } = useTranslation('admin');
/**
* Callback triggered when the user validates the machine form: handle create or update
*/
const onSubmit: SubmitHandler<Space> = (data: Space) => {
SpaceAPI[action](data).then((res) => {
onSuccess(t(`app.admin.space_form.${action}_success`));
window.location.href = `/#!/spaces/${res.slug}`;
}).catch(error => {
onError(error);
});
};
return (
<form className="space-form" onSubmit={handleSubmit(onSubmit)}>
<FormInput register={register} id="name"
formState={formState}
rules={{ required: true }}
label={t('app.admin.space_form.name')} />
<FormImageUpload setValue={setValue}
register={register}
control={control}
formState={formState}
rules={{ required: true }}
id="space_image_attributes"
accept="image/*"
defaultImage={output.space_image_attributes}
label={t('app.admin.space_form.illustration')} />
<FormInput register={register}
type="number"
id="default_places"
formState={formState}
rules={{ required: true }}
label={t('app.admin.space_form.default_seats')} />
<FormRichText control={control}
id="description"
rules={{ required: true }}
label={t('app.admin.space_form.description')}
limit={null}
heading bulletList blockquote link video image />
<FormRichText control={control}
id="characteristics"
label={t('app.admin.space_form.characteristics')}
limit={null}
heading bulletList blockquote link video image />
<div className='form-item-header space-files-header'>
<p>{t('app.admin.space_form.attached_files_pdf')}</p>
</div>
<FormMultiFileUpload setValue={setValue}
addButtonLabel={t('app.admin.space_form.add_an_attachment')}
control={control}
accept="application/pdf"
register={register}
id="space_files_attributes"
className="space-files" />
<FormSwitch control={control}
id="disabled"
label={t('app.admin.space_form.disable_space')}
tooltip={t('app.admin.space_form.disabled_help')} />
<FabButton type="submit" className="is-info submit-btn">
{t('app.admin.space_form.ACTION_space', { ACTION: action })}
</FabButton>
</form>
);
};
const SpaceFormWrapper: React.FC<SpaceFormProps> = (props) => {
return (
<Loader>
<ErrorBoundary>
<SpaceForm {...props} />
</ErrorBoundary>
</Loader>
);
};
Application.Components.component('spaceForm', react2angular(SpaceFormWrapper, ['action', 'space', 'onError', 'onSuccess']));

View File

@ -214,18 +214,22 @@ Application.Controllers.controller('SpacesController', ['$scope', '$state', 'spa
/**
* Controller used in the space creation page (admin)
*/
Application.Controllers.controller('NewSpaceController', ['$scope', '$state', 'CSRF', function ($scope, $state, CSRF) {
Application.Controllers.controller('NewSpaceController', ['$scope', '$state', 'CSRF', 'growl', function ($scope, $state, CSRF, growl) {
CSRF.setMetaTags();
// API URL where the form will be posted
$scope.actionUrl = '/api/spaces/';
/**
* Callback triggered by react components
*/
$scope.onSuccess = function (message) {
growl.success(message);
};
// Form action on the above URL
$scope.method = 'post';
// default space parameters
$scope.space =
{ space_files_attributes: [] };
/**
* Callback triggered by react components
*/
$scope.onError = function (message) {
growl.error(message);
};
// Using the SpacesController
return new SpacesController($scope, $state);
@ -234,18 +238,33 @@ Application.Controllers.controller('NewSpaceController', ['$scope', '$state', 'C
/**
* Controller used in the space edition page (admin)
*/
Application.Controllers.controller('EditSpaceController', ['$scope', '$state', '$transition$', 'spacePromise', 'CSRF',
function ($scope, $state, $transition$, spacePromise, CSRF) {
Application.Controllers.controller('EditSpaceController', ['$scope', '$state', '$transition$', 'spacePromise', 'CSRF', 'growl',
function ($scope, $state, $transition$, spacePromise, CSRF, growl) {
CSRF.setMetaTags();
// API URL where the form will be posted
$scope.actionUrl = `/api/spaces/${$transition$.params().id}`;
// Form action on the above URL
$scope.method = 'put';
// space to modify
$scope.space = spacePromise;
$scope.space = cleanSpace(spacePromise);
/**
* Callback triggered by react components
*/
$scope.onSuccess = function (message) {
growl.success(message);
};
/**
* Callback triggered by react components
*/
$scope.onError = function (message) {
growl.error(message);
};
// prepare the space for the react-hook-form
function cleanSpace (space) {
delete space.$promise;
delete space.$resolved;
return space;
}
// Using the SpacesController
return new SpacesController($scope, $state);

View File

@ -1,3 +1,4 @@
import { FileType } from './file';
export interface Space {
id: number,
@ -6,10 +7,6 @@ export interface Space {
slug: string,
default_places: number,
disabled: boolean,
space_image: string,
space_file_attributes?: {
id: number,
attachment: string,
attachement_url: string,
}
space_image_attributes: FileType,
space_file_attributes?: Array<FileType>
}

View File

@ -98,6 +98,7 @@
@import "modules/settings/check-list-setting";
@import "modules/settings/user-validation-setting";
@import "modules/socials/fab-socials";
@import "modules/spaces/space-form";
@import "modules/store/_utilities";
@import "modules/store/order-actions.scss";
@import "modules/store/order-item";

View File

@ -0,0 +1,11 @@
.space-form {
.space-files-header.form-item-header p {
cursor: default;
}
.space-files {
margin-bottom: 1.4rem;
}
.submit-btn {
float: right;
}
}

View File

@ -1,131 +0,0 @@
<uib-alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</uib-alert>
<div class="form-group m-b-lg" ng-class="{'has-error': spaceForm['space[name]'].$dirty && spaceForm['space[name]'].$invalid}">
<label for="space_name" class="col-sm-2 control-label">{{ 'app.shared.space.name' | translate }} *</label>
<div class="col-sm-4">
<input ng-model="space.name"
type="text"
name="space[name]"
class="form-control"
id="space_name"
placeholder="{{'app.shared.space.name' | translate}}"
required>
<span class="help-block" ng-show="spaceForm['space[name]'].$dirty && spaceForm['space[name]'].$error.required" translate>{{ 'app.shared.space.name_is_required' }}</span>
</div>
</div>
<div class="form-group m-b-lg">
<label for="space_image" class="col-sm-2 control-label">{{ 'app.shared.space.illustration' | translate }} *</label>
<div class="col-sm-10">
<div class="fileinput" data-provides="fileinput" ng-class="fileinputClass(space.space_image)">
<div class="fileinput-new thumbnail" style="width: 334px; height: 250px;">
<img src="data:image/png;base64," data-src="holder.js/100%x100%/text:&#xf03e;/font:'Font Awesome 5 Free'/icon" bs-holder ng-if="!space.space_image">
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 334px;">
<img ng-src="{{ space.space_image }}" alt="" />
</div>
<div>
<span class="btn btn-default btn-file">
<span class="fileinput-new">{{ 'app.shared.space.add_an_illustration' | translate }} <i class="fa fa-upload fa-fw"></i></span>
<span class="fileinput-exists" translate>{{ 'app.shared.buttons.change' }}</span>
<input type="file"
id="space_image"
ng-model="space.space_image"
name="space[space_image_attributes][attachment]"
accept="image/jpeg,image/gif,image/png"
required
bs-jasny-fileinput>
</span>
<a class="btn btn-danger fileinput-exists" data-dismiss="fileinput" translate>{{ 'app.shared.buttons.delete' }}</a>
</div>
</div>
</div>
</div>
<div class="form-group m-b-xl" ng-class="{'has-error': spaceForm['space[default_places]'].$dirty && spaceForm['space[default_places]'].$invalid}">
<label for="default_places" class="col-sm-2 control-label">{{ 'app.shared.space.default_places' | translate }} *</label>
<div class="col-sm-10">
<input type="number"
name="space[default_places]"
ng-model="space.default_places"
id="default_places"
class="form-control"
required>
<span class="help-block" ng-show="spaceForm['space[default_places]'].$dirty && spaceForm['space[default_places]'].$error.required" translate>{{ 'app.shared.space.default_places_is_required' }}</span>
</div>
</div>
<div class="form-group m-b-xl">
<label for="space_description" class="col-sm-2 control-label" translate>{{ 'app.shared.space.description' }}</label>
<div class="col-sm-10">
<input type="hidden"
name="space[description]"
ng-value="space.description" />
<summernote ng-model="space.description"
id="space_description"
placeholder=""
config="summernoteOpts"
name="space[description]">
</summernote>
</div>
</div>
<div class="form-group m-b-xl">
<label for="space_characteristics" class="col-sm-2 control-label" translate>{{ 'app.shared.space.characteristics' }}</label>
<div class="col-sm-10">
<input type="hidden"
name="space[characteristics]"
ng-value="space.characteristics" />
<summernote ng-model="space.characteristics"
id="space_characteristics"
placeholder=""
config="summernoteOpts"
name="space[characteristics]">
</summernote>
</div>
</div>
<div class="form-group m-b-xl">
<label class="col-sm-2 control-label" translate>{{ 'app.shared.space.attached_files_pdf' }}</label>
<div class="col-sm-10">
<div ng-repeat="file in space.space_files_attributes" ng-show="!file._destroy">
<input type="hidden" ng-model="file.id" name="space[space_files_attributes][][id]" ng-value="file.id" />
<input type="hidden" ng-model="file._destroy" name="space[space_files_attributes][][_destroy]" ng-value="file._destroy"/>
<div class="fileinput input-group" data-provides="fileinput" ng-class="fileinputClass(file.attachment)">
<div class="form-control" data-trigger="fileinput">
<i class="glyphicon glyphicon-file fileinput-exists"></i> <span class="fileinput-filename">{{file.attachment}}</span>
</div>
<span class="input-group-addon btn btn-default btn-file">
<span class="fileinput-new" translate>{{ 'app.shared.space.attach_a_file' }}</span>
<span class="fileinput-exists" translate>{{ 'app.shared.buttons.change' }}</span>
<input type="file" name="space[space_files_attributes][][attachment]" accept="application/pdf">
</span>
<a class="input-group-addon btn btn-danger fileinput-exists" data-dismiss="fileinput" ng-click="deleteFile(file)"><i class="fa fa-trash-o"></i></a>
</div>
</div>
<a class="btn btn-default" ng-click="addFile()" role="button"> {{ 'app.shared.space.add_an_attachment' | translate }} <i class="fa fa-file-o fa-fw"></i></a>
</div>
</div>
<div class="form-group">
<label for="space[disabled]" class="control-label col-sm-2" translate>
{{ 'app.shared.space.disable_space' }}
</label>
<div class="col-sm-10">
<input bs-switch
ng-model="space.disabled"
name="space[disabled]"
id="space[disabled]"
type="checkbox"
class="form-control"
switch-on-text="{{ 'app.shared.buttons.yes' | translate }}"
switch-off-text="{{ 'app.shared.buttons.no' | translate }}"
switch-animate="true"/>
<input type="hidden" name="space[disabled]" value="{{space.disabled}}">
</div>
</div>

View File

@ -20,30 +20,11 @@
<div class="row no-gutter" >
<div class="col-md-9 b-r nopadding">
<form role="form"
name="spaceForm"
class="form-horizontal"
action="{{ actionUrl }}"
ng-upload="submited(content)"
upload-options-enable-rails-csrf="true"
unsaved-warning-form
novalidate>
<input name="_method" type="hidden" ng-value="method">
<section class="panel panel-default bg-light m-lg">
<div class="panel-body m-r">
<ng-include src="'/spaces/_form.html'"></ng-include>
</div>
<div class="panel-footer no-padder">
<input type="submit"
value="{{ 'app.admin.space_edit.validate_the_changes' | translate }}"
class="r-b btn-valid btn btn-warning btn-block p-lg btn-lg text-u-c"
ng-disabled="spaceForm.$invalid"/>
</div>
</section>
</form>
<div class="panel panel-default bg-light m-lg">
<div class="panel-body m-r">
<space-form action="'update'" space="space" on-success="onSuccess" on-error="onError"></space-form>
</div>
</div>
</div>
<div class="col-md-3"/>

View File

@ -48,10 +48,10 @@
<div class="col-xs-12 col-sm-6 col-md-4 reservable-card" ng-class="{'disabled-reservable' : space.disabled && spaceFiltering === 'all'}" ng-repeat="space in spaces | filterDisabled:spaceFiltering">
<div class="widget panel panel-default">
<div class="panel-heading picture" ng-if="!space.space_image" ng-click="showSpace(space)">
<div class="panel-heading picture" ng-if="!space.space_image_attributes" ng-click="showSpace(space)">
<img src="data:image/png;base64," data-src="holder.js/100%x100%/text:&#xf03e;/font:'Font Awesome 5 Free'/icon" bs-holder class="img-responsive">
</div>
<div class="panel-heading picture" style="background-image:url({{space.space_image}})" ng-if="space.space_image" ng-click="showSpace(space)">
<div class="panel-heading picture" style="background-image:url({{space.space_image_attributes.attachment_url}})" ng-if="space.space_image_attributes" ng-click="showSpace(space)">
</div>
<div class="panel-body">
<h1 class="text-center m-b">{{space.name}}</h1>

View File

@ -26,30 +26,11 @@
{{ 'app.admin.space_new.consider_changing_its_prices_before_creating_any_reservation_slot' | translate }}
</div>
<form role="form"
name="spaceForm"
class="form-horizontal"
action="{{ actionUrl }}"
ng-upload="submited(content)"
upload-options-enable-rails-csrf="true"
unsaved-warning-form
novalidate>
<input name="_method" type="hidden" ng-value="method">
<section class="panel panel-default bg-light m-lg">
<div class="panel-body m-r">
<ng-include src="'/spaces/_form.html'"></ng-include>
</div>
<div class="panel-footer no-padder">
<input type="submit"
value="{{ 'app.admin.space_new.add_this_space' | translate }}"
class="r-b btn-valid btn btn-warning btn-block p-lg btn-lg text-u-c"
ng-disabled="spaceForm.$invalid"/>
</div>
</section>
</form>
<div class="panel panel-default bg-light m-lg">
<div class="panel-body m-r">
<space-form action="'create'" on-success="onSuccess" on-error="onError"></space-form>
</div>
</div>
</div>

View File

@ -30,8 +30,8 @@
<div class="article wrapper-lg">
<div class="article-thumbnail" ng-if="space.space_image">
<img ng-src="{{space.space_image}}" alt="{{space.name}}" class="img-responsive">
<div class="article-thumbnail" ng-if="space.space_image_attributes">
<img ng-src="{{space.space_image_attributes.attachment_url}}" alt="{{space.name}}" class="img-responsive">
</div>
<p class="intro" ng-bind-html="space.description | toTrusted"></p>
@ -42,7 +42,7 @@
<div class="col-sm-12 col-md-12 col-lg-4">
<div class="widget panel b-a m m-t-lg">
<div class="widget panel b-a m m-t-lg" ng-show="space.characteristics">
<div class="panel-heading b-b small">
<h3 translate>{{ 'app.public.space_show.characteristics' }}</h3>
</div>
@ -60,7 +60,7 @@
<ul class="widget-content list-group list-group-lg no-bg auto">
<li ng-repeat="file in space.space_files_attributes" class="list-group-item no-b clearfix">
<a target="_blank" ng-href="{{file.attachment_url}}"><i class="fa fa-arrow-circle-o-down"> </i> {{file.attachment | humanize : 25}}</a>
<a target="_blank" ng-href="{{file.attachment_url}}"><i class="fa fa-arrow-circle-o-down"> </i> {{file.attachment_name | humanize : 25}}</a>
</li>
</ul>
</section>

View File

@ -0,0 +1,10 @@
# frozen_string_literal: true
json.extract! space, :id, :name, :description, :slug, :default_places, :disabled
if space.space_image
json.space_image_attributes do
json.id space.space_image.id
json.attachment_name space.space_image.attachment_identifier
json.attachment_url space.space_image.attachment.url
end
end

View File

@ -1,6 +1,5 @@
# frozen_string_literal: true
json.array!(@spaces) do |space|
json.extract! space, :id, :name, :description, :slug, :default_places, :disabled
json.space_image space.space_image.attachment.medium.url if space.space_image
json.partial! 'api/spaces/space', space: space
end

View File

@ -1,14 +1,9 @@
# frozen_string_literal: true
json.extract! @space, :id, :name, :description, :characteristics, :created_at, :updated_at, :slug, :default_places, :disabled
json.space_image @space.space_image.attachment.large.url if @space.space_image
json.partial! 'api/spaces/space', space: @space
json.extract! @space, :characteristics, :created_at, :updated_at
json.space_files_attributes @space.space_files do |f|
json.id f.id
json.attachment f.attachment_identifier
json.attachment_name f.attachment_identifier
json.attachment_url f.attachment_url
end
# Unused for the moment. May be used to show a list of projects
# using the space in the space_show screen
# json.space_projects @space.projects do |p|
# json.extract! p, :slug, :name
# end

View File

@ -8,7 +8,6 @@ en:
description: "Description"
technical_specifications: "Technical specifications"
attached_files_pdf: "Attached files (pdf)"
attach_a_file: "Attach a file"
add_an_attachment: "Add an attachment"
disable_machine: "Disable machine"
disabled_help: "When disabled, the machine won't be reservable and won't appear by default in the machines list."
@ -31,6 +30,20 @@ en:
ACTION_training: "{ACTION, select, create{Create} other{Update}} the training"
create_success: "The training was created successfully"
update_success: "The training was updated successfully"
space_form:
name: "Name"
illustration: "Illustration"
add_an_illustration: "Add an illustration"
description: "Description"
characteristics: "Characteristics"
attached_files_pdf: "Attached files (pdf)"
add_an_attachment: "Add an attachment"
default_seats: "Default number of seats"
disable_space: "Disable the space"
disabled_help: "When disabled, the space won't be reservable and won't appear by default in the spaces list."
ACTION_space: "{ACTION, select, create{Create} other{Update}} the space"
create_success: "The space was created successfully"
update_success: "The space was updated successfully"
#add a new machine
machines_new:
declare_a_new_machine: "Declare a new machine"

View File

@ -141,21 +141,6 @@ en:
themes: "Themes"
tags: "Tags"
save_as_draft: "Save as draft"
#machine edition form
machine:
name: "Name"
name_is_required: "Name is required."
illustration: "Visual"
add_an_illustration: "Add a visual"
description: "Description"
description_is_required: "Description is required."
technical_specifications: "Technical specifications"
technical_specifications_are_required: "Technical specifications are required."
attached_files_pdf: "Attached files (pdf)"
attach_a_file: "Attach a file"
add_an_attachment: "Add an attachment"
disable_machine: "Disable machine"
validate_your_machine: "Validate your machine"
#button to book a machine reservation
reserve_button:
book_this_machine: "Book this machine"
@ -267,20 +252,6 @@ en:
email_address_is_required: "Email address is required."
disabled: "Disable subscription"
disable_plan_will_not_unsubscribe_users: "Beware: disabling this plan won't unsubscribe users having active subscriptions with it."
#training edition form
trainings:
name: "Name"
name_is_required: "Name is required."
illustration: "Illustration"
add_an_illustration: "Add an illustration"
description: "Description"
description_is_required: "Description is required."
add_a_new_training: "Add a new training"
validate_your_training: "Validate your training"
associated_machines: "Associated machines"
number_of_tickets: "Number of tickets"
public_page: "Show in training lists"
disable_training: "Disable the training"
#partial form to edit/create a user (admin view)
user_admin:
user: "User"
@ -397,22 +368,6 @@ en:
unable_to_apply_the_coupon_because_amount_exceeded: "Unable to apply the coupon: the discount exceed the total amount of this purchase."
unable_to_apply_the_coupon_because_undefined: "Unable to apply the coupon: an unexpected error occurred, please contact the Fablab's manager."
unable_to_apply_the_coupon_because_rejected: "This code does not exists."
#form to create/edit a space
space:
name: "Name"
name_is_required: "Name is required."
illustration: "Illustration"
add_an_illustration: "Add an illustration"
description: "Description"
description_is_required: "Description is required."
characteristics: "Characteristics"
characteristics_are_required: "Characteristics are required."
attached_files_pdf: "Attached files (pdf)"
attach_a_file: "Attach a file"
add_an_attachment: "Add an attachment"
default_places: "Default maximum tickets"
default_places_is_required: "Default maximum tickets is required."
disable_space: "Disable space"
payment_schedule_summary:
your_payment_schedule: "Your payment schedule"
NUMBER_monthly_payment_of_AMOUNT: "{NUMBER} monthly {NUMBER, plural, =1{payment} other{payments}} of {AMOUNT}"