Skip to content

Commit

Permalink
migrate a few modals
Browse files Browse the repository at this point in the history
  • Loading branch information
dekkerglen committed Oct 31, 2024
1 parent c3a59da commit b700699
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 405 deletions.
8 changes: 4 additions & 4 deletions dynamo/models/cube.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const FIELDS = {
OWNER: 'owner',
NAME: 'name',
VISIBILITY: 'visibility',
PRICE_VISIBLITY: 'priceVisibility',
PRICE_VISIBILITY: 'priceVisibility',
FEATURED: 'featured',
CATEGORY_OVERRIDE: 'categoryOverride',
CATEGORY_PREFIXES: 'categoryPrefixes',
Expand Down Expand Up @@ -57,7 +57,7 @@ const VISIBILITY = {
UNLISTED: 'un',
};

const PRICE_VISIBLITY = {
const PRICE_VISIBILITY = {
PUBLIC: 'pu',
PRIVATE: 'pr',
};
Expand Down Expand Up @@ -377,7 +377,7 @@ module.exports = {
[FIELDS.SHORT_ID]: cube.shortID,
[FIELDS.OWNER]: `${cube.owner}`,
[FIELDS.VISIBILITY]: getVisibility(cube.isListed, cube.isPrivate),
[FIELDS.PRICE_VISIBLITY]: cube.privatePrices ? PRICE_VISIBLITY.PRIVATE : PRICE_VISIBLITY.PUBLIC,
[FIELDS.PRICE_VISIBILITY]: cube.privatePrices ? PRICE_VISIBILITY.PRIVATE : PRICE_VISIBILITY.PUBLIC,
[FIELDS.FEATURED]: cube.isFeatured,
[FIELDS.CATEGORY_OVERRIDE]: cube.overrideCategory ? cube.categoryOverride : null,
[FIELDS.CATEGORY_PREFIXES]: cube.overrideCategory ? cube.categoryPrefixes : null,
Expand Down Expand Up @@ -448,6 +448,6 @@ module.exports = {
};
},
VISIBILITY,
PRICE_VISIBLITY,
PRICE_VISIBILITY,
FIELDS,
};
10 changes: 10 additions & 0 deletions public/css/stylesheet.css
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,16 @@ video {
background-color: rgb(254 249 195 / var(--tw-bg-opacity));
}

.bg-gray-600 {
--tw-bg-opacity: 1;
background-color: rgb(75 85 99 / var(--tw-bg-opacity));
}

.bg-red-600 {
--tw-bg-opacity: 1;
background-color: rgb(220 38 38 / var(--tw-bg-opacity));
}

.bg-opacity-50 {
--tw-bg-opacity: 0.5;
}
Expand Down
45 changes: 0 additions & 45 deletions routes/cube/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const { generatePack, buildTagColors, cubeCardTags, isCubeViewable } = require('

// Bring in models
const Cube = require('../../dynamo/models/cube');
const CubeHash = require('../../dynamo/models/cubeHash');
const Draft = require('../../dynamo/models/draft');
const Package = require('../../dynamo/models/package');
const Blog = require('../../dynamo/models/blog');
Expand All @@ -41,50 +40,6 @@ router.get('/cardimages', (_, res) =>
}),
);



router.post(
'/settings/:id',
ensureAuth,
body('priceVisibility', 'Invalid Price visibility').isIn(
Object.entries(Cube.PRICE_VISIBLITY).map((entry) => entry[1]),
),
body('disableAlerts').toBoolean(),
body('defaultStatus', 'status must be valid.').isIn(['Owned', 'Not Owned']),
body('defaultPrinting', 'Printing must be valid.').isIn(['recent', 'first']),
body('visibility', 'visibility must be valid').isIn(Object.entries(Cube.VISIBILITY).map((entry) => entry[1])),
jsonValidationErrors,
util.wrapAsyncApi(async (req, res) => {
const cube = await Cube.getById(req.params.id);

if (!isCubeViewable(cube, req.user)) {
return res.status(404).send({
success: 'false',
message: 'Cube Not Found',
});
}

if (cube.owner.id !== req.user.id) {
return res.status(403).send({
success: 'false',
message: 'Unauthorized',
});
}

const update = req.body;
for (const field of ['visibility', 'priceVisibility', 'disableAlerts', 'defaultStatus', 'defaultPrinting']) {
if (update[field] !== undefined) {
cube[field] = update[field];
}
}

await Cube.update(cube);
return res.status(200).send({
success: 'true',
});
}),
);

