Added favorites button to actor page.

This commit is contained in:
DebaucheryLibrarian
2021-03-15 03:30:47 +01:00
parent e371e9725a
commit 77b40817f2
27 changed files with 466 additions and 169 deletions

View File

@@ -24,6 +24,8 @@ function initActorActions(store, router) {
const { actor } = await graphql(`
query Actor(
$actorId: Int!
$userId: Int,
$hasAuth: Boolean!,
$limit:Int = 10,
$offset:Int = 0,
$after:Datetime = "1900-01-01",
@@ -236,6 +238,21 @@ function initActorActions(store, router) {
}
totalCount
}
stashes: stashesActors(
filter: {
stash: {
userId: {
equalTo: $userId
}
}
}
) @include(if: $hasAuth) {
stash {
id
name
slug
}
}
}
}
`, {
@@ -253,6 +270,8 @@ function initActorActions(store, router) {
includedEntities: getIncludedEntities(router),
includedActors: getIncludedActors(router),
mode,
hasAuth: !!store.state.auth.user,
userId: store.state.auth.user?.id,
});
if (!actor) {

View File

@@ -10,10 +10,16 @@ async function get(endpoint, query = {}) {
credentials: 'same-origin',
});
if (res.ok) {
const contentTypes = res.headers.get('content-type');
if (res.ok && contentTypes?.includes('application/json')) {
return res.json();
}
if (res.ok) {
return null;
}
const errorMsg = await res.text();
throw new Error(errorMsg);
@@ -30,10 +36,16 @@ async function post(endpoint, data) {
body: JSON.stringify(data),
});
if (res.ok) {
const contentTypes = res.headers.get('content-type');
if (res.ok && contentTypes?.includes('application/json')) {
return res.json();
}
if (res.ok) {
return null;
}
const errorMsg = await res.text();
throw new Error(errorMsg);

View File

@@ -2,11 +2,16 @@ import { get, post, del } from '../api';
function initAuthActions(_store, _router) {
async function fetchMe({ commit }) {
const user = await get('/session');
try {
const user = await get('/session');
commit('setUser', user);
commit('setUser', user);
return user;
return user;
} catch (error) {
// continue as guest
return null;
}
}
async function login({ commit }, credentials) {

View File

@@ -1,11 +1,13 @@
import state from './state';
import mutations from './mutations';
import getters from './getters';
import actions from './actions';
function initAuthStore(store, router) {
return {
state,
mutations,
getters,
actions: actions(store, router),
};
}

View File

@@ -0,0 +1,8 @@
function favoritesStash(state) {
return state.user.stashes.find(stash => stash.slug === 'favorites');
}
module.exports = {
favoritesStash,
favorites: favoritesStash,
};

View File

@@ -56,6 +56,8 @@ function curateActor(actor, release) {
curatedActor.aliasFor = curateActor(curatedActor.aliasFor);
}
curatedActor.stashes = actor.stashes?.map(stash => stash.stash || stash) || [];
return curatedActor;
}
@@ -126,9 +128,7 @@ function curateTag(tag) {
}
function curateStash(stash) {
const curatedStash = {
...stash,
};
const curatedStash = stash;
if (stash.scenes) {
curatedStash.scenes = stash.scenes.map(item => ({
@@ -147,10 +147,21 @@ function curateStash(stash) {
return curatedStash;
}
function curateUser(user) {
const curatedUser = user;
if (user.stashes) {
curatedUser.stashes = user.stashes.map(stash => curateStash(stash));
}
return curatedUser;
}
export {
curateActor,
curateEntity,
curateRelease,
curateTag,
curateStash,
curateUser,
};

View File

@@ -64,8 +64,8 @@ async function init() {
return `${path}/${filename}`;
}
initUiObservers(store, router);
initAuthObservers(store, router);
await initAuthObservers(store, router);
await initUiObservers(store, router);
if (window.env.sfw) {
store.dispatch('setSfw', true);

View File

@@ -1,101 +1,17 @@
import { graphql } from '../api';
import { curateStash } from '../curate';
import { post, del } from '../api';
function initStashesActions(_store, _router) {
async function fetchUserStashes(context, userId) {
const { stashes } = await graphql(`
query Stashes(
$userId: Int!
) {
stashes(
filter: {
userId: {
equalTo: $userId
}
}
) {
id
name
actors: stashesActors {
comment
actor {
id
name
slug
gender
age
ageFromBirth
dateOfBirth
birthCity
birthState
birthCountry: countryByBirthCountryAlpha2 {
alpha2
name
alias
}
avatar: avatarMedia {
id
path
thumbnail
lazy
}
}
}
scenes: stashesScenes {
comment
scene {
id
title
slug
url
date
actors: releasesActors {
actor {
id
name
slug
}
}
tags: releasesTags {
tag {
id
name
slug
}
}
entity {
id
name
slug
independent
parent {
id
name
slug
independent
}
}
poster: releasesPosterByReleaseId {
media {
path
thumbnail
lazy
isS3
}
}
}
}
}
}
`, {
userId,
});
async function stashActor(context, { actorId, stashId }) {
await post(`/stashes/${stashId}/actors`, { actorId });
}
return stashes.map(stash => curateStash(stash));
async function unstashActor(context, { actorId, stashId }) {
await del(`/stashes/${stashId}/actors/${actorId}`);
}
return {
fetchUserStashes,
stashActor,
unstashActor,
};
}

View File

@@ -1,8 +1,95 @@
import { get } from '../api';
import { graphql } from '../api';
function initUsersActions(_store, _router) {
async function fetchUser(context, username) {
const user = await get(`/users/${username}`);
const { user } = await graphql(`
query User(
$username: String!
) {
user: userByUsername(username: $username) {
id
role
username
stashes {
id
name
slug
public
actors: stashesActors {
comment
actor {
id
name
slug
gender
age
ageFromBirth
dateOfBirth
birthCity
birthState
birthCountry: countryByBirthCountryAlpha2 {
alpha2
name
alias
}
avatar: avatarMedia {
id
path
thumbnail
lazy
}
}
}
scenes: stashesScenes {
comment
scene {
id
title
slug
url
date
actors: releasesActors {
actor {
id
name
slug
}
}
tags: releasesTags {
tag {
id
name
slug
}
}
entity {
id
name
slug
independent
parent {
id
name
slug
independent
}
}
poster: releasesPosterByReleaseId {
media {
path
thumbnail
lazy
isS3
}
}
}
}
}
}
}
`, {
username,
});
return user;
}