You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.7 KiB
66 lines
2.7 KiB
import {EmpathyGroup} from "../../datatypes/EmpathyGroup";
|
|
import {isPopulatedArray} from "../../utils/Arrays";
|
|
import chalk from "chalk";
|
|
import pluralize from "pluralize";
|
|
import {InquireFunction} from "../Inquire";
|
|
import {EmpathyGroupPromptOptions} from "./EmpathyGroupPrompt";
|
|
|
|
export interface EmpathyGroupListPromptOptions {
|
|
readonly default?: readonly EmpathyGroup[]
|
|
readonly listName: string
|
|
}
|
|
|
|
export interface EmpathyGroupListPromptDependencies {
|
|
readonly inquire: InquireFunction
|
|
readonly promptForEmpathyGroup: (opts: EmpathyGroupPromptOptions) => Promise<EmpathyGroup | null>
|
|
}
|
|
|
|
export function empathyGroupListPrompt(deps: EmpathyGroupListPromptDependencies): (opts: EmpathyGroupListPromptOptions) => Promise<readonly EmpathyGroup[]> {
|
|
return (opts) => promptForEmpathyGroupList(opts, deps)
|
|
}
|
|
|
|
export async function promptForEmpathyGroupList(opts: EmpathyGroupListPromptOptions, deps: EmpathyGroupListPromptDependencies): Promise<readonly EmpathyGroup[]> {
|
|
const {default: value = [], listName} = opts
|
|
const {inquire, promptForEmpathyGroup} = deps
|
|
const option = isPopulatedArray(value) ? await inquire({
|
|
type: "list",
|
|
message: `Which group of ${listName} would you like to edit?`,
|
|
default: value.length + 1,
|
|
choices: [
|
|
...value.map((group, index) => ({
|
|
value: index,
|
|
name: `${group.header} ${isPopulatedArray(group.items) ? chalk.dim(`(currently ${pluralize("item", group.items.length, true)})`) : ""}`
|
|
})),
|
|
{value: -1, name: chalk.dim("(Add New Group)")},
|
|
{value: -2, name: "Done"}
|
|
]
|
|
}) : -1
|
|
switch (option) {
|
|
case -1:
|
|
const newGroup = await promptForEmpathyGroup({listName})
|
|
if (newGroup === null) {
|
|
if (isPopulatedArray(value)) {
|
|
return promptForEmpathyGroupList(opts, deps)
|
|
} else {
|
|
return []
|
|
}
|
|
}
|
|
return promptForEmpathyGroupList({...opts, default: [...value, newGroup]}, deps)
|
|
case -2:
|
|
return value
|
|
default:
|
|
const updatedGroup = await promptForEmpathyGroup({default: value[option], listName})
|
|
if (value.length > 1 || updatedGroup !== null) {
|
|
return promptForEmpathyGroupList({
|
|
...opts,
|
|
default: [
|
|
...value.slice(0, option),
|
|
...updatedGroup === null ? [] : [updatedGroup],
|
|
...value.slice(option + 1)
|
|
]
|
|
}, deps)
|
|
} else {
|
|
return []
|
|
}
|
|
}
|
|
} |