color editing

This commit is contained in:
Jori Lallo
2017-10-29 23:22:46 -07:00
parent a1bfde7aec
commit e70a8c2495
18 changed files with 261 additions and 16 deletions

View File

@ -11,14 +11,17 @@ import { Collection } from '../models';
const router = new Router();
router.post('collections.create', auth(), async ctx => {
const { name, description, type } = ctx.body;
const { name, color, description, type } = ctx.body;
ctx.assertPresent(name, 'name is required');
if (color)
ctx.assertHexColor(color, 'Invalid hex value (please use format #FFFFFF)');
const user = ctx.state.user;
const collection = await Collection.create({
name,
description,
color,
type: type || 'atlas',
teamId: user.teamId,
creatorId: user.id,
@ -30,11 +33,14 @@ router.post('collections.create', auth(), async ctx => {
});
router.post('collections.update', auth(), async ctx => {
const { id, name } = ctx.body;
const { id, name, color } = ctx.body;
ctx.assertPresent(name, 'name is required');
if (color)
ctx.assertHexColor(color, 'Invalid hex value (please use format #FFFFFF)');
const collection = await Collection.findById(id);
collection.name = name;
collection.color = color;
await collection.save();
ctx.body = {

View File

@ -1,6 +1,7 @@
// @flow
import apiError from '../../errors';
import validator from 'validator';
import { validateColorHex } from '../../../shared/utils/color';
export default function validation() {
return function validationMiddleware(ctx: Object, next: Function) {
@ -28,6 +29,12 @@ export default function validation() {
}
};
ctx.assertHexColor = (value, message) => {
if (!validateColorHex(value)) {
throw apiError(400, 'validation_error', message);
}
};
return next();
};
}

View File

@ -0,0 +1,11 @@
module.exports = {
up: function(queryInterface, Sequelize) {
queryInterface.addColumn('collections', 'color', {
type: Sequelize.TEXT,
});
},
down: function(queryInterface, Sequelize) {
queryInterface.removeColumn('collections', 'color');
},
};

View File

@ -22,6 +22,7 @@ const Collection = sequelize.define(
urlId: { type: DataTypes.STRING, unique: true },
name: DataTypes.STRING,
description: DataTypes.STRING,
color: DataTypes.STRING,
type: {
type: DataTypes.STRING,
validate: { isIn: allowedCollectionTypes },

View File

@ -5,7 +5,7 @@ import randomstring from 'randomstring';
import isUUID from 'validator/lib/isUUID';
import { DataTypes, sequelize } from '../sequelize';
import parseTitle from '../../shared/parseTitle';
import parseTitle from '../../shared/utils/parseTitle.js';
import Revision from './Revision';
const URL_REGEX = /^[a-zA-Z0-9-]*-([a-zA-Z0-9]{10,15})$/;

View File

@ -11,6 +11,7 @@ async function present(ctx: Object, collection: Collection) {
url: collection.getUrl(),
name: collection.name,
description: collection.description,
color: collection.color || '#4E5C6E',
type: collection.type,
createdAt: collection.createdAt,
updatedAt: collection.updatedAt,