From a9101c201f031760f86e80f48e77e5cdd0ee2d1e Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 30 Dec 2021 21:43:35 -0500 Subject: [PATCH] list categories for model --- fakeyou/src/common/ApiConfig.ts | 4 ++ .../api/category/ListCategoriesForTtsModel.ts | 67 +++++++++++++++++++ .../TtsEditCategoriesPage.tsx | 45 ++++++++++++- 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 fakeyou/src/v2/api/category/ListCategoriesForTtsModel.ts diff --git a/fakeyou/src/common/ApiConfig.ts b/fakeyou/src/common/ApiConfig.ts index 99284e8a07..37368cf561 100644 --- a/fakeyou/src/common/ApiConfig.ts +++ b/fakeyou/src/common/ApiConfig.ts @@ -250,6 +250,10 @@ class ApiConfig { return `${this.getScheme()}://${this.getNewApiHost()}/category/create`; } + listCategoriesForTtsModel(ttsModelToken: string) : string { + return `${this.getScheme()}://${this.getNewApiHost()}/category/assignments/tts/${ttsModelToken}`; + } + firehoseEvents() : string { return `${this.getScheme()}://${this.getNewApiHost()}/events`; } diff --git a/fakeyou/src/v2/api/category/ListCategoriesForTtsModel.ts b/fakeyou/src/v2/api/category/ListCategoriesForTtsModel.ts new file mode 100644 index 0000000000..e6e86e1ca5 --- /dev/null +++ b/fakeyou/src/v2/api/category/ListCategoriesForTtsModel.ts @@ -0,0 +1,67 @@ +import { ApiConfig } from "../../../common/ApiConfig"; + +export interface ListCategoriesForTtsModelSuccessResponse { + success: boolean, + categories: Array +} + +export interface TtsModelCategory { + 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, + + is_mod_approved?: boolean, + + category_created_at: Date, + category_updated_at: Date, + category_deleted_at?: Date, +} + +export interface ListCategoriesForTtsModelErrorResponse { + success: boolean, +} + +type ListCategoriesForTtsModelResponse = ListCategoriesForTtsModelSuccessResponse | ListCategoriesForTtsModelErrorResponse; + +export function ListCategoriesForTtsModelIsOk(response: ListCategoriesForTtsModelResponse): response is ListCategoriesForTtsModelSuccessResponse { + return response?.success === true; +} + +export function ListCategoriesForTtsModelIsError(response: ListCategoriesForTtsModelResponse): response is ListCategoriesForTtsModelErrorResponse { + return response?.success === false; +} + +export async function ListCategoriesForTtsModel(ttsModelToken: string) : Promise +{ + const endpoint = new ApiConfig().listCategoriesForTtsModel(ttsModelToken); + + 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 }; + }); +} diff --git a/fakeyou/src/v2/view/tts/tts_edit_categories/TtsEditCategoriesPage.tsx b/fakeyou/src/v2/view/tts/tts_edit_categories/TtsEditCategoriesPage.tsx index a9d7653ae9..138365aa64 100644 --- a/fakeyou/src/v2/view/tts/tts_edit_categories/TtsEditCategoriesPage.tsx +++ b/fakeyou/src/v2/view/tts/tts_edit_categories/TtsEditCategoriesPage.tsx @@ -4,6 +4,7 @@ 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'; interface Props { sessionWrapper: SessionWrapper, @@ -17,6 +18,8 @@ function TtsEditCategoriesPage(props: Props) { const [ttsModel, setTtsModel] = useState(undefined); const [notFoundState, setNotFoundState] = useState(false); + const [assignedCategories, setAssignedCategories] = useState([]); + const getModel = useCallback(async (token) => { const model = await GetTtsModel(token); @@ -31,8 +34,19 @@ function TtsEditCategoriesPage(props: Props) { } }, []); + const listCategoriesForModel = useCallback(async (token) => { + const categoryList = await ListCategoriesForTtsModel(token); + + if (ListCategoriesForTtsModelIsOk(categoryList)) { + setAssignedCategories(categoryList.categories); + } else if (ListCategoriesForTtsModelIsError(categoryList)) { + // TODO: Improve + } + }, []); + useEffect(() => { getModel(token); + listCategoriesForModel(token); }, [token, getModel]); @@ -48,17 +62,44 @@ function TtsEditCategoriesPage(props: Props) { const modelLink = FrontendUrlConfig.ttsModelPage(token); + let currentCategoriesList = ( + <> +

No categories yet...

+ + ); + + if (assignedCategories.length !== 0) { + currentCategoriesList = ( + <> + {assignedCategories.map(category => { + return ( +
  • + {category.name} [edit category] [remove] +
  • + ); + })} + + ); + } + return (
    -

    Edit TTS Categories

    +

    Edit TTS Categories

    +

    Model: {ttsModel.title}

    +

    Current categories

    +
      {currentCategoriesList}
    + +

    Add new category

    +

    - Model: {ttsModel.title} +

    +
    ) }