From d65845dd09a5deae54347bc56cacbbfcb5c42a62 Mon Sep 17 00:00:00 2001 From: James Date: Sun, 26 Jan 2020 15:43:35 -0500 Subject: [PATCH] fixes bugs with breaking NotFound error messages, builds frontend Global views --- demo/globals/Footer.js | 3 +- demo/globals/Header.js | 4 +- src/client/components/Routes.js | 18 +++++ .../components/views/globals/Edit/index.js | 70 +++++++++++++++++++ .../components/views/globals/Edit/index.scss | 16 +++++ src/globals/registerSchema.js | 2 +- src/globals/requestHandlers.js | 28 ++++---- src/mongoose/requestHandlers/findOne.js | 4 +- src/mongoose/requestHandlers/query.js | 4 +- src/mongoose/resolvers/findOne.js | 4 +- src/mongoose/resolvers/modelById.js | 2 +- src/responses/formatError.js | 1 + 12 files changed, 131 insertions(+), 25 deletions(-) create mode 100644 src/client/components/views/globals/Edit/index.js create mode 100644 src/client/components/views/globals/Edit/index.scss diff --git a/demo/globals/Footer.js b/demo/globals/Footer.js index 883c5b1c67..46e600fe1f 100644 --- a/demo/globals/Footer.js +++ b/demo/globals/Footer.js @@ -13,8 +13,7 @@ module.exports = { { name: 'copyright', label: 'Copyright', - type: 'input', - required: true, + type: 'text', }, ], }; diff --git a/demo/globals/Header.js b/demo/globals/Header.js index 5300e8df2f..332cb7e16a 100644 --- a/demo/globals/Header.js +++ b/demo/globals/Header.js @@ -15,7 +15,7 @@ module.exports = { { name: 'title', label: 'Site Title', - type: 'input', + type: 'text', localized: true, maxLength: 100, required: true, @@ -23,7 +23,7 @@ module.exports = { { name: 'logo', label: 'Logo', - type: 'media', + type: 'upload', required: false, }, { diff --git a/src/client/components/Routes.js b/src/client/components/Routes.js index a2eb273181..b6913185aa 100644 --- a/src/client/components/Routes.js +++ b/src/client/components/Routes.js @@ -13,6 +13,7 @@ import CreateFirstUser from './views/CreateFirstUser'; import MediaLibrary from './views/MediaLibrary'; import Edit from './views/collections/Edit'; import List from './views/collections/List'; +import EditGlobal from './views/globals/Edit'; import { requests } from '../api'; const Routes = () => { @@ -123,6 +124,23 @@ const Routes = () => { /> ); })} + {config.globals.map((global) => { + return ( + { + return ( + + ); + }} + /> + ); + })} diff --git a/src/client/components/views/globals/Edit/index.js b/src/client/components/views/globals/Edit/index.js new file mode 100644 index 0000000000..a53af6718a --- /dev/null +++ b/src/client/components/views/globals/Edit/index.js @@ -0,0 +1,70 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import getSanitizedConfig from '../../../../config/getSanitizedConfig'; +import DefaultTemplate from '../../../layout/DefaultTemplate'; +import usePayloadAPI from '../../../../hooks/usePayloadAPI'; +import Form from '../../../forms/Form'; +import StickyHeader from '../../../modules/StickyHeader'; +import APIURL from '../../../modules/APIURL'; +import Button from '../../../controls/Button'; +import FormSubmit from '../../../forms/Submit'; +import RenderFields from '../../../forms/RenderFields'; + +import './index.scss'; + +const { + serverURL, + routes: { + admin + } +} = getSanitizedConfig(); + +const baseClass = 'global-edit'; + +const EditView = (props) => { + const { global } = props; + + const [{ data }] = usePayloadAPI( + `${serverURL}/globals/${global.slug}`, + { initialParams: { 'fallback-locale': 'null' } } + ); + + const nav = [{ + url: `${admin}/globals/${global.slug}`, + label: global.label, + }]; + + return ( + +
+

+ Edit {global.label} +

+
+
+ + } action={ + <> + + Save + + } /> + + +
+ ); +}; + +EditView.propTypes = { + global: PropTypes.shape({ + label: PropTypes.string, + slug: PropTypes.string, + }).isRequired, +}; + +export default EditView; diff --git a/src/client/components/views/globals/Edit/index.scss b/src/client/components/views/globals/Edit/index.scss new file mode 100644 index 0000000000..e1a41c55d4 --- /dev/null +++ b/src/client/components/views/globals/Edit/index.scss @@ -0,0 +1,16 @@ +@import '~payload/client/scss/styles'; + +.global-edit { + &__header { + + h1 { + margin: 0; + } + } + + .sticky-header { + &:before { + left: - base(1.5); + } + } +} diff --git a/src/globals/registerSchema.js b/src/globals/registerSchema.js index 3e95b97a9e..13003f8b7a 100644 --- a/src/globals/registerSchema.js +++ b/src/globals/registerSchema.js @@ -17,7 +17,7 @@ const registerSchema = (globalConfigs, config) => { globalConfig.fields.forEach((field) => { const fieldSchema = fieldToSchemaMap[field.type]; - if (fieldSchema) globalFields[globalConfig.slug][field.name] = fieldSchema(field, { path: globalConfig.slug, localization: config.localization }); + if (fieldSchema) globalFields[globalConfig.slug][field.name] = fieldSchema(field, config); }); globalSchemaGroups[globalConfig.slug] = globalFields[globalConfig.slug]; }); diff --git a/src/globals/requestHandlers.js b/src/globals/requestHandlers.js index d1925f1ace..351190d0f4 100644 --- a/src/globals/requestHandlers.js +++ b/src/globals/requestHandlers.js @@ -1,11 +1,11 @@ const httpStatus = require('http-status'); const { findOne } = require('../mongoose/resolvers'); const { NotFound } = require('../errors'); +const formatErrorResponse = require('../responses/formatError'); const upsert = (req, res) => { if (!req.model.schema.tree[req.params.slug]) { - res.status(httpStatus.NOT_FOUND).json(new NotFound()); - return; + return res.status(httpStatus.NOT_FOUND).json(formatErrorResponse(new NotFound(), 'APIError')); } req.model.findOne({}, (findErr, doc) => { @@ -18,7 +18,7 @@ const upsert = (req, res) => { } return req.model.create(global, (createErr, result) => { - if (createErr) return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: createErr }); + if (createErr) return res.status(httpStatus.INTERNAL_SERVER_ERROR).json(formatErrorResponse(createErr, 'mongoose')); return res.status(httpStatus.CREATED) .json({ @@ -35,11 +35,11 @@ const upsert = (req, res) => { Object.assign(doc[req.params.slug], req.body); return doc.save((err) => { - if (err) return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }); + if (err) return res.status(httpStatus.INTERNAL_SERVER_ERROR).json(formatErrorResponse(err, 'mongoose')); return res.json({ - message: 'success', - result: doc.toJSON({ virtuals: true }), + message: 'Saved successfully.', + doc: doc.toJSON({ virtuals: true }), }); }); }); @@ -55,16 +55,16 @@ const fetch = (req, res) => { findOne(query) .then((doc) => { - const globals = { ...doc }; - - if (globals[req.params.key]) { - return res.json(globals[req.params.key]); - } if (req.params.key) { - return res.status(httpStatus.NOT_FOUND).json(new NotFound()); + if (doc[req.params.slug]) { + return res.json(doc[req.params.slug]); + } if (req.params.slug) { + return res.status(httpStatus.NOT_FOUND).json(formatErrorResponse(new NotFound(), 'APIError')); } - return res.json(globals); + return res.json(doc); }) - .catch(err => res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err })); + .catch((err) => { + return res.status(httpStatus.NOT_FOUND).json(formatErrorResponse(err, 'APIError')); + }); }; module.exports = { diff --git a/src/mongoose/requestHandlers/findOne.js b/src/mongoose/requestHandlers/findOne.js index 36b12d09e2..746caf141b 100644 --- a/src/mongoose/requestHandlers/findOne.js +++ b/src/mongoose/requestHandlers/findOne.js @@ -1,4 +1,6 @@ +const httpStatus = require('http-status'); const { modelById } = require('../resolvers'); +const formatErrorResponse = require('../../responses/formatError'); const findOne = (req, res) => { const query = { @@ -11,7 +13,7 @@ const findOne = (req, res) => { modelById(query) .then(doc => res.json(doc)) - .catch(err => res.status(err.status).json(err)); + .catch(err => res.status(httpStatus.INTERNAL_SERVER_ERROR).json(formatErrorResponse(err, 'mongoose'))); }; module.exports = findOne; diff --git a/src/mongoose/requestHandlers/query.js b/src/mongoose/requestHandlers/query.js index 8fce4b7f58..0d523c33ae 100644 --- a/src/mongoose/requestHandlers/query.js +++ b/src/mongoose/requestHandlers/query.js @@ -1,4 +1,5 @@ const httpStatus = require('http-status'); +const formatErrorResponse = require('../../responses/formatError'); const query = (req, res) => { const queryOptions = {}; @@ -11,8 +12,7 @@ const query = (req, res) => { req.model.paginate(req.model.apiQuery(req.query, req.locale), { options: queryOptions }, (err, result) => { if (err) { - res.status(httpStatus.INTERNAL_SERVER_ERROR).json(err); - return; + return res.status(httpStatus.INTERNAL_SERVER_ERROR).json(formatErrorResponse(err, 'mongoose')); } res.status(httpStatus.OK).json({ ...result, diff --git a/src/mongoose/resolvers/findOne.js b/src/mongoose/resolvers/findOne.js index 8686619792..54ac6cf2b3 100644 --- a/src/mongoose/resolvers/findOne.js +++ b/src/mongoose/resolvers/findOne.js @@ -1,6 +1,6 @@ const { NotFound } = require('../../errors'); -const find = ({ +const findOne = ({ Model, locale, fallback, depth, }) => { const options = {}; @@ -30,4 +30,4 @@ const find = ({ }); }; -module.exports = find; +module.exports = findOne; diff --git a/src/mongoose/resolvers/modelById.js b/src/mongoose/resolvers/modelById.js index d378fc12f0..67f429bd67 100644 --- a/src/mongoose/resolvers/modelById.js +++ b/src/mongoose/resolvers/modelById.js @@ -13,7 +13,7 @@ const modelById = (query) => { return new Promise((resolve, reject) => { query.Model.findOne({ _id: query.id }, {}, options, (err, doc) => { if (err || !doc) { - reject(new NotFound()); + reject(err); return; } diff --git a/src/responses/formatError.js b/src/responses/formatError.js index ccbd98bfd6..9c6c3588f0 100644 --- a/src/responses/formatError.js +++ b/src/responses/formatError.js @@ -10,6 +10,7 @@ const formatErrorResponse = (incoming, source) => { }, []), }; + case 'APIError': return { errors: [