Skip to content

Commit

Permalink
Refactor replacedMapToObject in single.tsx and replace spread operato…
Browse files Browse the repository at this point in the history
…r with structuredClone() in EnergyUseHistoryChart.tsx
  • Loading branch information
TBardini committed Oct 2, 2024
1 parent ba23c1f commit 04ea51b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function EnergyUseHistoryChart({ usage_data }: { usage_data: UsageDataSch

const handleOverrideCheckboxChange = (index: number) => {
setBillingRecords((prevRecords) => {
const newRecords = [...prevRecords]
const newRecords = structuredClone(prevRecords)
const period = newRecords[index]

if (period) {
Expand All @@ -80,7 +80,7 @@ export function EnergyUseHistoryChart({ usage_data }: { usage_data: UsageDataSch

newRecords[index] = { ...period }
}

return newRecords
})
}
Expand Down
59 changes: 31 additions & 28 deletions heat-stack/app/routes/_heat+/single.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -453,33 +453,37 @@ function reviver(key: any, value: any) {
return value;
}

function mapToObject(input: any): any {
// Base case: if input is not an object or is null, return it as-is
if (typeof input !== 'object' || input === null) {
return input;
}

// Handle case where input is a Map-like object (with "dataType" as "Map" and a "value" array)
if (input.dataType === 'Map' && Array.isArray(input.value)) {
const obj: Record<any, any> = {}; // Initialize an empty object
for (const [key, value] of input.value) {
obj[key] = mapToObject(value); // Recursively process nested Maps
}
return obj;
}

// Handle case where input is an array
if (Array.isArray(input)) {
return input.map(mapToObject); // Recursively process each array element
}

console.log('input', input)
// Return the input for any other types of objects
return input;
/**
* Translates an already replaced (see https://stackoverflow.com/a/56150320) and then parsed Map from pyodide into a plain js Object.
* @param input {Map}
* @returns {Object}
*/
function replacedMapToObject(input: any): any {
// Base case: if input is not an object or is null, return it as-is
if (typeof input !== 'object' || input === null) {
return input
}

// Handle case where input is a Map-like object (with "dataType" as "Map" and a "value" array)
if (input.dataType === 'Map' && Array.isArray(input.value)) {
const obj: Record<any, any> = {} // Initialize an empty object
for (const [key, value] of input.value) {
obj[key] = replacedMapToObject(value) // Recursively process nested Maps
}
return obj
}

// Handle case where input is an array
if (Array.isArray(input)) {
return input.map(replacedMapToObject) // Recursively process each array element
}

console.log('input', input)
// Return the input for any other types of objects
return input
}



export default function Inputs() {
// const location = useLocation();
// console.log(`location:`, location); // `.state` is `null`
Expand Down Expand Up @@ -543,16 +547,15 @@ export default function Inputs() {
const parsedData = JSON.parse(lastResult.data);

// Recursively transform any Maps in lastResult to objects
modifiedLastResult = mapToObject(parsedData);
modifiedLastResult = replacedMapToObject(parsedData);
usage_data = modifiedLastResult; // Get the relevant part of the transformed result

console.log('usage_data', usage_data)

} catch (error) {
console.error('Error parsing lastResult data:', error);
}
}

console.log('usage_data', usage_data);

type SchemaZodFromFormType = z.infer<typeof Schema>
const [form, fields] = useForm({
/* removed lastResult , consider re-adding https://conform.guide/api/react/useForm#options */
Expand Down

0 comments on commit 04ea51b

Please sign in to comment.