router.get('/imagedict', (_, res) => {
res.status(200).send({
success: 'true',
Expand Down
240 changes: 146 additions & 94 deletions routes/cube/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ router.post('/add', ensureAuth, async (req, res) => {
description: 'This is a brand new cube!',
date: Date.now().valueOf(),
visibility: Cube.VISIBILITY.PUBLIC,
priceVisibility: Cube.PRICE_VISIBLITY.PUBLIC,
priceVisibility: Cube.PRICE_VISIBILITY.PUBLIC,
featured: false,
tagColors: [],
defaultFormat: -1,
Expand Down Expand Up @@ -127,7 +127,7 @@ router.get('/clone/:id', async (req, res) => {
description: `Cloned from [${source.name}](/c/${source.id})\n\n${source.description}`,
date: Date.now().valueOf(),
visibility: Cube.VISIBILITY.PUBLIC,
priceVisibility: Cube.PRICE_VISIBLITY.PUBLIC,
priceVisibility: Cube.PRICE_VISIBILITY.PUBLIC,
featured: false,
tagColors: source.tagColors,
defaultFormat: source.defaultFormat,
Expand Down Expand Up @@ -278,114 +278,108 @@ router.post(
}),
);

router.post(
'/editoverview',
ensureAuth,
async (req, res) => {
try {
const updatedCube = JSON.parse(req.body.cube);
router.post('/editoverview', ensureAuth, async (req, res) => {
try {
const updatedCube = JSON.parse(req.body.cube);

const cube = await Cube.getById(updatedCube.id);
const { user } = req;
const cube = await Cube.getById(updatedCube.id);
const { user } = req;

if (!isCubeViewable(cube, user)) {
req.flash('danger', 'Cube not found');
return redirect(req, res, '/404');
}
if (!isCubeViewable(cube, user)) {
req.flash('danger', 'Cube not found');
return redirect(req, res, '/404');
}

if (cube.owner.id !== user.id) {
req.flash('danger', 'Unauthorized');
return redirect(req, res, '/cube/overview/' + cube.id);
}

if (updatedCube.shortId !== cube.shortId) {
const taken = await CubeHash.getSortedByName(`shortid:${updatedCube.shortId.toLowerCase()}`);

if (cube.owner.id !== user.id) {
req.flash('danger', 'Unauthorized');
if (taken.items.length === 1 && taken.items[0].cube !== cube.id) {
req.flash('danger', 'Custom URL is already taken');
return redirect(req, res, '/cube/overview/' + cube.id);
}
if (taken.items.length > 1) {
req.flash('danger', 'Custom URL is already taken');
return redirect(req, res, '/cube/overview/' + cube.id);
}

if (updatedCube.shortId !== cube.shortId) {
const taken = await CubeHash.getSortedByName(`shortid:${updatedCube.shortId.toLowerCase()}`);
cube.shortId = updatedCube.shortId;
}

if (taken.items.length === 1 && taken.items[0].cube !== cube.id) {
req.flash('danger', 'Custom URL is already taken');
return redirect(req, res, '/cube/overview/' + cube.id);
}
if (taken.items.length > 1) {
req.flash('danger', 'Custom URL is already taken');
return redirect(req, res, '/cube/overview/' + cube.id);
}
cube.name = updatedCube.name;
cube.imageName = updatedCube.imageName;

cube.shortId = updatedCube.shortId;
if (updatedCube.description !== null) {
cube.description = updatedCube.description;
}
cube.date = Date.now().valueOf();

// cube category override
if (updatedCube.categoryOverride !== null) {
const categories = [
'',
'Vintage',
'Legacy+',
'Legacy',
'Modern',
'Premodern',
'Pioneer',
'Historic',
'Standard',
'Set',
];

if (!categories.includes(updatedCube.categoryOverride)) {
req.flash('danger', 'Not a valid category override.');
return redirect(req, res, '/cube/overview/' + cube.id);
}

cube.name = updatedCube.name;
cube.imageName = updatedCube.imageName;
cube.categoryOverride = updatedCube.categoryOverride;
} else {
cube.categoryOverride = null;
}

if (updatedCube.description !== null) {
cube.description = updatedCube.description;
}
cube.date = Date.now().valueOf();

// cube category override
if (updatedCube.categoryOverride !== null) {
const categories = [
'',
'Vintage',
'Legacy+',
'Legacy',
'Modern',
'Premodern',
'Pioneer',
'Historic',
'Standard',
'Set',
];

if (!categories.includes(updatedCube.categoryOverride)) {
req.flash('danger', 'Not a valid category override.');
if (updatedCube.categoryPrefixes !== null) {
const prefixes = [
'Powered',
'Unpowered',
'Pauper',
'Peasant',
'Budget',
'Silver-bordered',
'Commander',
'Battle Box',
'Multiplayer',
'Judge Tower',
'Desert',
];
for (let i = 0; i < (updatedCube.categoryPrefixes || []).length; i += 1) {
if (!prefixes.includes(updatedCube.categoryPrefixes[i])) {
req.flash('danger', 'Not a valid category prefix.');
return redirect(req, res, '/cube/overview/' + cube.id);
}


cube.categoryOverride = updatedCube.categoryOverride;
} else {
cube.categoryOverride = null;
}

if (updatedCube.categoryPrefixes !== null) {
const prefixes = [
'Powered',
'Unpowered',
'Pauper',
'Peasant',
'Budget',
'Silver-bordered',
'Commander',
'Battle Box',
'Multiplayer',
'Judge Tower',
'Desert',
];
for (let i = 0; i < (updatedCube.categoryPrefixes || []).length; i += 1) {
if (!prefixes.includes(updatedCube.categoryPrefixes[i])) {
req.flash('danger', 'Not a valid category prefix.');
return redirect(req, res, '/cube/overview/' + cube.id);
}
}
cube.categoryPrefixes = updatedCube.categoryPrefixes;
} else {
cube.categoryPrefixes = [];
}
cube.categoryPrefixes = updatedCube.categoryPrefixes;
} else {
cube.categoryPrefixes = [];
}

// cube tags
cube.tags = updatedCube.tags.filter((tag) => tag && tag.length > 0).map((tag) => tag.toLowerCase());

// cube tags
cube.tags = updatedCube.tags.filter((tag) => tag && tag.length > 0).map((tag) => tag.toLowerCase());

await Cube.update(cube);
req.flash('success', 'Cube updated successfully');
return redirect(req, res, '/cube/overview/' + cube.id);
} catch (err) {
req.logger.error(err.message, err.stack);
req.flash('danger', 'Error updating cube');
return redirect(req, res, '/cube/overview/' + cube.id);
}
},
);
await Cube.update(cube);
req.flash('success', 'Cube updated successfully');
return redirect(req, res, '/cube/overview/' + cube.id);
} catch (err) {
req.logger.error(err.message, err.stack);
req.flash('danger', 'Error updating cube');
return redirect(req, res, '/cube/overview/' + cube.id);
}
});

router.post('/feature/:id', ensureAuth, async (req, res) => {
const redirect = `/cube/overview/${encodeURIComponent(req.params.id)}`;
Expand Down Expand Up @@ -1474,6 +1468,64 @@ router.get('/format/remove/:cubeid/:index', ensureAuth, param('index').toInt(),
}
});

router.post('/updatesettings/:id', ensureAuth, async (req, res) => {
try {
const { priceVisibility, disableAlerts, defaultStatus, defaultPrinting, visibility } = req.body;

const errors = [];
if (priceVisibility !== 'true' && priceVisibility !== 'false') {
errors.push({ msg: 'Invalid Price visibility' });
}
if (disableAlerts !== 'true' && disableAlerts !== 'false') {
errors.push({ msg: 'Invalid value for disableAlerts' });
}
if (!['Owned', 'Not Owned'].includes(defaultStatus)) {
errors.push({ msg: 'Status must be valid.' });
}
if (!['recent', 'first'].includes(defaultPrinting)) {
errors.push({ msg: 'Printing must be valid.' });
}
if (!Object.values(Cube.VISIBILITY).includes(visibility)) {
errors.push({ msg: 'Visibility must be valid' });
}

if (errors.length > 0) {
req.flash('danger', 'Error updating cube: ' + errors.map((error) => error.msg).join(', '));
return redirect(req, res, '/cube/overview/' + req.params.id);
}

const cube = await Cube.getById(req.params.id);

if (!isCubeViewable(cube, req.user)) {
req.flash('danger', 'Cube not found.');
return redirect(req, res, '/404');
}

if (cube.owner.id !== req.user.id) {
req.flash('danger', 'Unauthorized');
return redirect(req, res, '/cube/overview/' + req.params.id);
}

const update = req.body;
for (const field of ['visibility', 'defaultStatus', 'defaultPrinting']) {
if (update[field] !== undefined) {
cube[field] = update[field];
}
}
cube.disableAlerts = update.disableAlerts === 'true';
cube.priceVisibility =
update.priceVisibility === 'true' ? Cube.PRICE_VISIBILITY.PUBLIC : Cube.PRICE_VISIBILITY.PRIVATE;

await Cube.update(cube);
req.flash('success', 'Settings updated successfully.');
return redirect(req, res, '/cube/overview/' + req.params.id);
} catch (err) {
req.flash('danger', 'Error updating settings. ' + err.message);
req.logger.error('Error updating settings:', err);
return redirect(req, res, '/cube/overview/' + req.params.id);
}
});

router.get(
'/:id/defaultdraftformat/:formatId',
ensureAuth,
Expand Down
Loading

0 comments on commit b700699

Please sign in to comment.