From 32bf87176bdf211389dbb9fd6c0b99f7ec81ad76 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 18 Aug 2021 18:44:46 -0400 Subject: [PATCH] Reusable API functions (GetW2lTemplate) --- vocodes/src/v2/api/w2l/GetW2lTemplate.ts | 57 +++++++++++++++++++ .../w2l_template_view/W2lTemplateViewFc.tsx | 56 ++---------------- 2 files changed, 63 insertions(+), 50 deletions(-) create mode 100644 vocodes/src/v2/api/w2l/GetW2lTemplate.ts diff --git a/vocodes/src/v2/api/w2l/GetW2lTemplate.ts b/vocodes/src/v2/api/w2l/GetW2lTemplate.ts new file mode 100644 index 0000000000..a921e51ed8 --- /dev/null +++ b/vocodes/src/v2/api/w2l/GetW2lTemplate.ts @@ -0,0 +1,57 @@ +import { ApiConfig } from "../../../common/ApiConfig"; + +export interface W2lTemplateViewResponsePayload { + success: boolean, + template: W2lTemplate, +} + +export interface W2lTemplate { + template_token: string, + template_type: string, + creator_user_token: string, + creator_username: string, + creator_display_name: string, + updatable_slug: string, + title: string, + frame_width: number, + frame_height: number, + duration_millis: number, + maybe_image_object_name: string, + maybe_video_object_name: string, + creator_set_visibility: string, + is_public_listing_approved: boolean | null, + created_at: string, + updated_at: string, + maybe_moderator_fields: W2lTemplateModeratorFields | null | undefined, +} + +export interface W2lTemplateModeratorFields { + creator_ip_address_creation: string, + creator_ip_address_last_update: string, + mod_deleted_at: string | undefined | null, + user_deleted_at: string | undefined | null, +} + +export async function GetW2lTemplate(templateToken: string) : Promise { + const endpoint = new ApiConfig().viewW2lTemplate(templateToken); + + return await fetch(endpoint, { + method: 'GET', + headers: { + 'Accept': 'application/json', + }, + credentials: 'include', + }) + .then(res => res.json()) + .then(res => { + const templatesResponse : W2lTemplateViewResponsePayload = res; + if (!templatesResponse.success) { + return; + } + + return templatesResponse?.template; + }) + .catch(e => { + return undefined; + }); +} diff --git a/vocodes/src/v2/w2l/w2l_template_view/W2lTemplateViewFc.tsx b/vocodes/src/v2/w2l/w2l_template_view/W2lTemplateViewFc.tsx index 1a92f416fc..f3032d9d3e 100644 --- a/vocodes/src/v2/w2l/w2l_template_view/W2lTemplateViewFc.tsx +++ b/vocodes/src/v2/w2l/w2l_template_view/W2lTemplateViewFc.tsx @@ -12,44 +12,13 @@ import { UploadIcon } from '../../../icons/UploadIcon'; import { VisibleIconFc } from '../../../icons/VisibleIconFc'; import { HiddenIconFc } from '../../../icons/HiddenIconFc'; import { FrontendUrlConfig } from '../../../common/FrontendUrlConfig'; - -interface W2lTemplateViewResponsePayload { - success: boolean, - template: W2lTemplate, -} +import { GetW2lTemplate, W2lTemplate } from '../../api/w2l/GetW2lTemplate'; interface W2lTemplateUseCountResponsePayload { success: boolean, count: number | null | undefined, } -interface W2lTemplate { - template_token: string, - template_type: string, - creator_user_token: string, - creator_username: string, - creator_display_name: string, - updatable_slug: string, - title: string, - frame_width: number, - frame_height: number, - duration_millis: number, - maybe_image_object_name: string, - maybe_video_object_name: string, - creator_set_visibility: string, - is_public_listing_approved: boolean | null, - created_at: string, - updated_at: string, - maybe_moderator_fields: W2lTemplateModeratorFields | null | undefined, -} - -interface W2lTemplateModeratorFields { - creator_ip_address_creation: string, - creator_ip_address_last_update: string, - mod_deleted_at: string | undefined | null, - user_deleted_at: string | undefined | null, -} - interface EnqueueJobResponsePayload { success: boolean, inference_job_token?: string, @@ -76,34 +45,21 @@ function W2lTemplateViewFc(props: Props) { // Moderation const [modApprovedFormValue, setModApprovedFormValue] = useState(true); - const getTemplate = useCallback((templateSlug: string) => { - const api = new ApiConfig(); - const endpointUrl = api.viewW2l(templateSlug); + const getTemplate = useCallback(async (templateSlug: string) => { + const templateResponse = await GetW2lTemplate(templateSlug); - fetch(endpointUrl, { - method: 'GET', - headers: { - 'Accept': 'application/json', - }, - credentials: 'include', - }) - .then(res => res.json()) - .then(res => { - const templatesResponse : W2lTemplateViewResponsePayload = res; - if (!templatesResponse.success) { + if (!templateResponse) { return; } - setW2lTemplate(templatesResponse.template) + setW2lTemplate(templateResponse) - let modApprovalState = templatesResponse?.template?.is_public_listing_approved; + let modApprovalState = templateResponse?.is_public_listing_approved; if (modApprovedFormValue === undefined || modApprovalState === null) { modApprovalState = true; } setModApprovedFormValue(modApprovalState); - }) - .catch(e => {}); }, [modApprovedFormValue]);