Skip to content

Commit

Permalink
feat(idea/frontend): display sails payload boolean primitives as a ch…
Browse files Browse the repository at this point in the history
…eckbox (#1598)
  • Loading branch information
nikitayutanov authored Jul 16, 2024
1 parent 0e16414 commit 3961072
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion idea/frontend/src/features/sails/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type SailsService = Services[string];
type Functions = SailsService[keyof SailsService];

type Result = typeof RESULT[keyof typeof RESULT];
type PayloadValue = string | null | Array<PayloadValue> | { [key: string]: PayloadValue };
type PayloadValue = string | boolean | null | Array<PayloadValue> | { [key: string]: PayloadValue };
type PayloadValueSchema = ReturnType<typeof getPayloadSchema>;

export type { ISailsFuncArg, Result, PayloadValue, PayloadValueSchema, Ctors, Services, Functions };
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TypeDef } from 'sails-js';

import { Input } from '@/shared/ui';
import { Checkbox, Input } from '@/shared/ui';

import { getLabel } from '../../utils';

Expand All @@ -11,7 +11,11 @@ type Props = {
};

function PrimitiveField({ def, name, label }: Props) {
return <Input name={name} direction="y" label={getLabel(label, def)} />;
const props = { name, label: getLabel(label, def) };

if (def.asPrimitive.isBool) return <Checkbox {...props} />;

return <Input direction="y" {...props} />;
}

export { PrimitiveField };
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
EnumDef,
FixedSizeArrayDef,
MapDef,
PrimitiveDef,
ResultDef,
Sails,
StructDef,
Expand All @@ -16,6 +17,7 @@ import { RESULT } from '../../consts';
import { PayloadValue, ISailsFuncArg } from '../../types';

const getDefaultValue = (sails: Sails) => {
const getPrimitiveValue = (def: PrimitiveDef) => (def.isBool ? false : '');
const getResultValue = ({ [RESULT.OK]: { def } }: ResultDef) => ({ [RESULT.OK]: getValue(def) });
const getVecValue = ({ def }: VecDef) => getPreformattedText([getValue(def)]);
const getFixedSizeArrayValue = ({ len, def }: FixedSizeArrayDef) => new Array<PayloadValue>(len).fill(getValue(def));
Expand All @@ -30,7 +32,7 @@ const getDefaultValue = (sails: Sails) => {
};

const getValue = (def: TypeDef): PayloadValue => {
if (def.isPrimitive) return '';
if (def.isPrimitive) return getPrimitiveValue(def.asPrimitive);
if (def.isOptional) return null;
if (def.isResult) return getResultValue(def.asResult);
if (def.isVec) return getVecValue(def.asVec);
Expand All @@ -48,6 +50,7 @@ const getDefaultValue = (sails: Sails) => {

const getDefaultPayloadValue = (sails: Sails, args: ISailsFuncArg[]) => {
const result = args.map(({ typeDef }, index) => [index, getDefaultValue(sails)(typeDef)] as const);
console.log('Object.fromEntries(result): ', Object.fromEntries(result));

return Object.fromEntries(result);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const isUnion = <T>(arr: T[]): arr is [T, T, ...T[]] => arr.length >= 2;

const getPayloadSchema = (sails: Sails, args: ISailsFuncArg[], encode: (..._args: unknown[]) => HexString) => {
const getSchema = (def: TypeDef): z.ZodType<unknown> => {
if (def.isPrimitive) return z.string().trim();
if (def.isPrimitive) return def.asPrimitive.isBool ? z.boolean() : z.string().trim();

if (def.isOptional) return z.union([z.null(), getSchema(def.asOptional.def)]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PayloadValue } from '../../types';

const getResetPayloadValue = (value: PayloadValue): PayloadValue => {
if (isString(value)) return '';
if (typeof value === 'boolean') return false;
if (Array.isArray(value)) return value.map((_value) => getResetPayloadValue(_value));

if (typeof value === 'object' && value !== null)
Expand Down

0 comments on commit 3961072

Please sign in to comment.