88 lines
3.6 KiB
TypeScript
88 lines
3.6 KiB
TypeScript
import { finales, initials, tones } from "./data";
|
|
import { IState } from "./store";
|
|
import { SylPart, Syllable, Tone, Status } from "./types";
|
|
import { GetSyllablesByInitAndFin, getRandomArray, toggle } from "./utils";
|
|
|
|
export enum ActionType {
|
|
toggleOne, toggleAll, refreshPlayList, setPause, setCount, setStatus
|
|
}
|
|
|
|
export enum ToggleType { init, fin }
|
|
export type TogglePayload = { type: ToggleType, part: SylPart }
|
|
|
|
export type Action = { type: ActionType, payload?: any };
|
|
|
|
const ProceedAllInitials = (state: IState):{ allInitiales:boolean, initiales: SylPart[], foundSyllables:Syllable[], foundTones: Tone[]} =>
|
|
{
|
|
let toggled = state.allInitiales ? [] as SylPart[] : initials
|
|
let foundSyllables:Syllable[] = GetSyllablesByInitAndFin( toggled, state.finales )
|
|
let foundTones = tones.filter( t => foundSyllables.some( syl => syl.tones.some( st => st===t.tone) ) )
|
|
return {
|
|
allInitiales: !state.allInitiales,
|
|
initiales: toggled,
|
|
foundSyllables: foundSyllables,
|
|
foundTones: foundTones
|
|
}
|
|
}
|
|
|
|
const ProceedAllFinales = (state: IState):{ allfinales:boolean, finales: SylPart[], foundSyllables:Syllable[], foundTones: Tone[]} =>
|
|
{
|
|
let toggled = state.allfinales ? [] as SylPart[] : finales
|
|
let foundSyllables:Syllable[] = GetSyllablesByInitAndFin( state.initiales, toggled )
|
|
let foundTones = tones.filter( t => foundSyllables.some( syl => syl.tones.some( st => st===t.tone) ) )
|
|
return {
|
|
allfinales: !state.allfinales,
|
|
finales: toggled,
|
|
foundSyllables: foundSyllables,
|
|
foundTones: foundTones
|
|
}
|
|
}
|
|
|
|
const ProceedInitiale = (state: IState, index: SylPart):{ initiales: SylPart[], foundSyllables:Syllable[], foundTones: Tone[]} =>
|
|
{
|
|
let toggled = toggle(state.initiales,index)
|
|
let foundSyllables:Syllable[] = GetSyllablesByInitAndFin( toggled, state.finales )
|
|
let foundTones = tones.filter( t => foundSyllables.some( syl => syl.tones.some( st => st===t.tone) ) )
|
|
return {
|
|
initiales: toggled,
|
|
foundSyllables: foundSyllables,
|
|
foundTones: foundTones
|
|
}
|
|
}
|
|
|
|
const ProceedFinale = (state: IState, index: SylPart):{ finales: SylPart[], foundSyllables:Syllable[], foundTones: Tone[]} =>
|
|
{
|
|
let toggled = toggle(state.finales,index)
|
|
let foundSyllables:Syllable[] = GetSyllablesByInitAndFin( state.initiales , toggled )
|
|
let foundTones = tones.filter( t => foundSyllables.some( syl => syl.tones.some( st => st===t.tone) ) )
|
|
return {
|
|
finales: toggled,
|
|
foundSyllables: foundSyllables,
|
|
foundTones: foundTones
|
|
}
|
|
}
|
|
|
|
export const reducer = (state:IState, action:Action):IState => {
|
|
switch (action.type) {
|
|
case ActionType.setPause: return { ...state, sylPause: action.payload as number }
|
|
case ActionType.setCount: return { ...state, sylCount: action.payload as number }
|
|
case ActionType.refreshPlayList: return { ...state, randomTones: getRandomArray( state.foundTones, state.sylCount! ) }
|
|
case ActionType.setStatus: return { ...state, status: action.payload as Status}
|
|
|
|
case ActionType.toggleAll: {
|
|
if (action.payload as ToggleType === ToggleType.init) return { ...state, ...ProceedAllInitials(state) }
|
|
if (action.payload as ToggleType === ToggleType.fin) return { ...state, ...ProceedAllFinales(state) }
|
|
return state
|
|
}
|
|
|
|
case ActionType.toggleOne: {
|
|
if ( (action.payload as TogglePayload).type === ToggleType.init)
|
|
return { ...state, ...ProceedInitiale(state, (action.payload as TogglePayload).part ) }
|
|
if ( (action.payload as TogglePayload).type === ToggleType.fin)
|
|
return { ...state, ...ProceedFinale(state, (action.payload as TogglePayload).part ) }
|
|
return state
|
|
}
|
|
|
|
default: return state
|
|
}
|
|
} |