#How to Refactor Action-Specific Data Handling

10 messages · Page 1 of 1 (latest)

crisp creek
#

That's my code:

type SearchProps = {
  interaction:
    | Command.ChatInputCommandInteraction
    | Command.ContextMenuCommandInteraction;
  user: User;
  type: "create" | "delete" | "info";
};

type ActionData = {
  create: CreateAction;
  delete: DeleteAction;
  info: InfoAction;
};

type CreateAction = {
  type: "create";
  data: {
    projects: string[];
    assignee_section: string;
  };
};

type DeleteAction = {
  type: "delete";
  data: {
    id: number;
  };
};

type InfoAction = {
  type: "info";
  data: {
    userId: number;
  };
};

type ActionType = keyof ActionData;

export async function searchForProjectAndSection({
  interaction,
  user,
  type,
}: SearchProps) {
  if (!user || !interaction) return;

  const { mongoose, asana } = user;
  const taskData: Partial<ActionData[ActionType]['data']> = {}; // Using Partial to initialize an empty object

  const projects = await getWorkspaceProjects(
    mongoose.access_token,
    mongoose.workspace
  );

  if (projects && projects?.length > 0) {
    const option = await selectOption(
      interaction,
      {
        customId: `task_${type}_projects`,
        description: texts[type].project,
      },
      projects
    );
    switch (type) {
      case "create":  taskData. 
    }
  }
}

my problem is here :

 switch (type) {
      case "create":  taskData. 
    }

it don't show the data

analog saffron
#

Ehm what's even type?

#

Ah nvm I see it

crisp creek
#

Should show the data here

analog saffron
#
interface SearchProps<K extends keyof ActionData> {
  // ..
  type: K;
}
export async function searchForProjectAndSection<K extends keyof ActionData>({
  // ..
  type
}: SearchProps<K>) {
  const taskData: Partial<ActionData[K]['data']> = {};
#

Otherwise there's no link between taskData and the actual type

crisp creek
#

@analog saffron

analog saffron
#

Would need some playground thingie

#

And I'm walking outside so it'll take a little while 😄