1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 13:19:44 +02:00

Add validation to background field

This commit is contained in:
Maksim Eltyshev 2020-06-03 22:27:20 +05:00
parent ab83d7c9ab
commit 99d9ab4a9e
10 changed files with 130 additions and 25 deletions

View file

@ -4,6 +4,8 @@ import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import classNames from 'classnames'; import classNames from 'classnames';
import { ProjectBackgroundTypes } from '../../constants/Enums';
import styles from './Background.module.scss'; import styles from './Background.module.scss';
import globalStyles from '../../styles.module.scss'; import globalStyles from '../../styles.module.scss';
@ -11,7 +13,8 @@ const Background = ({ type, name, imageUrl }) => (
<div <div
className={classNames( className={classNames(
styles.wrapper, styles.wrapper,
type === 'gradient' && globalStyles[`background${upperFirst(camelCase(name))}`], type === ProjectBackgroundTypes.GRADIENT &&
globalStyles[`background${upperFirst(camelCase(name))}`],
)} )}
style={{ style={{
background: type === 'image' && `url("${imageUrl}") center / cover`, background: type === 'image' && `url("${imageUrl}") center / cover`,

View file

@ -54,17 +54,10 @@ const ActionsStep = React.memo(
); );
const handleBackgroundImageDelete = useCallback(() => { const handleBackgroundImageDelete = useCallback(() => {
const data = { onUpdate({
backgroundImage: null, backgroundImage: null,
}; });
}, [onUpdate]);
// TODO: move to services?
if (project.background && project.background.type === 'image') {
data.background = null;
}
onUpdate(data);
}, [project.background, onUpdate]);
if (step) { if (step) {
if (step) { if (step) {

View file

@ -9,6 +9,7 @@ import { Button, Image } from 'semantic-ui-react';
import { FilePicker, Popup } from '../../../lib/custom-ui'; import { FilePicker, Popup } from '../../../lib/custom-ui';
import ProjectBackgroundGradients from '../../../constants/ProjectBackgroundGradients'; import ProjectBackgroundGradients from '../../../constants/ProjectBackgroundGradients';
import { ProjectBackgroundTypes } from '../../../constants/Enums';
import styles from './EditBackgroundStep.module.scss'; import styles from './EditBackgroundStep.module.scss';
import globalStyles from '../../../styles.module.scss'; import globalStyles from '../../../styles.module.scss';
@ -30,15 +31,12 @@ const EditBackgroundStep = React.memo(
const handleGradientClick = useCallback( const handleGradientClick = useCallback(
(_, { value }) => { (_, { value }) => {
const background = { const background = {
type: 'gradient', type: ProjectBackgroundTypes.GRADIENT,
name: value, name: value,
}; };
if (!dequal(background, defaultValue)) { if (!dequal(background, defaultValue)) {
onUpdate({ onUpdate(background);
type: 'gradient',
name: value,
});
} }
}, },
[defaultValue, onUpdate], [defaultValue, onUpdate],
@ -46,7 +44,7 @@ const EditBackgroundStep = React.memo(
const handleImageClick = useCallback(() => { const handleImageClick = useCallback(() => {
const background = { const background = {
type: 'image', type: ProjectBackgroundTypes.IMAGE,
}; };
if (!dequal(background, defaultValue)) { if (!dequal(background, defaultValue)) {
@ -93,7 +91,7 @@ const EditBackgroundStep = React.memo(
className={classNames( className={classNames(
styles.gradientButton, styles.gradientButton,
defaultValue && defaultValue &&
defaultValue.type === 'gradient' && defaultValue.type === ProjectBackgroundTypes.GRADIENT &&
gradient === defaultValue.name && gradient === defaultValue.name &&
styles.gradientButtonActive, styles.gradientButtonActive,
globalStyles[`background${upperFirst(camelCase(gradient))}`], globalStyles[`background${upperFirst(camelCase(gradient))}`],

View file

@ -66,6 +66,10 @@
.image { .image {
cursor: pointer; cursor: pointer;
margin-bottom: 8px; margin-bottom: 8px;
&:hover {
opacity: 0.9;
}
} }
.imageLabel { .imageLabel {

View file

@ -8,6 +8,7 @@ import { Link } from 'react-router-dom';
import { Container, Grid } from 'semantic-ui-react'; import { Container, Grid } from 'semantic-ui-react';
import Paths from '../../constants/Paths'; import Paths from '../../constants/Paths';
import { ProjectBackgroundTypes } from '../../constants/Enums';
import { ReactComponent as PlusIcon } from '../../assets/images/plus-icon.svg'; import { ReactComponent as PlusIcon } from '../../assets/images/plus-icon.svg';
import styles from './Projects.module.scss'; import styles from './Projects.module.scss';
@ -33,7 +34,7 @@ const Projects = React.memo(({ items, isEditable, onAdd }) => {
styles.card, styles.card,
styles.open, styles.open,
item.background && item.background &&
item.background.type === 'gradient' && item.background.type === ProjectBackgroundTypes.GRADIENT &&
globalStyles[`background${upperFirst(camelCase(item.background.name))}`], globalStyles[`background${upperFirst(camelCase(item.background.name))}`],
)} )}
style={{ style={{

View file

@ -1,4 +1,8 @@
// eslint-disable-next-line import/prefer-default-export export const ProjectBackgroundTypes = {
GRADIENT: 'gradient',
IMAGE: 'image',
};
export const ActionTypes = { export const ActionTypes = {
CREATE_CARD: 'createCard', CREATE_CARD: 'createCard',
MOVE_CARD: 'moveCard', MOVE_CARD: 'moveCard',

View file

@ -1,6 +1,7 @@
import { Model, attr, many } from 'redux-orm'; import { Model, attr, many } from 'redux-orm';
import ActionTypes from '../constants/ActionTypes'; import ActionTypes from '../constants/ActionTypes';
import { ProjectBackgroundTypes } from '../constants/Enums';
export default class extends Model { export default class extends Model {
static modelName = 'Project'; static modelName = 'Project';
@ -28,10 +29,20 @@ export default class extends Model {
}); });
break; break;
case ActionTypes.PROJECT_UPDATE: case ActionTypes.PROJECT_UPDATE: {
Project.withId(payload.id).update(payload.data); const project = Project.withId(payload.id);
project.update(payload.data);
if (
payload.data.backgroundImage === null &&
project.background &&
project.background.type === ProjectBackgroundTypes.IMAGE
) {
project.background = null;
}
break; break;
}
case ActionTypes.PROJECT_DELETE: case ActionTypes.PROJECT_DELETE:
Project.withId(payload.id).deleteWithRelated(); Project.withId(payload.id).deleteWithRelated();

View file

@ -15,9 +15,35 @@ module.exports = {
type: 'string', type: 'string',
isNotEmptyString: true, isNotEmptyString: true,
}, },
// TODO: add validation
background: { background: {
type: 'json', type: 'json',
custom: (value) => {
if (_.isNull(value)) {
return true;
}
if (!_.isPlainObject(value)) {
return false;
}
if (!Project.BACKGROUND_TYPES.includes(value.type)) {
return false;
}
if (
value.type === 'gradient' &&
_.size(value) === 2 &&
Project.BACKGROUND_GRADIENTS.includes(value.name)
) {
return true;
}
if (value.type === 'image' && _.size(value) === 1) {
return true;
}
return false;
},
}, },
backgroundImage: { backgroundImage: {
type: 'json', type: 'json',

View file

@ -11,7 +11,7 @@ module.exports = {
type: 'json', type: 'json',
custom: (value) => custom: (value) =>
_.isPlainObject(value) && _.isPlainObject(value) &&
/* _.isUndefined(value.background) || _.isNull(value.background) && */ (_.isUndefined(value.background) || _.isPlainObject(value.background)) &&
(_.isUndefined(value.backgroundImage) || _.isNull(value.backgroundImage)), (_.isUndefined(value.backgroundImage) || _.isNull(value.backgroundImage)),
required: true, required: true,
}, },
@ -20,6 +20,10 @@ module.exports = {
}, },
}, },
exits: {
invalidParams: {},
},
async fn(inputs, exits) { async fn(inputs, exits) {
if (!_.isUndefined(inputs.values.backgroundImage)) { if (!_.isUndefined(inputs.values.backgroundImage)) {
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
@ -33,9 +37,37 @@ module.exports = {
inputs.values.background = { inputs.values.background = {
type: 'image', type: 'image',
}; };
} else if (
_.isNull(inputs.values.backgroundImageDirname) &&
inputs.record.background &&
inputs.record.background.type === 'image'
) {
inputs.values.background = null; // eslint-disable-line no-param-reassign
} }
const project = await Project.updateOne(inputs.record.id).set(inputs.values); let project;
if (inputs.values.background && inputs.values.background.type === 'image') {
if (_.isNull(inputs.values.backgroundImageDirname)) {
throw 'invalidParams';
}
if (_.isUndefined(inputs.values.backgroundImageDirname)) {
project = await Project.updateOne({
id: inputs.record.id,
backgroundImageDirname: {
'!=': null,
},
}).set(inputs.values);
if (!project) {
delete inputs.values.background; // eslint-disable-line no-param-reassign
}
}
}
if (!project) {
project = await Project.updateOne(inputs.record.id).set(inputs.values);
}
if (project) { if (project) {
if ( if (

View file

@ -5,7 +5,40 @@
* @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models * @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models
*/ */
const BACKGROUND_TYPES = ['gradient', 'image'];
const BACKGROUND_GRADIENTS = [
'old-lime',
'ocean-dive',
'tzepesch-style',
'jungle-mesh',
'strawberry-dust',
'purple-rose',
'sun-scream',
'warm-rust',
'sky-change',
'green-eyes',
'blue-xchange',
'blood-orange',
'sour-peel',
'green-ninja',
'algae-green',
'coral-reef',
'steel-grey',
'heat-waves',
'velvet-lounge',
'purple-rain',
'blue-steel',
'blueish-curve',
'prism-light',
'green-mist',
'red-curtain',
];
module.exports = { module.exports = {
BACKGROUND_TYPES,
BACKGROUND_GRADIENTS,
attributes: { attributes: {
// ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦ ╦╔═╗╔═╗ // ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦ ╦╔═╗╔═╗
// ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗ // ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