mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-16 06:42:14 +00:00
Add categories (messy)
This commit is contained in:
1 parent
a9101c201f
commit
f616016a21
5 files changed
+198
-15
No files matched your search
@@ -250,7 +250,15 @@ class ApiConfig {
|
||||
return `${this.getScheme()}://${this.getNewApiHost()}/category/create`;
|
||||
}
|
||||
|
||||
listCategoriesForTtsModel(ttsModelToken: string) : string {
|
||||
assignTtsCategory() : string {
|
||||
return `${this.getScheme()}://${this.getNewApiHost()}/category/assign/tts`;
|
||||
}
|
||||
|
||||
listTtsCategories() : string {
|
||||
return `${this.getScheme()}://${this.getNewApiHost()}/category/list/tts`;
|
||||
}
|
||||
|
||||
listTtsCategoriesForModel(ttsModelToken: string) : string {
|
||||
return `${this.getScheme()}://${this.getNewApiHost()}/category/assignments/tts/${ttsModelToken}`;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { ApiConfig } from "../../../common/ApiConfig";
|
||||
|
||||
export interface AssignTtsCategoryRequest {
|
||||
category_token: string,
|
||||
tts_model_token: string,
|
||||
// Whether to add or delete the assignment
|
||||
assign: boolean,
|
||||
}
|
||||
|
||||
export interface AssignTtsCategorySuccessResponse {
|
||||
success: boolean,
|
||||
}
|
||||
|
||||
export interface AssignTtsCategoryErrorResponse {
|
||||
success: boolean,
|
||||
}
|
||||
|
||||
type AssignTtsCategoryResponse = AssignTtsCategorySuccessResponse | AssignTtsCategoryErrorResponse;
|
||||
|
||||
export function AssignTtsCategoryIsOk(response: AssignTtsCategoryResponse): response is AssignTtsCategorySuccessResponse {
|
||||
return response?.success === true;
|
||||
}
|
||||
|
||||
export function AssignTtsCategoryIsError(response: AssignTtsCategoryResponse): response is AssignTtsCategoryErrorResponse {
|
||||
return response?.success === false;
|
||||
}
|
||||
|
||||
export async function AssignTtsCategory(request: AssignTtsCategoryRequest) : Promise<AssignTtsCategoryResponse>
|
||||
{
|
||||
const endpoint = new ApiConfig().assignTtsCategory();
|
||||
|
||||
return await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
credentials: 'include',
|
||||
body: JSON.stringify(request),
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(res => {
|
||||
if (!res) {
|
||||
return { success : false }; // TODO: This loses error semantics and is deprecated
|
||||
}
|
||||
|
||||
if (res && 'success' in res) {
|
||||
return res;
|
||||
} else {
|
||||
return { success : false };
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
return { success : false };
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { ApiConfig } from "../../../common/ApiConfig";
|
||||
|
||||
export interface ListTtsCategoriesSuccessResponse {
|
||||
success: boolean,
|
||||
categories: Array<TtsCategory>
|
||||
}
|
||||
|
||||
export interface TtsCategory {
|
||||
category_token: string,
|
||||
model_type: string,
|
||||
maybe_super_category_token?: string,
|
||||
|
||||
can_directly_have_models: boolean,
|
||||
can_have_subcategories: boolean,
|
||||
can_only_mods_apply: boolean,
|
||||
|
||||
name: string,
|
||||
maybe_dropdown_name?: string,
|
||||
}
|
||||
|
||||
export interface ListTtsCategoriesErrorResponse {
|
||||
success: boolean,
|
||||
}
|
||||
|
||||
type ListTtsCategoriesResponse = ListTtsCategoriesSuccessResponse | ListTtsCategoriesErrorResponse;
|
||||
|
||||
export function ListTtsCategoriesIsOk(response: ListTtsCategoriesResponse): response is ListTtsCategoriesSuccessResponse {
|
||||
return response?.success === true;
|
||||
}
|
||||
|
||||
export function ListTtsCategoriesIsError(response: ListTtsCategoriesResponse): response is ListTtsCategoriesErrorResponse {
|
||||
return response?.success === false;
|
||||
}
|
||||
|
||||
export async function ListTtsCategories() : Promise<ListTtsCategoriesResponse>
|
||||
{
|
||||
const endpoint = new ApiConfig().listTtsCategories();
|
||||
|
||||
return await fetch(endpoint, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
credentials: 'include',
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(res => {
|
||||
if (!res) {
|
||||
return { success : false }; // TODO: This loses error semantics and is deprecated
|
||||
}
|
||||
|
||||
if (res && 'success' in res) {
|
||||
return res;
|
||||
} else {
|
||||
return { success : false };
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
return { success : false };
|
||||
});
|
||||
}
|
||||
+7
-7
@@ -1,6 +1,6 @@
|
||||
import { ApiConfig } from "../../../common/ApiConfig";
|
||||
|
||||
export interface ListCategoriesForTtsModelSuccessResponse {
|
||||
export interface ListTtsCategoriesForModelSuccessResponse {
|
||||
success: boolean,
|
||||
categories: Array<TtsModelCategory>
|
||||
}
|
||||
@@ -24,23 +24,23 @@ export interface TtsModelCategory {
|
||||
category_deleted_at?: Date,
|
||||
}
|
||||
|
||||
export interface ListCategoriesForTtsModelErrorResponse {
|
||||
export interface ListTtsCategoriesForModelErrorResponse {
|
||||
success: boolean,
|
||||
}
|
||||
|
||||
type ListCategoriesForTtsModelResponse = ListCategoriesForTtsModelSuccessResponse | ListCategoriesForTtsModelErrorResponse;
|
||||
type ListTtsCategoriesForModelResponse = ListTtsCategoriesForModelSuccessResponse | ListTtsCategoriesForModelErrorResponse;
|
||||
|
||||
export function ListCategoriesForTtsModelIsOk(response: ListCategoriesForTtsModelResponse): response is ListCategoriesForTtsModelSuccessResponse {
|
||||
export function ListTtsCategoriesForModelIsOk(response: ListTtsCategoriesForModelResponse): response is ListTtsCategoriesForModelSuccessResponse {
|
||||
return response?.success === true;
|
||||
}
|
||||
|
||||
export function ListCategoriesForTtsModelIsError(response: ListCategoriesForTtsModelResponse): response is ListCategoriesForTtsModelErrorResponse {
|
||||
export function ListTtsCategoriesForModelIsError(response: ListTtsCategoriesForModelResponse): response is ListTtsCategoriesForModelErrorResponse {
|
||||
return response?.success === false;
|
||||
}
|
||||
|
||||
export async function ListCategoriesForTtsModel(ttsModelToken: string) : Promise<ListCategoriesForTtsModelResponse>
|
||||
export async function ListTtsCategoriesForModel(ttsModelToken: string) : Promise<ListTtsCategoriesForModelResponse>
|
||||
{
|
||||
const endpoint = new ApiConfig().listCategoriesForTtsModel(ttsModelToken);
|
||||
const endpoint = new ApiConfig().listTtsCategoriesForModel(ttsModelToken);
|
||||
|
||||
return await fetch(endpoint, {
|
||||
method: 'GET',
|
||||
@@ -4,7 +4,9 @@ import { useParams, useHistory } from 'react-router-dom';
|
||||
import { FrontendUrlConfig } from '../../../../common/FrontendUrlConfig';
|
||||
import { BackLink } from '../../_common/BackLink';
|
||||
import { GetTtsModel, GetTtsModelIsErr, GetTtsModelIsOk, TtsModel, TtsModelLookupError } from '../../../api/tts/GetTtsModel';
|
||||
import { ListCategoriesForTtsModel, ListCategoriesForTtsModelIsError, ListCategoriesForTtsModelIsOk, TtsModelCategory } from '../../../api/category/ListCategoriesForTtsModel';
|
||||
import { ListTtsCategoriesForModel, ListTtsCategoriesForModelIsError, ListTtsCategoriesForModelIsOk, TtsModelCategory } from '../../../api/category/ListTtsCategoriesForModel';
|
||||
import { ListTtsCategories, ListTtsCategoriesIsError, ListTtsCategoriesIsOk, TtsCategory } from '../../../api/category/ListTtsCategories';
|
||||
import { AssignTtsCategory, AssignTtsCategoryIsError, AssignTtsCategoryIsOk } from '../../../api/category/AssignTtsCategory';
|
||||
|
||||
interface Props {
|
||||
sessionWrapper: SessionWrapper,
|
||||
@@ -18,6 +20,7 @@ function TtsEditCategoriesPage(props: Props) {
|
||||
const [ttsModel, setTtsModel] = useState<TtsModel|undefined>(undefined);
|
||||
const [notFoundState, setNotFoundState] = useState<boolean>(false);
|
||||
|
||||
const [allTtsCategories, setAllTtsCategories] = useState<TtsCategory[]>([]);
|
||||
const [assignedCategories, setAssignedCategories] = useState<TtsModelCategory[]>([]);
|
||||
|
||||
const getModel = useCallback(async (token) => {
|
||||
@@ -34,20 +37,31 @@ function TtsEditCategoriesPage(props: Props) {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const listCategoriesForModel = useCallback(async (token) => {
|
||||
const categoryList = await ListCategoriesForTtsModel(token);
|
||||
const listTtsCategories = useCallback(async () => {
|
||||
const categoryList = await ListTtsCategories();
|
||||
|
||||
if (ListCategoriesForTtsModelIsOk(categoryList)) {
|
||||
if (ListTtsCategoriesIsOk(categoryList)) {
|
||||
setAllTtsCategories(categoryList.categories);
|
||||
} else if (ListTtsCategoriesIsError(categoryList)) {
|
||||
// TODO: Improve
|
||||
}
|
||||
}, []);
|
||||
|
||||
const listTtsCategoriesForModel = useCallback(async (token) => {
|
||||
const categoryList = await ListTtsCategoriesForModel(token);
|
||||
|
||||
if (ListTtsCategoriesForModelIsOk(categoryList)) {
|
||||
setAssignedCategories(categoryList.categories);
|
||||
} else if (ListCategoriesForTtsModelIsError(categoryList)) {
|
||||
} else if (ListTtsCategoriesForModelIsError(categoryList)) {
|
||||
// TODO: Improve
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
getModel(token);
|
||||
listCategoriesForModel(token);
|
||||
}, [token, getModel]);
|
||||
listTtsCategories();
|
||||
listTtsCategoriesForModel(token);
|
||||
}, [token, getModel, listTtsCategories, listTtsCategoriesForModel]);
|
||||
|
||||
|
||||
if (notFoundState) {
|
||||
@@ -60,6 +74,28 @@ function TtsEditCategoriesPage(props: Props) {
|
||||
return <div />
|
||||
}
|
||||
|
||||
const handleAddCategory = async (ev: React.FormEvent<HTMLSelectElement>) => {
|
||||
const categoryToken = (ev.target as HTMLSelectElement).value;
|
||||
|
||||
if (categoryToken === '') {
|
||||
return; // Default dropdown option is a no-op
|
||||
}
|
||||
|
||||
const assignRequest = {
|
||||
category_token: categoryToken,
|
||||
tts_model_token: token,
|
||||
assign: true,
|
||||
};
|
||||
|
||||
const result = await AssignTtsCategory(assignRequest);
|
||||
|
||||
if (AssignTtsCategoryIsOk(result)) {
|
||||
listTtsCategoriesForModel(token); // Reload
|
||||
} else if (AssignTtsCategoryIsError(result)) {
|
||||
// TODO: Improve
|
||||
}
|
||||
}
|
||||
|
||||
const modelLink = FrontendUrlConfig.ttsModelPage(token);
|
||||
|
||||
let currentCategoriesList = (
|
||||
@@ -82,6 +118,17 @@ function TtsEditCategoriesPage(props: Props) {
|
||||
);
|
||||
}
|
||||
|
||||
const addCategoryOptions = allTtsCategories.filter(category => {
|
||||
//if (category.category_token)
|
||||
return true;
|
||||
}).map(category => {
|
||||
return (
|
||||
<>
|
||||
<option value={category.category_token}>{category.name}</option>
|
||||
</>
|
||||
)
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="content">
|
||||
<h1 className="title is-1">Edit TTS Categories</h1>
|
||||
@@ -95,6 +142,17 @@ function TtsEditCategoriesPage(props: Props) {
|
||||
<ul>{currentCategoriesList}</ul>
|
||||
|
||||
<h3 className="is-3"> Add new category </h3>
|
||||
|
||||
<div className="field">
|
||||
<div className="control">
|
||||
<div className="select is-info">
|
||||
<select onChange={handleAddCategory}>
|
||||
<option value="">Select category to add...</option>
|
||||
{addCategoryOptions}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
<BackLink link={modelLink} text="Back to model" />
|
||||
|
||||
Reference in new issue
Block a user