From 8b87ad92f1e496b75c227fc64ddde53998c84f24 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 9 Jun 2021 10:58:45 +0200 Subject: [PATCH] Added option to pin new apps/categories by default --- .../Settings/OtherSettings/OtherSettings.tsx | 42 +++++++++++++++++-- controllers/apps.js | 19 ++++++++- controllers/category.js | 19 ++++++++- utils/initConfig.js | 13 +++--- utils/initialConfig.json | 32 ++++++++++++++ 5 files changed, 111 insertions(+), 14 deletions(-) create mode 100644 utils/initialConfig.json diff --git a/client/src/components/Settings/OtherSettings/OtherSettings.tsx b/client/src/components/Settings/OtherSettings/OtherSettings.tsx index 2d3a6cd..7a6090e 100644 --- a/client/src/components/Settings/OtherSettings/OtherSettings.tsx +++ b/client/src/components/Settings/OtherSettings/OtherSettings.tsx @@ -9,6 +9,8 @@ import { ApiResponse, Config, NewNotification } from '../../../interfaces'; interface FormState { customTitle: string; + pinAppsByDefault: number; + pinCategoriesByDefault: number; } interface ComponentProps { @@ -17,12 +19,14 @@ interface ComponentProps { const OtherSettings = (props: ComponentProps): JSX.Element => { const [formData, setFormData] = useState({ - customTitle: document.title + customTitle: document.title, + pinAppsByDefault: 0, + pinCategoriesByDefault: 0 }) // get initial config useEffect(() => { - axios.get>('/api/config?keys=customTitle') + axios.get>('/api/config?keys=customTitle,pinAppsByDefault,pinCategoriesByDefault') .then(data => { let tmpFormData = { ...formData }; @@ -60,10 +64,16 @@ const OtherSettings = (props: ComponentProps): JSX.Element => { document.title = formData.customTitle; } - const inputChangeHandler = (e: ChangeEvent) => { + const inputChangeHandler = (e: ChangeEvent, isNumber?: boolean) => { + let value: string | number = e.target.value; + + if (isNumber) { + value = parseFloat(value); + } + setFormData({ ...formData, - [e.target.name]: e.target.value + [e.target.name]: value }) } @@ -80,6 +90,30 @@ const OtherSettings = (props: ComponentProps): JSX.Element => { onChange={(e) => inputChangeHandler(e)} /> + + + + + + + + ) diff --git a/controllers/apps.js b/controllers/apps.js index 496fa54..4f21394 100644 --- a/controllers/apps.js +++ b/controllers/apps.js @@ -1,12 +1,29 @@ const asyncWrapper = require('../middleware/asyncWrapper'); const ErrorResponse = require('../utils/ErrorResponse'); const App = require('../models/App'); +const Config = require('../models/Config'); // @desc Create new app // @route POST /api/apps // @access Public exports.createApp = asyncWrapper(async (req, res, next) => { - const app = await App.create(req.body); + // Get config from database + const pinApps = await Config.findOne({ + where: { key: 'pinAppsByDefault' } + }); + + let app; + + if (pinApps) { + if (parseInt(pinApps.value)) { + app = await App.create({ + ...req.body, + isPinned: true + }) + } else { + app = await App.create(req.body); + } + } res.status(201).json({ success: true, diff --git a/controllers/category.js b/controllers/category.js index 60e7305..a390405 100644 --- a/controllers/category.js +++ b/controllers/category.js @@ -2,12 +2,29 @@ const asyncWrapper = require('../middleware/asyncWrapper'); const ErrorResponse = require('../utils/ErrorResponse'); const Category = require('../models/Category'); const Bookmark = require('../models/Bookmark'); +const Config = require('../models/Config'); // @desc Create new category // @route POST /api/categories // @access Public exports.createCategory = asyncWrapper(async (req, res, next) => { - const category = await Category.create(req.body); + // Get config from database + const pinCategories = await Config.findOne({ + where: { key: 'pinCategoriesByDefault' } + }); + + let category; + + if (pinCategories) { + if (parseInt(pinCategories.value)) { + category = await Category.create({ + ...req.body, + isPinned: true + }) + } else { + category = await Category.create(req.body); + } + } res.status(201).json({ success: true, diff --git a/utils/initConfig.js b/utils/initConfig.js index c8e43fa..de1cc88 100644 --- a/utils/initConfig.js +++ b/utils/initConfig.js @@ -1,16 +1,13 @@ const { Op } = require('sequelize'); const Config = require('../models/Config'); +const { config } = require('./initialConfig.json'); const initConfig = async () => { - // Config keys - const keys = ['WEATHER_API_KEY', 'lat', 'long', 'isCelsius', 'customTitle']; - const values = ['', 0, 0, true, 'Flame']; - // Get config values const configPairs = await Config.findAll({ where: { key: { - [Op.or]: keys + [Op.or]: config.map(pair => pair.key) } } }) @@ -19,12 +16,12 @@ const initConfig = async () => { const configKeys = configPairs.map((pair) => pair.key); // Create missing pairs - keys.forEach(async (key, idx) => { + config.forEach(async ({ key, value}) => { if (!configKeys.includes(key)) { await Config.create({ key, - value: values[idx], - valueType: typeof values[idx] + value, + valueType: typeof value }) } }) diff --git a/utils/initialConfig.json b/utils/initialConfig.json new file mode 100644 index 0000000..eae974a --- /dev/null +++ b/utils/initialConfig.json @@ -0,0 +1,32 @@ +{ + "config": [ + { + "key": "WEATHER_API_KEY", + "value": "" + }, + { + "key": "lat", + "value": 0 + }, + { + "key": "long", + "value": 0 + }, + { + "key": "isCelsius", + "value": true + }, + { + "key": "customTitle", + "value": "Flame" + }, + { + "key": "pinAppsByDefault", + "value": true + }, + { + "key": "pinCategoriesByDefault", + "value": true + } + ] +} \ No newline at end of file