-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Mapping tokens to wearables (#3138)
* feat: Mapping tokens to wearables * fix: Types and tests * fix: Types * fix: Word break
- Loading branch information
1 parent
bb514b1
commit 22fa9b9
Showing
34 changed files
with
394 additions
and
92 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.main { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.mappings { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.main :global(.ui.dropdown > .text > img) { | ||
margin-top: 3px; | ||
} | ||
|
||
.mappingType :global(.ui.dropdown .menu > .item > img) { | ||
margin-top: 0px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import { SyntheticEvent, useCallback, useMemo } from 'react' | ||
import { DropdownProps, Field, InputOnChangeData, SelectField, TextAreaField, TextAreaProps } from 'decentraland-ui' | ||
import { MappingType, MultipleMapping } from '@dcl/schemas' | ||
import { t } from 'decentraland-dapps/dist/modules/translation' | ||
import allIcon from '../../icons/all.svg' | ||
import multipleIcon from '../../icons/multiple.svg' | ||
import singleIcon from '../../icons/single.svg' | ||
import rangeIcon from '../../icons/range.svg' | ||
import { Props } from './MappingEditor.types' | ||
import styles from './MappingEditor.module.css' | ||
|
||
const mappingTypeIcons = { | ||
[MappingType.ANY]: allIcon, | ||
[MappingType.MULTIPLE]: multipleIcon, | ||
[MappingType.SINGLE]: singleIcon, | ||
[MappingType.RANGE]: rangeIcon | ||
} | ||
|
||
export const MappingEditor = (props: Props) => { | ||
const { mapping, error, disabled, onChange } = props | ||
const [mappingType, mappingValue] = useMemo(() => { | ||
switch (mapping.type) { | ||
case MappingType.MULTIPLE: | ||
return [MappingType.MULTIPLE, mapping.ids.join(', ')] | ||
case MappingType.SINGLE: | ||
return [MappingType.SINGLE, mapping.id] | ||
case MappingType.RANGE: | ||
return [MappingType.RANGE, `${mapping.from},${mapping.to}`] | ||
case MappingType.ANY: | ||
default: | ||
return [MappingType.ANY, ''] | ||
} | ||
}, [mapping]) | ||
|
||
const mappingTypeOptions = useMemo( | ||
() => | ||
Object.values(MappingType).map((mapping: MappingType) => ({ | ||
value: mapping, | ||
image: mappingTypeIcons[mapping], | ||
text: t(`mapping_editor.mapping_types.${mapping}`) | ||
})), | ||
[] | ||
) | ||
|
||
const handleMappingTypeChange = useCallback((_: SyntheticEvent<HTMLElement, Event>, { value }: DropdownProps) => { | ||
const mappingType = value as MappingType | ||
switch (mappingType) { | ||
case MappingType.ANY: | ||
props.onChange({ type: mappingType }) | ||
break | ||
case MappingType.MULTIPLE: | ||
props.onChange({ type: mappingType, ids: [] }) | ||
break | ||
case MappingType.SINGLE: | ||
props.onChange({ type: mappingType, id: '' }) | ||
break | ||
case MappingType.RANGE: | ||
props.onChange({ type: mappingType, to: '', from: '' }) | ||
break | ||
} | ||
}, []) | ||
|
||
const handleSingleMappingValueChange = useCallback((_: React.ChangeEvent<HTMLInputElement>, data: InputOnChangeData) => { | ||
onChange({ type: MappingType.SINGLE, id: data.value }) | ||
}, []) | ||
|
||
const handleMultipleMappingValueChange = useCallback((_: React.ChangeEvent<HTMLTextAreaElement>, data: TextAreaProps) => { | ||
const ids = | ||
data.value | ||
?.toString() | ||
.replaceAll(/[^0-9,\s]/g, '') | ||
.split(',') | ||
.map(value => value.trim()) ?? [] | ||
|
||
onChange({ | ||
type: MappingType.MULTIPLE, | ||
ids | ||
}) | ||
}, []) | ||
|
||
const handleFromMappingValueChange = useCallback( | ||
(_: React.ChangeEvent<HTMLInputElement>, data: InputOnChangeData) => { | ||
onChange({ type: MappingType.RANGE, from: data.value, to: mappingValue.split(',')[1] }) | ||
}, | ||
[mappingValue] | ||
) | ||
|
||
const handleToMappingValueChange = useCallback( | ||
(_: React.ChangeEvent<HTMLInputElement>, data: InputOnChangeData) => { | ||
onChange({ type: MappingType.RANGE, from: mappingValue.split(',')[0], to: data.value }) | ||
}, | ||
[mappingValue] | ||
) | ||
|
||
return ( | ||
<div className={styles.main}> | ||
<SelectField | ||
label={t('mapping_editor.mapping_type_label')} | ||
onChange={handleMappingTypeChange} | ||
value={mappingType} | ||
className={styles.mappingType} | ||
options={mappingTypeOptions} | ||
/>{' '} | ||
<div className={styles.mappings}> | ||
{mappingType === MappingType.ANY ? ( | ||
<Field label={t('mapping_editor.mapping_value_label')} disabled value={t('mapping_editor.mapping_value_any')} /> | ||
) : mappingType === MappingType.SINGLE ? ( | ||
<Field | ||
label={t('mapping_editor.mapping_value_label')} | ||
disabled={disabled} | ||
type={mappingType === MappingType.SINGLE ? 'number' : 'text'} | ||
value={mappingValue} | ||
error={!!error} | ||
message={error} | ||
placeholder={'1234567890'} | ||
maxLength={78} | ||
onChange={handleSingleMappingValueChange} | ||
/> | ||
) : mappingType === MappingType.MULTIPLE ? ( | ||
<TextAreaField | ||
label={t('mapping_editor.mapping_value_label')} | ||
disabled={disabled} | ||
info={ | ||
mappingValue.length === 0 && !error | ||
? t('mapping_editor.mapping_value_multiple_info') | ||
: t('mapping_editor.mapping_value_multiple_amount_info', { count: (mapping as MultipleMapping).ids.length }) | ||
} | ||
error={error} | ||
placeholder={'1, 2, 3, 4'} | ||
value={mappingValue} | ||
onChange={handleMultipleMappingValueChange} | ||
/> | ||
) : mappingType === MappingType.RANGE ? ( | ||
<> | ||
<Field | ||
label={t('mapping_editor.mapping_value_from_label')} | ||
disabled={disabled} | ||
type="number" | ||
placeholder={'1'} | ||
maxLength={78} | ||
value={mappingValue.split(',')[0]} | ||
onChange={handleFromMappingValueChange} | ||
/> | ||
<Field | ||
label={t('mapping_editor.mapping_value_to_label')} | ||
disabled={disabled} | ||
type="number" | ||
placeholder={'4000'} | ||
maxLength={78} | ||
value={mappingValue.split(',')[1]} | ||
onChange={handleToMappingValueChange} | ||
/> | ||
</> | ||
) : null} | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Mapping } from '@dcl/schemas' | ||
|
||
export type Props = { | ||
mapping: Mapping | ||
error?: string | ||
disabled?: boolean | ||
onChange: (mapping: Mapping) => void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,4 +81,5 @@ | |
|
||
.ui.modal.ApprovalFlowModal .error-container .urn { | ||
font-size: 12px; | ||
word-break: break-all; | ||
} |
Oops, something went wrong.