github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/public/src/overmind/utils/errors.ts (about) 1 import { Code } from "@bufbuild/connect" 2 import { AnyMessage } from "@bufbuild/protobuf" 3 import { Response } from "../../client" 4 5 /** Prompt contains the messages to display to the user when prompting for confirmation. */ 6 export namespace Prompt { 7 export const GroupDelete = "Are you sure you want to delete this group?" 8 export const GroupRepoNotEmpty = "Warning: The group repository is not empty. Do you still want to delete the group, GitHub team, and group repository?" 9 export const EnrollmentRepoNotEmpty = "Warning: The enrollment repository is not empty. Do you still want to delete the enrollment and enrollment repository?" 10 } 11 12 /** promptOnErrorResponse prompts the user with a warning if the response contains an error with the given code. 13 * If the user confirms the warning, the function returns null. Otherwise, it returns the error. 14 * The function is used to prompt the user before performing an action that may result in data loss. 15 * @param response The response to check for errors. 16 * @param errorCode The error code to check for. 17 * @param message The message to display to the user. 18 * @returns The error if the user did not confirm the warning, or null if the user did. 19 * 20 */ 21 export function promptOnErrorResponse<T extends AnyMessage>(response: Response<T>, errorCode: Code, message: string) { 22 if (response.error) { 23 if (response.error.code === errorCode) { 24 if (confirm(message)) { 25 return null 26 } 27 } 28 } 29 return response.error 30 }