167 lines
6.1 KiB
TypeScript
167 lines
6.1 KiB
TypeScript
import { createSignal } from 'solid-js';
|
|
import { createStore, produce } from 'solid-js/store';
|
|
import type { ExtractedItem } from '../utils/csv-parser';
|
|
import type { TemplateItem, Scope, SubScope, EstimateItem, JobInfo, Modifier } from '../types';
|
|
import { ModifierRegistry } from '../utils/modifier-registry';
|
|
|
|
// Raw CSV Data
|
|
const [activeItems, setActiveItems] = createSignal<ExtractedItem[]>([]);
|
|
const [activeIgnoredLines, setActiveIgnoredLines] = createSignal<string[][]>([]);
|
|
|
|
// Estimate Metadata
|
|
const [activeEstimateId, setActiveEstimateId] = createSignal<string | null>(null);
|
|
const [activeEstimateName, setActiveEstimateName] = createSignal('');
|
|
const [activeHistory, setActiveHistory] = createSignal<any[]>([]);
|
|
const [activeLatestSnapshot, setActiveLatestSnapshot] = createSignal<any | null>(null);
|
|
const [isLatestVersion, setIsLatestVersion] = createSignal(true);
|
|
|
|
// Estimate Builder State
|
|
const [scopes, setScopes] = createStore<Scope[]>([]);
|
|
const [subScopes, setSubScopes] = createStore<SubScope[]>([]);
|
|
const [estimateItems, setEstimateItems] = createStore<EstimateItem[]>([]);
|
|
|
|
const [clientInfo, setClientInfo] = createStore({
|
|
name: '',
|
|
address: '',
|
|
phone: '',
|
|
fax: ''
|
|
});
|
|
|
|
const [jobInfo, setJobInfo] = createStore<JobInfo>({
|
|
number: '',
|
|
name: '',
|
|
address: '',
|
|
workOrder: ''
|
|
});
|
|
|
|
const [generalEstimateNotes, setGeneralEstimateNotes] = createSignal('');
|
|
const [generalPreNotes, setGeneralPreNotes] = createSignal('');
|
|
|
|
// Template Creator State
|
|
const [activeTemplateName, setActiveTemplateName] = createSignal('');
|
|
const [activeTemplateItems, setActiveTemplateItems] = createStore<TemplateItem[]>([]);
|
|
const [activeTemplateModifiers, setActiveTemplateModifiers] = createStore<Modifier[]>([]);
|
|
const [activeTemplateSubScopeName, setActiveTemplateSubScopeName] = createSignal<string | undefined>(undefined);
|
|
const [activeTemplateId, setActiveTemplateId] = createSignal<string | null>(null);
|
|
|
|
export const appStore = {
|
|
// Raw CSV Extraction
|
|
items: activeItems,
|
|
setItems: setActiveItems,
|
|
ignoredLines: activeIgnoredLines,
|
|
setIgnoredLines: setActiveIgnoredLines,
|
|
|
|
// Estimate Meta
|
|
estimateId: activeEstimateId,
|
|
setEstimateId: setActiveEstimateId,
|
|
estimateName: activeEstimateName,
|
|
setEstimateName: setActiveEstimateName,
|
|
history: activeHistory,
|
|
setHistory: setActiveHistory,
|
|
latestSnapshot: activeLatestSnapshot,
|
|
setLatestSnapshot: setActiveLatestSnapshot,
|
|
isLatestVersion: isLatestVersion,
|
|
setIsLatestVersion: setIsLatestVersion,
|
|
|
|
// Estimate Builder
|
|
scopes, setScopes,
|
|
subScopes, setSubScopes,
|
|
estimateItems, setEstimateItems,
|
|
clientInfo, setClientInfo,
|
|
jobInfo, setJobInfo,
|
|
generalEstimateNotes, setGeneralEstimateNotes,
|
|
generalPreNotes, setGeneralPreNotes,
|
|
// Scopes
|
|
addModifier(subScopeId: string, moduleId: string) {
|
|
const module = ModifierRegistry.getModule(moduleId);
|
|
const newModifier: Modifier = {
|
|
id: Math.random().toString(36).substr(2, 9),
|
|
moduleId: module.id,
|
|
name: module.defaultLabel,
|
|
value: module.defaultValue,
|
|
valueType: module.defaultValueType,
|
|
unitLabel: module.defaultUnitLabel,
|
|
type: module.id as any, // Temporary cast
|
|
includeInTotal: module.id !== 'man-hours'
|
|
};
|
|
|
|
setSubScopes(s => s.id === subScopeId, produce((ss) => {
|
|
if (!ss.modifiers) ss.modifiers = [];
|
|
ss.modifiers.push(newModifier);
|
|
}));
|
|
},
|
|
updateModifier: (subScopeId: string, modifierId: string, updates: Partial<Modifier>) => {
|
|
setSubScopes(
|
|
s => s.id === subScopeId,
|
|
'modifiers',
|
|
(m: Modifier) => m.id === modifierId,
|
|
updates
|
|
);
|
|
},
|
|
removeModifier: (subScopeId: string, modifierId: string) => {
|
|
setSubScopes(
|
|
s => s.id === subScopeId,
|
|
'modifiers',
|
|
(m = []) => m.filter(mod => mod.id !== modifierId)
|
|
);
|
|
},
|
|
|
|
// Templates
|
|
templateId: activeTemplateId,
|
|
setTemplateId: setActiveTemplateId,
|
|
templateName: activeTemplateName,
|
|
setTemplateName: setActiveTemplateName,
|
|
templateItems: activeTemplateItems,
|
|
setTemplateItems: setActiveTemplateItems,
|
|
templateModifiers: activeTemplateModifiers,
|
|
setTemplateModifiers: setActiveTemplateModifiers,
|
|
templateSubScopeName: activeTemplateSubScopeName,
|
|
setTemplateSubScopeName: setActiveTemplateSubScopeName,
|
|
|
|
addTemplateModifier(moduleId: string) {
|
|
const module = ModifierRegistry.getModule(moduleId);
|
|
const newModifier: Modifier = {
|
|
id: Math.random().toString(36).substr(2, 9),
|
|
moduleId: module.id,
|
|
name: module.defaultLabel,
|
|
value: module.defaultValue,
|
|
valueType: module.defaultValueType,
|
|
unitLabel: module.defaultUnitLabel,
|
|
type: module.id as any,
|
|
includeInTotal: module.id !== 'man-hours'
|
|
};
|
|
setActiveTemplateModifiers(prev => [...prev, newModifier]);
|
|
},
|
|
updateTemplateModifier: (modifierId: string, updates: Partial<Modifier>) => {
|
|
setActiveTemplateModifiers(
|
|
(m: Modifier) => m.id === modifierId,
|
|
updates
|
|
);
|
|
},
|
|
removeTemplateModifier: (modifierId: string) => {
|
|
setActiveTemplateModifiers(prev => prev.filter(mod => mod.id !== modifierId));
|
|
},
|
|
|
|
resetEstimate: () => {
|
|
setActiveItems([]);
|
|
setActiveIgnoredLines([]);
|
|
setActiveEstimateId(null);
|
|
setActiveEstimateName('');
|
|
setActiveHistory([]);
|
|
setActiveLatestSnapshot(null);
|
|
setIsLatestVersion(true);
|
|
setScopes([]);
|
|
setSubScopes([]);
|
|
setEstimateItems([]);
|
|
setClientInfo({ name: '', address: '', phone: '', fax: '' });
|
|
setJobInfo({ number: '', name: '', address: '', workOrder: '' });
|
|
setGeneralEstimateNotes('');
|
|
setGeneralPreNotes('');
|
|
setActiveTemplateName('');
|
|
setActiveTemplateItems([]);
|
|
setActiveTemplateModifiers([]);
|
|
setActiveTemplateSubScopeName(undefined);
|
|
setActiveTemplateId(null);
|
|
}
|
|
};
|