Skip to content

Commit

Permalink
Force type guards by explicitly specifiying field type
Browse files Browse the repository at this point in the history
  • Loading branch information
guerler committed Nov 15, 2023
1 parent ed14cd0 commit cb41ce3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions client/src/components/Grid/configs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface Action {
handler: (router: Router) => void;
}

type Field = FieldKey | FieldOperations;
export type FieldArray = Array<FieldKey | FieldOperations>;

interface FieldKey {
key: string;
Expand All @@ -25,14 +25,14 @@ interface OperationHandlerMessage {
status: string;
}

type OperationHandlerReturn = OperationHandlerMessage | void;
type OperationHandlerReturn = Promise<OperationHandlerMessage> | void;

/**
* Exported Type declarations
*/
export interface Config {
actions?: Array<Action>;
fields: Array<Field>;
fields: FieldArray;
filtering: Filtering<any>;
getData: (offset: number, limit: number, search: string, sort_by: string, sort_desc: boolean) => Promise<any>;
plural: string;
Expand All @@ -45,6 +45,7 @@ export interface Config {
export interface FieldOperations {
key: string;
title: string;
condition?: (data: RowData) => boolean;
operations: Array<Operation>;
width?: number;
}
Expand All @@ -55,7 +56,7 @@ export type RowData = Record<string, unknown>;

export interface Operation {
title: string;
icon: string;
icon: any;
condition?: (data: RowData) => boolean;
handler: (data: RowData, router: Router) => OperationHandlerReturn;
}
8 changes: 4 additions & 4 deletions client/src/components/Grid/configs/visualizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Filtering, { contains, equals, expandNameTag, toBool, type ValidFilter }
import { withPrefix } from "@/utils/redirect";
import { errorMessageAsString, rethrowSimple } from "@/utils/simple-error";

import { type Config } from "./types";
import { type Config, type FieldArray } from "./types";

/**
* Api endpoint handlers
Expand Down Expand Up @@ -61,12 +61,12 @@ const actions = [
/**
* Declare columns to be displayed
*/
const fields = [
const fields: FieldArray = [
{
title: "Title",
key: "title",
type: "operations",
width: "40%",
width: 40,
condition: (data: VisualizationEntry) => !data.deleted,
operations: [
{
Expand Down Expand Up @@ -141,7 +141,7 @@ const fields = [
{
title: "Restore",
icon: faTrashRestore,
condition: (data: VisualizationEntry) => data.deleted,
condition: (data: VisualizationEntry) => !!data.deleted,
handler: async (data: VisualizationEntry) => {
try {
await axios.put(withPrefix(`/api/visualizations/${data.id}`), { deleted: false });
Expand Down

0 comments on commit cb41ce3

Please sign in to comment.