Skip to content

Commit

Permalink
fix: bundle functions together to reduce code size
Browse files Browse the repository at this point in the history
  • Loading branch information
jcw780 committed Jul 6, 2020
1 parent 170166c commit 0de1651
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 77 deletions.
131 changes: 63 additions & 68 deletions src/components/ShellForms/DefaultForms.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -40,7 +41,7 @@ export class DefaultForm extends React.PureComponent<defaultFormProps, defaultFo
return (
<Form.Group className="form-inline" style={{marginBottom: ".25rem"}}>
<Form.Label column sm="3">{props.children}</Form.Label>
<Form.Control as="select" placeholder="" defaultValue={props.defaultValue} aria-label={props.ariaLabel}
<Form.Control as="select" aria-label={props.ariaLabel}
onChange={this.handleChange} ref={this.form} style={{width: "70%"}} value={this.state.value}>
{this.addOptions()()}
</Form.Control>
Expand Down Expand Up @@ -71,7 +72,7 @@ type singleFormT = [string, React.RefObject<DefaultForm>, number]
type defaultFormType = T.defaultFormGeneric<singleFormT>

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<DefaultForm>(), 0],
nation: ['Nation' , React.createRef<DefaultForm>(), 1],
Expand All @@ -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) => {
Expand All @@ -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 (<DefaultForm key={i} keyProp={this.props.keyProp} controlId={name} ref={v[singleFormIndex.ref]}
ariaLabel={v[singleFormIndex.name]} handleValueChange={this.changeForm}
defaultValue={defaultValue} defaultIndex={i}
defaultOptions={defaultDataN[T.singleDefaultDataIndex.options]}
defaultValues={defaultDataN[T.singleDefaultDataIndex.values]}>
defaultValue={defaultValue}
defaultOptions={defaultDataN[S.DefaultDataRowI.options]}
defaultValues={defaultDataN[S.DefaultDataRowI.values]}>
{v[singleFormIndex.name]}
</DefaultForm>);
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/ShellForms/ShellForms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ interface shellFormsProps{
index: number, colors: Array<string>, 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<shellFormsProps> {
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: {}
Expand Down Expand Up @@ -334,7 +334,7 @@ export class ShellForms extends React.PureComponent<shellFormsProps> {
}

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<number>, disabled: boolean}>{
state = {keys: new Set([0, 1]), disabled: false}; deletedKeys: number[] = [];
Expand Down Expand Up @@ -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();
}
Expand Down
8 changes: 7 additions & 1 deletion src/components/ShellForms/Types.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as T from '../commonTypes';
import {ParameterForm} from '../UtilityComponents';

export enum labelI {name, unit, ref, description}
Expand All @@ -10,4 +11,9 @@ export interface formTemplate<K>{
export type formLabelsT = formTemplate<labelT>;
export type formsT = keyof(formLabelsT);

export interface formDataT extends formTemplate<number>{name: string, colors: string[]}
export interface formDataT extends formTemplate<number>{name: string, colors: string[]}

export enum DefaultDataRowI {value, options, values}
export type DefaultDataRowT = [string, string[], string[]]
interface queriedDataT {queriedData: Record<string, Record<string, any>>}
export type defaultDataT = T.defaultFormGeneric<DefaultDataRowT> & queriedDataT
4 changes: 0 additions & 4 deletions src/components/commonTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ export interface defaultFormGeneric<T>{
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<string, Record<string, any>>}
export type defaultDataT = defaultFormGeneric<singleDefaultDataT> & queriedDataT

//Target Data
export interface targetDataNoAngleT {
Expand Down

0 comments on commit 0de1651

Please sign in to comment.