-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move from store, make more testable
- Loading branch information
1 parent
3f4d2ec
commit 5e1ae08
Showing
3 changed files
with
111 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Store } from "pages/FlowEditor/lib/store"; | ||
import { SetValue } from "./model"; | ||
|
||
interface HandleSetValue { | ||
nodeData: SetValue; | ||
previousValues: string | string[] | undefined; | ||
passport: Store.passport; | ||
} | ||
|
||
/** | ||
* Handle modifying passport values when passing through a SetValue component | ||
* Called by computePassport() | ||
*/ | ||
export const handleSetValue = ({ | ||
nodeData: { operation, fn, val: current }, | ||
previousValues, | ||
passport, | ||
}: HandleSetValue): Store.passport => { | ||
// We do not amend values set at objects | ||
// These are internal exceptions we do not want to allow users to edit | ||
// e.g. property.boundary.title | ||
const isObject = | ||
typeof previousValues === "object" && | ||
!Array.isArray(previousValues) && | ||
previousValues !== null; | ||
if (isObject) return passport; | ||
|
||
const previous = formatPreviousValues(previousValues); | ||
|
||
const newValues = calculateNewValues({ | ||
operation, | ||
previous, | ||
current, | ||
}); | ||
|
||
if (newValues) { | ||
passport.data![fn] = newValues; | ||
|
||
// Operation has cleared passport value | ||
if (!newValues.length) delete passport.data![fn]; | ||
} | ||
|
||
return passport; | ||
}; | ||
|
||
interface CalculateNewValues { | ||
operation: SetValue["operation"]; | ||
previous: string | string[]; | ||
current: string; | ||
} | ||
|
||
const calculateNewValues = ({ | ||
operation, | ||
previous, | ||
current, | ||
}: CalculateNewValues): undefined | string[] => { | ||
switch (operation) { | ||
case "replace": | ||
// Default behaviour when assigning passport variables | ||
// No custom logic needed | ||
break; | ||
|
||
case "removeOne": { | ||
if (Array.isArray(previous)) { | ||
const removeCurrent = (val: string) => val !== current; | ||
const filtered = previous.filter(removeCurrent); | ||
return filtered; | ||
} | ||
|
||
if (previous === current) { | ||
return []; | ||
} | ||
|
||
break; | ||
} | ||
|
||
case "removeAll": | ||
return []; | ||
|
||
case "append": { | ||
const combined = [...previous, current]; | ||
const unique = [...new Set(combined)]; | ||
return unique; | ||
} | ||
} | ||
} | ||
|
||
const formatPreviousValues = (value: HandleSetValue["previousValues"]): string[] => { | ||
if (!value) return []; | ||
if (Array.isArray(value)) return value; | ||
return [value]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters