Start working on get shelf with fake data

This commit is contained in:
Robbie Antenesse 2020-01-07 22:50:58 -07:00
parent 5eb409e46e
commit b7392105e7
4 changed files with 100 additions and 28 deletions

View File

@ -7,25 +7,7 @@ export class ShelvesController extends ViewController {
super(state, i18n, 'shelves', {
openModal: null, // state value for edit modals
myShelves: [], // array of objects in sort order with name, id, and deletability.
loadedShelves: { // object key is shelf id with name and shelfItems
0: {
name: 'Test Shelf',
user: {
userName: 'testinTesterton',
displayName: 'Testin Testerton',
},
shelfItems: [
{
name: 'Book Title',
author: 'Someone Talented',
coverURL: 'https://picnicss.com/web/img/optimised.svg',
coverEdition: 'Special Edition',
rating: 4,
review: 'The Special Edition of Book Title by Someone Talented is my favorite thing in the whole world. I think and dream about it constantly and there is nothing better in the whole world.',
}
]
},
},
loadedShelves: {}, // object key is shelf id with name and shelfItems
});
// If using controller methods in an input's onchange or onclick instance,
@ -44,6 +26,10 @@ export class ShelvesController extends ViewController {
return typeof this.appState.query.shelf !== 'undefined' ? parseInt(this.appState.query.shelf) : null;
}
get targetDomain () {
return typeof this.appState.query.domain !== 'undefined' ? parseInt(this.appState.query.domain) : null;
}
getUserShelves () {
return fetch('/api/shelves/get').then(response => response.json()).then(shelves => {
this.state.myShelves = shelves;
@ -51,7 +37,8 @@ export class ShelvesController extends ViewController {
}
getTargetShelf () {
return fetch('/api/shelf/get/' + this.targetShelf).then(response => response.json()).then(shelf => {
const target = this.targetShelf + (this.targetDomain !== null ? `/${this.targetDomain}` : '');
return fetch('/api/shelf/get/' + target).then(response => response.json()).then(shelf => {
this.state.loadedShelves[this.targetShelf] = shelf;
});
}

View File

@ -45,6 +45,19 @@ export const shelfView = (shelvesController, emit) => {
];
}
if (typeof shelf.error !== 'undefined') {
return [
html`<section>
<h2>${__('global.error')}</h2>
<article class="card">
<header>
${shelf.message}
</header>
</article>
</section>`,
];
}
const shelfItems = shelf !== null ? shelf.shelfItems : [];
// Returning an array in a view allows non-shared parent HTML elements.
@ -54,7 +67,7 @@ export const shelfView = (shelvesController, emit) => {
<div class="flex two">
<div class="two-third three-fourth-700">
<h2>${shelf.name}</h2>
<span>${__('shelves.owned_by')} <a href="/profile?user=${shelf.user.userName}" title=${shelf.user.userName}>${shelf.user.displayName}</a></span>
<span>${__('shelves.owned_by')} ${shelf.user === null ? __('shelves.you') : `<a href="/profile?user=${shelf.user.handle}" title=${shelf.user.handle}>${shelf.user.name}</a>`}</span>
</div>
<div class="third sixth-700">
<button class="pseudo" onclick=${() => {

View File

@ -111,13 +111,15 @@ class ShelfController {
}
async getShelfById(shelfId) {
if (isNaN(parse(shelfId))) {
if (isNaN(parseInt(shelfId))) {
return {
error: 'Shelf ID Provided is not a number.',
};
}
const shelf = await this.shelfModel.findByPk(shelfId);
return this.getFakeShelf();
const shelf = await this.shelfModel.findByPk(shelfId);
if (shelf === null) {
return {
error: `Shelf with ID ${shelfId} not found.`,
@ -128,9 +130,75 @@ class ShelfController {
return shelf;
}
getFakeShelf () {
const faker = require('faker');
const isOwnShelf = faker.random.boolean();
const fakeName = faker.random.boolean()
? faker.fake('{{name.firstName}} {{name.lastName}}')
: faker.fake('{{hacker.adjective}}{{hacker.noun}}');
const shelf = {
id: faker.random.number(),
userId: faker.random.number(),
user: isOwnShelf ? null : {
name: fakeName,
handle: faker.fake('@{{internet.userName}}@{{internet.domainName}}'),
},
name: faker.fake('{{hacker.ingverb}} {{hacker.noun}}'),
isPublic: Math.random() < 0.9,
updatedAt: faker.date.past(),
shelfItems: [],
};
const numberOfShelfItems = faker.random.number(50);
for (let i = 0; i < numberOfShelfItems; i++) {
const source = {
source: faker.internet.domainWord(),
sourceId: faker.random.uuid(),
};
const shelfItem = {
name: faker.lorem.words().split(' ').map(word => (word[0].toUpperCase() + word.substr(1))).join(' '),
author: faker.fake('a work by {{name.firstName}} {{name.lastName}}'),
source,
coverURL: faker.image.imageUrl(),
coverEdition: `img_${source.sourceId}`,
rating: faker.random.number(5),
review: faker.random.boolean()
? null
: faker.lorem.paragraph(),
}
shelf.shelfItems.push(shelfItem);
}
if (isOwnShelf) {
shelf.createdAt = faker.date.past(undefined, shelf.updatedAt);
shelf.isDeletable = true;
}
return shelf;
}
async userCanViewShelf (user, shelf) {
// This needs work when permissions are added.
return user.id === shelf.userId || shelf.isPublic;
const userOwnsShelf = typeof user !== 'undefined' && user.id === shelf.userId;
console.log('owned?', userOwnsShelf);
console.log('isPublic?', shelf.isPublic);
return userOwnsShelf || shelf.isPublic;
}
async scrubShelfData (shelf, user) {
const userOwnsShelf = user.id === shelf.userId;
const shelfUser = userOwnsShelf ? null : shelf.getUser();
let userData = {};
if (shelfUser !== null) {
userData.name = shelfUser.displayName;
userData.handle = `@${shelfUser.username}`;
}
const shelfData = {
name: shelf.name,
user: userOwnsShelf ? null : shelf.getUser(),
}
}
}

View File

@ -27,8 +27,8 @@ async function routes(fastify, options) {
});
});
fastify.get('/api/shelf/get', async (request, reply) => {
if (typeof request.body.shelf === 'undefined') {
fastify.get('/api/shelf/get/:shelfId/:domain', async (request, reply) => {
if (typeof request.params.shelfId === 'undefined') {
return reply.code(400).send({
error: true,
message: 'api.shelf.get.missing_id',
@ -36,13 +36,15 @@ async function routes(fastify, options) {
}
const shelfController = new ShelfController(fastify.models.Shelf, fastify.models.ShelfItem);
const shelf = shelfController.getShelfById(request.body.shelf);
const shelf = await shelfController.getShelfById(request.params.shelfId);
if (typeof shelf.error !== 'undefined') {
shelf.message = 'api.shelf.get.nonexistent_shelf';
return reply.code(400).send(shelf);
}
const userCanViewShelf = shelfController.userCanViewShelf(request.user, shelf);
const userCanViewShelf = await shelfController.userCanViewShelf(request.user, shelf);
console.log('can view?', userCanViewShelf);
if (userCanViewShelf !== true) {
return reply.code(400).send({
error: true,
@ -50,6 +52,8 @@ async function routes(fastify, options) {
});
}
// const shelfData = await shelfController.scrubShelfData(shelf, request.user);
// return reply.send(shelfData);
return reply.send(shelf);
});