From 3556f29a2493f671f98d78c99958b2f70500d434 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Wed, 13 May 2020 18:40:09 +0800 Subject: [PATCH] refactor with const enum --- .../assets/src/scripts/hooks/useTexture.ts | 2 +- resources/assets/src/scripts/types.ts | 6 +++- .../PlayersManagement/ModalUpdateTexture.tsx | 3 +- .../assets/src/views/skinlib/Show/index.tsx | 31 +++++++++++++------ .../skinlib/SkinLibrary/FilterSelector.tsx | 13 ++++---- .../src/views/skinlib/SkinLibrary/index.tsx | 9 ++++-- .../src/views/skinlib/SkinLibrary/utils.ts | 5 +-- resources/assets/src/views/skinlib/Upload.tsx | 18 +++++------ .../assets/src/views/user/Closet/index.tsx | 21 ++++++++----- .../assets/src/views/user/Players/index.tsx | 4 +-- .../assets/tests/views/skinlib/Show.test.tsx | 6 ++-- .../tests/views/skinlib/SkinLibrary.test.tsx | 3 +- .../assets/tests/views/user/Closet.test.tsx | 6 ++-- .../assets/tests/views/user/Players.test.tsx | 20 ++++++------ 14 files changed, 90 insertions(+), 57 deletions(-) diff --git a/resources/assets/src/scripts/hooks/useTexture.ts b/resources/assets/src/scripts/hooks/useTexture.ts index a3aac1cc..2f861663 100644 --- a/resources/assets/src/scripts/hooks/useTexture.ts +++ b/resources/assets/src/scripts/hooks/useTexture.ts @@ -7,7 +7,7 @@ type Response = fetch.ResponseBody export default function useTexture() { const [tid, setTid] = useState(0) const [url, setUrl] = useState('') - const [type, setType] = useState('steve') + const [type, setType] = useState(TextureType.Steve) useEffect(() => { if (tid <= 0) { diff --git a/resources/assets/src/scripts/types.ts b/resources/assets/src/scripts/types.ts index 6aedcdfa..f4fb89b6 100644 --- a/resources/assets/src/scripts/types.ts +++ b/resources/assets/src/scripts/types.ts @@ -39,7 +39,11 @@ export type Texture = { likes: number } -export type TextureType = 'steve' | 'alex' | 'cape' +export const enum TextureType { + Steve = 'steve', + Alex = 'alex', + Cape = 'cape', +} export type ClosetItem = Texture & { pivot: { user_uid: number; texture_tid: number; item_name: string } diff --git a/resources/assets/src/views/admin/PlayersManagement/ModalUpdateTexture.tsx b/resources/assets/src/views/admin/PlayersManagement/ModalUpdateTexture.tsx index b73f6cd5..e70a4499 100644 --- a/resources/assets/src/views/admin/PlayersManagement/ModalUpdateTexture.tsx +++ b/resources/assets/src/views/admin/PlayersManagement/ModalUpdateTexture.tsx @@ -1,5 +1,6 @@ import React, { useState } from 'react' import { t } from '@/scripts/i18n' +import { TextureType } from '@/scripts/types' import Modal from '@/components/Modal' interface Props { @@ -58,7 +59,7 @@ const ModalUpdateTexture: React.FC = (props) => { className="mr-1" type="radio" value="cape" - checked={type === 'cape'} + checked={type === TextureType.Cape} onChange={handleTypeChange} /> {t('general.cape')} diff --git a/resources/assets/src/views/skinlib/Show/index.tsx b/resources/assets/src/views/skinlib/Show/index.tsx index 5e77ad4e..693df0ef 100644 --- a/resources/assets/src/views/skinlib/Show/index.tsx +++ b/resources/assets/src/views/skinlib/Show/index.tsx @@ -93,9 +93,9 @@ const Show: React.FC = () => { input: texture.type, inputType: 'radios', choices: [ - { text: 'Steve', value: 'steve' }, - { text: 'Alex', value: 'alex' }, - { text: t('general.cape'), value: 'cape' }, + { text: 'Steve', value: TextureType.Steve }, + { text: 'Alex', value: TextureType.Alex }, + { text: t('general.cape'), value: TextureType.Cape }, ], }) type = value as TextureType @@ -232,7 +232,10 @@ const Show: React.FC = () => { const linkToUploader = (() => { const search = new URLSearchParams() - search.append('filter', texture.type === 'cape' ? 'cape' : 'skin') + search.append( + 'filter', + texture.type === TextureType.Cape ? TextureType.Cape : 'skin', + ) search.append('uploader', texture.uploader?.toString()) return `${blessing.base_url}/skinlib?${search}` @@ -246,8 +249,12 @@ const Show: React.FC = () => { {createPortal( }> {currentUid === 0 ? ( @@ -284,7 +291,7 @@ const Show: React.FC = () => { {t('skinlib.addToCloset')} )} - {texture.type !== 'cape' && ( + {texture.type !== TextureType.Cape && (