From 0de16519401a55ce892dde633165b38db5fc8d06 Mon Sep 17 00:00:00 2001 From: jwc780 Date: Sun, 5 Jul 2020 22:46:50 -0400 Subject: [PATCH] fix: bundle functions together to reduce code size --- src/components/ShellForms/DefaultForms.tsx | 131 ++++++++++----------- src/components/ShellForms/ShellForms.tsx | 8 +- src/components/ShellForms/Types.tsx | 8 +- src/components/commonTypes.tsx | 4 - 4 files changed, 74 insertions(+), 77 deletions(-) diff --git a/src/components/ShellForms/DefaultForms.tsx b/src/components/ShellForms/DefaultForms.tsx index 9b40e51..a425766 100644 --- a/src/components/ShellForms/DefaultForms.tsx +++ b/src/components/ShellForms/DefaultForms.tsx @@ -1,10 +1,11 @@ import React from 'react'; import {Form} from 'react-bootstrap'; import * as T from '../commonTypes'; +import * as S from './Types'; interface defaultFormProps{ controlId: string, keyProp: number, ariaLabel : string, children : string | JSX.Element, - defaultValue: string, defaultIndex: number, defaultOptions: string[], defaultValues: string[], handleValueChange: Function, + defaultValue: string, defaultOptions: string[], defaultValues: string[], handleValueChange: Function, } interface defaultFormState{ options: string[], value: string @@ -40,7 +41,7 @@ export class DefaultForm extends React.PureComponent {props.children} - {this.addOptions()()} @@ -71,7 +72,7 @@ type singleFormT = [string, React.RefObject, number] type defaultFormType = T.defaultFormGeneric export class DefaultShips extends React.PureComponent -<{sendDefault: Function, reset: Function, index: number, keyProp: number, defaultData: T.defaultDataT}> { +<{sendDefault: Function, reset: Function, index: number, keyProp: number, defaultData: S.defaultDataT}> { defaultForms : defaultFormType = Object.seal({ version: ['Version' , React.createRef(), 0], nation: ['Nation' , React.createRef(), 1], @@ -83,22 +84,18 @@ export class DefaultShips extends React.PureComponent changeForm = async (value, id : keyof(defaultFormType)) => { //this.defaultForms[id][singleFormIndex.value] = value; let queryIndex = this.defaultForms[id][singleFormIndex.queryIndex]; - const queries = [ - this.queryNation, this.queryType, this.queryShip, - this.queryArtillery, this.queryShellType, this.sendData - ] const defaultData = this.props.defaultData; if(queryIndex === 0){ defaultData.queriedData = await fetchJsonData( `${dataURL}${value}_s.json`); }else if (queryIndex === 3){ - value = defaultData[id][T.singleDefaultDataIndex.values][ - defaultData[id][T.singleDefaultDataIndex.options].indexOf(value) + value = defaultData[id][S.DefaultDataRowI.values][ + defaultData[id][S.DefaultDataRowI.options].indexOf(value) ]; } - defaultData[id][T.singleDefaultDataIndex.value] = value; + defaultData[id][S.DefaultDataRowI.value] = value; for(; queryIndex <= 5; queryIndex++){ - if(queryIndex in queries){queries[queryIndex]();} + this.postVersion(queryIndex)(); } } updateForm = (target : keyof(defaultFormType), options, values) => { @@ -107,88 +104,86 @@ export class DefaultShips extends React.PureComponent //apparently prevents async calls from updating deleted refs I guess... //fixes delete ship crash bug const targetData = this.props.defaultData[target] - let newValue = targetData[T.singleDefaultDataIndex.value]; + let newValue = targetData[S.DefaultDataRowI.value]; if(!options.includes(newValue)){ newValue = options[0]; } - targetData[T.singleDefaultDataIndex.options] = options; - targetData[T.singleDefaultDataIndex.values] = values; + targetData[S.DefaultDataRowI.options] = options; + targetData[S.DefaultDataRowI.values] = values; if(target === 'ship'){ - targetData[T.singleDefaultDataIndex.value] = targetData[T.singleDefaultDataIndex.values][ - targetData[T.singleDefaultDataIndex.options].indexOf(newValue) + targetData[S.DefaultDataRowI.value] = targetData[S.DefaultDataRowI.values][ + targetData[S.DefaultDataRowI.options].indexOf(newValue) ]; }else{ - targetData[T.singleDefaultDataIndex.value] = newValue; + targetData[S.DefaultDataRowI.value] = newValue; } refCurrent.updateOptions(options, newValue); } } - queryVersion = async () => { + queryVersion = async () => { //probably should be called initialize since it is never called ever again... const data = await fetchJsonData(`${dataURL}versions.json`); const reversed = data.reverse(); this.updateForm('version', reversed, reversed); this.changeForm(reversed[0], 'version'); } - queryNation = () => { - const defaultData = this.props.defaultData; - const options = Object.keys(defaultData.queriedData.ships); - this.updateForm('nation', options, options); - } - queryType = () => { - const dData = this.props.defaultData; - const nation = dData.nation[T.singleDefaultDataIndex.value]; - const qDataS = dData.queriedData.ships; - const options = Object.keys(qDataS[nation]); - this.updateForm('shipType', options, options); - } - queryShip = async () => { - const dData = this.props.defaultData, qDataS = dData.queriedData.ships; - const sDI = T.singleDefaultDataIndex.value; - const nation = dData.nation[sDI], type = dData.shipType[sDI]; - const ships = qDataS[nation][type]; - let values = Object.keys(ships), options : string[] = []; - values.sort((a, b) => {return ships[a]['Tier'] - ships[b]['Tier']}); - values.forEach((ship, i) => {options.push(`(${ships[ship]['Tier']}) ${ship}`);}); - this.updateForm('ship', options, values); - } - queryArtillery = () => { - const dData = this.props.defaultData, qDataS = dData.queriedData.ships; - const sDI = T.singleDefaultDataIndex.value; - const nation = dData.nation[sDI], type = dData.shipType[sDI], ship = dData.ship[sDI]; - const options = Object.keys(qDataS[nation][type][ship].artillery); - this.updateForm('artillery', options, options); - } - queryShellType = () => { - const dData = this.props.defaultData, qDataS = dData.queriedData.ships; - const sDI = T.singleDefaultDataIndex.value; - const nation = dData.nation[sDI], type = dData.shipType[sDI]; - const ship = dData.ship[sDI], artillery = dData.artillery[sDI]; - const options = Object.keys(qDataS[nation][type][ship].artillery[artillery]); - this.updateForm('shellType', options, options); - } - sendData = () => { + postVersion = (index: number) => { const dData = this.props.defaultData, qDataS = dData.queriedData.ships; - const sDI = T.singleDefaultDataIndex.value; - const nation = dData.nation[sDI], type = dData.shipType[sDI], ship = dData.ship[sDI]; - const artillery = dData.artillery[sDI], shellType = dData.shellType[sDI]; - const shellName = qDataS[nation][type][ship].artillery[artillery][shellType]; - this.props.sendDefault(dData.queriedData.shells[shellName], ship); + const sDI = S.DefaultDataRowI.value; + const queryNation = () => { + const options = Object.keys(dData.queriedData.ships); + this.updateForm('nation', options, options); + } + const queryType = () => { + const nation = dData.nation[sDI]; + const qDataS = dData.queriedData.ships; + const options = Object.keys(qDataS[nation]); + this.updateForm('shipType', options, options); + } + const queryShip = () => { + const nation = dData.nation[sDI], type = dData.shipType[sDI]; + const ships = qDataS[nation][type]; + let values = Object.keys(ships), options : string[] = []; + values.sort((a, b) => {return ships[a]['Tier'] - ships[b]['Tier']}); + values.forEach((ship, i) => {options.push(`(${ships[ship]['Tier']}) ${ship}`);}); + this.updateForm('ship', options, values); + } + const queryArtillery = () => { + const nation = dData.nation[sDI], type = dData.shipType[sDI], ship = dData.ship[sDI]; + const options = Object.keys(qDataS[nation][type][ship].artillery); + this.updateForm('artillery', options, options); + } + const queryShellType = () => { + const nation = dData.nation[sDI], type = dData.shipType[sDI]; + const ship = dData.ship[sDI], artillery = dData.artillery[sDI]; + const options = Object.keys(qDataS[nation][type][ship].artillery[artillery]); + this.updateForm('shellType', options, options); + } + const sendData = () => { + const nation = dData.nation[sDI], type = dData.shipType[sDI], ship = dData.ship[sDI]; + const artillery = dData.artillery[sDI], shellType = dData.shellType[sDI]; + const shellName = qDataS[nation][type][ship].artillery[artillery][shellType]; + this.props.sendDefault(dData.queriedData.shells[shellName], ship); + } + const queries = [ + queryNation, queryType, queryShip, + queryArtillery, queryShellType, sendData + ] + return queries[index]; } private addDefaultForms = () => { const defaultData = this.props.defaultData; const singleForm = ([name, v] : [string, singleFormT], i) : JSX.Element => { const defaultDataN = defaultData[name]; - let defaultValue = defaultDataN[T.singleDefaultDataIndex.value]; + let defaultValue = defaultDataN[S.DefaultDataRowI.value]; if(name === 'ship'){ - defaultValue = defaultDataN[T.singleDefaultDataIndex.options][ - defaultDataN[T.singleDefaultDataIndex.values].indexOf(defaultValue)] + defaultValue = defaultDataN[S.DefaultDataRowI.options][ + defaultDataN[S.DefaultDataRowI.values].indexOf(defaultValue)] } - return ( + defaultValue={defaultValue} + defaultOptions={defaultDataN[S.DefaultDataRowI.options]} + defaultValues={defaultDataN[S.DefaultDataRowI.values]}> {v[singleFormIndex.name]} ); } diff --git a/src/components/ShellForms/ShellForms.tsx b/src/components/ShellForms/ShellForms.tsx index 50bb1bc..4668a38 100644 --- a/src/components/ShellForms/ShellForms.tsx +++ b/src/components/ShellForms/ShellForms.tsx @@ -18,14 +18,14 @@ interface shellFormsProps{ index: number, colors: Array, keyProp: number, graph: boolean, deleteShip : Function, copyShip : Function, reset: () => void, settings : T.settingsT, size: number - formData?: S.formDataT, defaultData?: T.defaultDataT, copied: boolean + formData?: S.formDataT, defaultData?: S.defaultDataT, copied: boolean } export class ShellForms extends React.PureComponent { public static defaultProps = { copied : false, graph : true } graph = this.props.graph; - defaultData : T.defaultDataT = Object.seal({ + defaultData : S.defaultDataT = Object.seal({ version: ['', [''], ['']], nation: ['', [''], ['']], shipType: ['', [''], ['']], ship: ['', [''], ['']], artillery: ['', [''], ['']], shellType: ['', [''], ['']], queriedData: {} @@ -334,7 +334,7 @@ export class ShellForms extends React.PureComponent { } interface copyTempT { - default: T.defaultDataT, data: S.formDataT, graph: boolean + default: S.defaultDataT, data: S.formDataT, graph: boolean } export class ShellFormsContainer extends React.Component<{settings : T.settingsT}, {keys: Set, disabled: boolean}>{ state = {keys: new Set([0, 1]), disabled: false}; deletedKeys: number[] = []; @@ -374,7 +374,7 @@ export class ShellFormsContainer extends React.Component<{settings : T.settingsT } } } - copyShip = (defaultData : T.defaultDataT, shellData : S.formDataT, graph : boolean) => { + copyShip = (defaultData : S.defaultDataT, shellData : S.formDataT, graph : boolean) => { this.copyTemp = {default: defaultData, data: shellData, graph: graph}; this.copied = true; this.addShip(); } diff --git a/src/components/ShellForms/Types.tsx b/src/components/ShellForms/Types.tsx index 3df2037..b729241 100644 --- a/src/components/ShellForms/Types.tsx +++ b/src/components/ShellForms/Types.tsx @@ -1,3 +1,4 @@ +import * as T from '../commonTypes'; import {ParameterForm} from '../UtilityComponents'; export enum labelI {name, unit, ref, description} @@ -10,4 +11,9 @@ export interface formTemplate{ export type formLabelsT = formTemplate; export type formsT = keyof(formLabelsT); -export interface formDataT extends formTemplate{name: string, colors: string[]} \ No newline at end of file +export interface formDataT extends formTemplate{name: string, colors: string[]} + +export enum DefaultDataRowI {value, options, values} +export type DefaultDataRowT = [string, string[], string[]] +interface queriedDataT {queriedData: Record>} +export type defaultDataT = T.defaultFormGeneric & queriedDataT \ No newline at end of file diff --git a/src/components/commonTypes.tsx b/src/components/commonTypes.tsx index b2777dc..cbb0309 100644 --- a/src/components/commonTypes.tsx +++ b/src/components/commonTypes.tsx @@ -20,10 +20,6 @@ export interface defaultFormGeneric{ version: T, nation: T, shipType: T, ship: T, artillery: T, shellType: T, } -export enum singleDefaultDataIndex {value, options, values} -export type singleDefaultDataT = [string, string[], string[]] -interface queriedDataT {queriedData: Record>} -export type defaultDataT = defaultFormGeneric & queriedDataT //Target Data export interface targetDataNoAngleT {