From d75a930381908963e4c2a4498549b105f6d85102 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 19 Aug 2021 02:52:17 -0400 Subject: [PATCH] Template approval works again --- vocodes/src/common/FrontendUrlConfig.ts | 4 + vocodes/src/v2/NewVocodesContainer.tsx | 7 + .../W2lTemplateApproveFc.tsx | 181 ++++++++++++++++++ .../w2l_template_view/W2lTemplateViewFc.tsx | 26 ++- 4 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 vocodes/src/v2/w2l/w2l_template_approve/W2lTemplateApproveFc.tsx diff --git a/vocodes/src/common/FrontendUrlConfig.ts b/vocodes/src/common/FrontendUrlConfig.ts index 67605c7d77..2927838fcd 100644 --- a/vocodes/src/common/FrontendUrlConfig.ts +++ b/vocodes/src/common/FrontendUrlConfig.ts @@ -46,6 +46,10 @@ class FrontendUrlConfig { return `/w2l/${templateToken}/edit`; } + static w2lTemplateApprovalPage(templateToken: string) : string { + return `/w2l/${templateToken}/approval`; + } + static w2lTemplateDeletePage(templateToken: string) : string { return `/w2l/${templateToken}/delete`; } diff --git a/vocodes/src/v2/NewVocodesContainer.tsx b/vocodes/src/v2/NewVocodesContainer.tsx index d54030d22b..e0b0055a86 100644 --- a/vocodes/src/v2/NewVocodesContainer.tsx +++ b/vocodes/src/v2/NewVocodesContainer.tsx @@ -35,6 +35,7 @@ import { W2lResultEditFc } from './w2l/w2l_result_edit/W2lResultEditFc'; import { W2lTemplateDeleteFc } from './w2l/w2l_template_delete/W2lTemplateDeleteFc'; import { W2lTemplateEditFc } from './w2l/w2l_template_edit/W2lTemplateEditFc'; import { W2lResultDeleteFc } from './w2l/w2l_result_delete/W2lResultDeleteFc'; +import { W2lTemplateApproveFc } from './w2l/w2l_template_approve/W2lTemplateApproveFc'; interface Props { sessionWrapper: SessionWrapper, @@ -184,6 +185,12 @@ class NewVocodesContainer extends React.Component { /> + + + + (undefined); + const [w2lTemplateUseCount, setW2lTemplateUseCount] = useState(undefined); + const [approvedState, setApprovedState] = useState(DEFAULT_APPROVED_STATE); + + const getTemplate = useCallback(async (templateToken) => { + const template = await GetW2lTemplate(templateToken); + if (template) { + setW2lTemplate(template) + const currentlyApproved = template?.is_public_listing_approved; + setApprovedState(currentlyApproved); + } + }, []); + + const getTemplateUseCount = useCallback(async (templateToken) => { + const count = await GetW2lTemplateUseCount(templateToken); + setW2lTemplateUseCount(count || 0) + }, []); + + const templateLink = FrontendUrlConfig.w2lTemplatePage(templateToken); + + useEffect(() => { + getTemplate(templateToken); + getTemplateUseCount(templateToken); + }, [templateToken, getTemplate, getTemplateUseCount]); + + const handleModApprovalChange= (ev: React.FormEvent) => { + const value = (ev.target as HTMLSelectElement).value; + const updatedValue = value === "true" ? true : false; + setApprovedState(updatedValue) + }; + + const handleApproveFormSubmit = (ev: React.FormEvent) : boolean => { + ev.preventDefault(); + + const endpointUri = new ApiConfig().moderateW2l(templateToken); + + const request = { + is_approved: approvedState || false, + } + + fetch(endpointUri, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify(request), + }) + .then(res => res.json()) + .then(res => { + if (res.success) { + history.push(templateLink); + } + }) + .catch(e => { + }); + return false; + } + + let creatorLink = ; + + if (!!w2lTemplate?.creator_display_name) { + const creatorUrl = FrontendUrlConfig.userProfilePage(w2lTemplate?.creator_display_name); + creatorLink = ( + {w2lTemplate?.creator_display_name} + ); + } + + const currentlyApproved = w2lTemplate?.is_public_listing_approved; + + const h1Title = currentlyApproved ? "Unapprove Template?" : "Approve Template?"; + + const buttonTitle = currentlyApproved ? "Confirm Unapprove" : "Confirm Approve"; + + const buttonCss = currentlyApproved ? + "button is-warning is-large is-fullwidth" : + "button is-danger is-large is-fullwidth"; + + const formLabel = currentlyApproved ? + "Recover the W2L Template (makes it visible again)" : + "Approve W2L Template (hides from everyone but mods)"; + + let humanUseCount : string | number = 'Fetching...'; + + const approvedFormDefaultState = approvedState ? "true" : "false"; + + if (w2lTemplateUseCount !== undefined && w2lTemplateUseCount !== null) { + humanUseCount = w2lTemplateUseCount; + } + + return ( +
+

{h1Title}

+ +

+ < Back to template +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
DetailValue
Creator + {creatorLink} +
Use Count{humanUseCount}
Title{w2lTemplate?.title}
Upload Date (UTC){w2lTemplate?.created_at}
+ +
+ +
+ + +
+
+ +
+ +
+
+ +

+ +

+ +
+
+ +
+
+ < Back to template +
+ ) +} + +export { W2lTemplateApproveFc }; diff --git a/vocodes/src/v2/w2l/w2l_template_view/W2lTemplateViewFc.tsx b/vocodes/src/v2/w2l/w2l_template_view/W2lTemplateViewFc.tsx index ad94f481cb..8a34c9c2f8 100644 --- a/vocodes/src/v2/w2l/w2l_template_view/W2lTemplateViewFc.tsx +++ b/vocodes/src/v2/w2l/w2l_template_view/W2lTemplateViewFc.tsx @@ -272,6 +272,28 @@ function W2lTemplateViewFc(props: Props) { ); } + let approveButton = ; + + if (props.sessionWrapper.canApproveW2lTemplates()) { + const currentlyApproved = w2lTemplate?.is_public_listing_approved; + + const approveButtonTitle = currentlyApproved? "Unapprove Template?" : "Approve Template?"; + + const approveButtonCss = currentlyApproved? + "button is-danger is-light is-large is-fullwidth" : + "button is-info is-light is-large is-fullwidth"; + + approveButton = ( + <> +
+ {approveButtonTitle} + + ); + } + let deleteButton = ; if (props.sessionWrapper.canDeleteTtsModelByUserToken(w2lTemplate?.creator_user_token)) { @@ -431,10 +453,10 @@ function W2lTemplateViewFc(props: Props) { - {modOnlyApprovalForm} - {editButton} + {approveButton} + {deleteButton}