import { Dispatch, FC, SetStateAction, useState } from 'react'; import { FaPlus, FaTimes } from 'react-icons/fa'; import { GroupBadgePart } from '../../../api'; import { Column, Flex, Grid, LayoutBadgeImageView } from '../../../common'; import { useGroup } from '../../../hooks'; interface GroupBadgeCreatorViewProps { badgeParts: GroupBadgePart[]; setBadgeParts: Dispatch>; } const POSITIONS: number[] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]; export const GroupBadgeCreatorView: FC = props => { const { badgeParts = [], setBadgeParts = null } = props; const [ selectedIndex, setSelectedIndex ] = useState(-1); const { groupCustomize = null } = useGroup(); const setPartProperty = (partIndex: number, property: string, value: number) => { const newBadgeParts = [ ...badgeParts ]; newBadgeParts[partIndex][property] = value; setBadgeParts(newBadgeParts); if(property === 'key') setSelectedIndex(-1); }; const getSymbolNames = (item: { id: number, images: string[] }) => { if(!item || !item.images || !item.images.length) return []; return item.images .filter(value => !!value && !!value.length) .map(value => value.replace('.png', '').replace('.gif', '').toLowerCase()); }; const isAlphaNumericSymbol = (name: string) => { return /(^|_)symbol_[a-z0-9]$/i.test(name) || /(^|_)symbol_[a-z0-9]_part[12]$/i.test(name); }; const getAvailableSymbols = () => { const symbols = groupCustomize.badgeSymbols || []; if(selectedIndex < 0) return symbols; switch(selectedIndex) { case 1: return symbols.filter(item => getSymbolNames(item).some(name => name.includes('_part1'))); case 2: return symbols.filter(item => getSymbolNames(item).some(name => name.includes('_part2'))); case 3: return symbols.filter(item => getSymbolNames(item).some(name => isAlphaNumericSymbol(name))); case 4: return symbols.filter(item => { const names = getSymbolNames(item); return !names.some(name => name.includes('_part1') || name.includes('_part2') || isAlphaNumericSymbol(name)); }); default: return symbols; } }; if(!badgeParts || !badgeParts.length) return null; return ( <> { ((selectedIndex < 0) && badgeParts && (badgeParts.length > 0)) && badgeParts.map((part, index) => { return ( setSelectedIndex(index) }> { (badgeParts[index].code && (badgeParts[index].code.length > 0)) && } { (!badgeParts[index].code || !badgeParts[index].code.length) && } { (part.type !== GroupBadgePart.BASE) && { POSITIONS.map((position, posIndex) => { return
setPartProperty(index, 'position', position) } />; }) } } { (groupCustomize.badgePartColors.length > 0) && groupCustomize.badgePartColors.map((item, colorIndex) => { return
setPartProperty(index, 'color', (colorIndex + 1)) } />; }) } ); }) } { (selectedIndex >= 0) && { (badgeParts[selectedIndex].type === GroupBadgePart.SYMBOL) && setPartProperty(selectedIndex, 'key', 0) }> } { ((badgeParts[selectedIndex].type === GroupBadgePart.BASE) ? groupCustomize.badgeBases : getAvailableSymbols()).map((item, index) => { return ( setPartProperty(selectedIndex, 'key', item.id) }> ); }) } } ); };