Skip to content

Commit

Permalink
feat: implement statefull toggle and adapt new prop (#49)
Browse files Browse the repository at this point in the history
Because

- We need a transition state when deal with toggling the model state

This commit

- Implement the StatefulToggleButton
  • Loading branch information
EiffelFly committed Jun 26, 2022
1 parent d5b8d83 commit 9358247
Show file tree
Hide file tree
Showing 41 changed files with 408 additions and 60 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ queryClient.setQueryData<Model[]>(["models"], (old) => {
- Error bundary
- Suspense
- Refactor react-query queryKey to make it more reliable, especially model services
- Right now the data type is
- PipelineWithRawRecipe -> process -> Pipeline
- Source -> process -> SourceWithDefinition
- Try to refactor above with same logic
- Right now the data type is
- PipelineWithRawRecipe -> process -> Pipeline
- Source -> process -> SourceWithDefinition
- Try to refactor above with same logic
- Complete Button base design in design-system
- Fix RawPipelineRecipe type
- Fix RawPipelineRecipe type
- Unify react-query cache key
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"dependencies": {
"@amplitude/analytics-browser": "^0.4.1",
"@instill-ai/design-system": "^0.0.69",
"@instill-ai/design-system": "^0.0.72",
"axios": "^0.27.2",
"clsx": "^1.1.1",
"cookie": "^0.5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const SingleSelect: FC<SingleSelectProps & FieldProps> = ({
menuPlacement,
value,
error,
additionalMessageOnLabel,
...props
}) => {
const onChange = (_: string, option: SingleSelectOption) => {
Expand All @@ -43,6 +44,7 @@ const SingleSelect: FC<SingleSelectProps & FieldProps> = ({
onChangeInput={onChange}
menuPlacement={menuPlacement}
value={value}
additionalMessageOnLabel={additionalMessageOnLabel}
/>
);
};
Expand All @@ -57,6 +59,7 @@ const SingleSelectFieldFormikWrapper: FC<SingleSelectProps> = ({
description,
label,
additionalOnChangeCb,
additionalMessageOnLabel,
menuPlacement,
value,
error,
Expand All @@ -73,6 +76,7 @@ const SingleSelectFieldFormikWrapper: FC<SingleSelectProps> = ({
description={description}
additionalOnChangeCb={additionalOnChangeCb}
label={label}
additionalMessageOnLabel={additionalMessageOnLabel}
value={value}
menuPlacement={menuPlacement}
error={error}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { FC } from "react";
import { Field, FieldProps } from "formik";
import {
BasicToggleFieldProps,
StatefulToggleField,
} from "@instill-ai/design-system";
import { Nullable, State } from "@/types/general";

export type ToggleFieldProps = Omit<BasicToggleFieldProps, "onChangeInput"> & {
name: string;
additionalOnChangeCb: Nullable<(value: boolean) => void>;
state: State;
};

const StatefulToggleFieldWrapper: FC<ToggleFieldProps & FieldProps> = ({
field,
form,
id,
defaultChecked,
additionalOnChangeCb,
additionalMessageOnLabel,
error,
...props
}) => {
const onChange = (_: string, value: boolean) => {
form.setFieldValue(field.name, value);
if (additionalOnChangeCb) {
additionalOnChangeCb(value);
}
};

return (
<StatefulToggleField
{...props}
id={id}
error={error}
onChangeInput={onChange}
defaultChecked={defaultChecked}
additionalMessageOnLabel={additionalMessageOnLabel}
/>
);
};

const StatefulToggleFieldFormikWrapper: FC<ToggleFieldProps> = ({
id,
name,
disabled,
readOnly,
required,
description,
label,
additionalOnChangeCb,
additionalMessageOnLabel,
defaultChecked,
error,
state,
}) => {
return (
<Field
id={id}
name={name}
component={StatefulToggleFieldWrapper}
disabled={disabled}
readOnly={readOnly}
required={required}
description={description}
additionalOnChangeCb={additionalOnChangeCb}
label={label}
additionalMessageOnLabel={additionalMessageOnLabel}
defaultChecked={defaultChecked}
error={error}
state={state}
/>
);
};

export default StatefulToggleFieldFormikWrapper;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default } from "./StatefulToggleField";
export * from "./StatefulToggleField";
11 changes: 10 additions & 1 deletion src/components/formik/FormikField/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const TextArea: FC<TextAreaProps & FieldProps> = ({
form,
id,
additionalOnChangeCb,
additionalMessageOnLabel,
error,
...props
}) => {
Expand All @@ -24,7 +25,13 @@ const TextArea: FC<TextAreaProps & FieldProps> = ({
};

return (
<BasicTextArea {...props} id={id} error={error} onChangeInput={onChange} />
<BasicTextArea
{...props}
id={id}
error={error}
onChangeInput={onChange}
additionalMessageOnLabel={additionalMessageOnLabel}
/>
);
};

Expand All @@ -36,6 +43,7 @@ const TextAreaFormikWrapper: FC<TextAreaProps> = ({
required,
description,
label,
additionalMessageOnLabel,
additionalOnChangeCb,
placeholder,
autoComplete,
Expand All @@ -55,6 +63,7 @@ const TextAreaFormikWrapper: FC<TextAreaProps> = ({
description={description}
additionalOnChangeCb={additionalOnChangeCb}
label={label}
additionalMessageOnLabel={additionalMessageOnLabel}
placeholder={placeholder}
autoComplete={autoComplete}
value={value}
Expand Down
11 changes: 10 additions & 1 deletion src/components/formik/FormikField/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const TextField: FC<TextFieldProps & FieldProps> = ({
field,
form,
id,
additionalMessageOnLabel,
additionalOnChangeCb,
error,
...props
Expand All @@ -24,7 +25,13 @@ const TextField: FC<TextFieldProps & FieldProps> = ({
};

return (
<BasicTextField {...props} id={id} error={error} onChangeInput={onChange} />
<BasicTextField
{...props}
id={id}
error={error}
onChangeInput={onChange}
additionalMessageOnLabel={additionalMessageOnLabel}
/>
);
};

Expand All @@ -36,6 +43,7 @@ const TextFieldFormikWrapper: FC<TextFieldProps> = ({
required,
description,
label,
additionalMessageOnLabel,
additionalOnChangeCb,
placeholder,
type,
Expand All @@ -54,6 +62,7 @@ const TextFieldFormikWrapper: FC<TextFieldProps> = ({
description={description}
additionalOnChangeCb={additionalOnChangeCb}
label={label}
additionalMessageOnLabel={additionalMessageOnLabel}
placeholder={placeholder}
type={type}
autoComplete={autoComplete}
Expand Down
4 changes: 4 additions & 0 deletions src/components/formik/FormikField/ToggleField/ToggleField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const ToggleField: FC<ToggleFieldProps & FieldProps> = ({
id,
defaultChecked,
additionalOnChangeCb,
additionalMessageOnLabel,
error,
...props
}) => {
Expand All @@ -34,6 +35,7 @@ const ToggleField: FC<ToggleFieldProps & FieldProps> = ({
error={error}
onChangeInput={onChange}
defaultChecked={defaultChecked}
additionalMessageOnLabel={additionalMessageOnLabel}
/>
);
};
Expand All @@ -47,6 +49,7 @@ const ToggleFieldFormikWrapper: FC<ToggleFieldProps> = ({
description,
label,
additionalOnChangeCb,
additionalMessageOnLabel,
defaultChecked,
error,
}) => {
Expand All @@ -61,6 +64,7 @@ const ToggleFieldFormikWrapper: FC<ToggleFieldProps> = ({
description={description}
additionalOnChangeCb={additionalOnChangeCb}
label={label}
additionalMessageOnLabel={additionalMessageOnLabel}
defaultChecked={defaultChecked}
error={error}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const UploadFileField: FC<UploadFileFieldProps & FieldProps> = ({
form,
id,
additionalOnChangeCb,
additionalMessageOnLabel,
error,
...props
}) => {
Expand All @@ -35,6 +36,7 @@ const UploadFileField: FC<UploadFileFieldProps & FieldProps> = ({
id={id}
error={error}
onChangeInput={onChange}
additionalMessageOnLabel={additionalMessageOnLabel}
/>
);
};
Expand All @@ -48,6 +50,7 @@ const UploadFileFieldFormikWrapper: FC<UploadFileFieldProps> = ({
description,
label,
additionalOnChangeCb,
additionalMessageOnLabel,
placeholder,
uploadButtonText,
error,
Expand All @@ -63,6 +66,7 @@ const UploadFileFieldFormikWrapper: FC<UploadFileFieldProps> = ({
description={description}
additionalOnChangeCb={additionalOnChangeCb}
label={label}
additionalMessageOnLabel={additionalMessageOnLabel}
placeholder={placeholder}
uploadButtonText={uploadButtonText}
error={error}
Expand Down
12 changes: 11 additions & 1 deletion src/components/formik/FormikField/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ import type { UploadFileFieldProps } from "./UploadFileField";
import TextArea, { TextAreaProps } from "./TextArea";
import ToggleField from "./ToggleField";
import type { ToggleFieldProps } from "./ToggleField";
import StatefulToggleField from "./StatefulToggleField";
import type { StatefulToggleFieldProps } from "@instill-ai/design-system";

export { SingleSelect, TextField, UploadFileField, TextArea, ToggleField };
export {
SingleSelect,
TextField,
UploadFileField,
TextArea,
ToggleField,
StatefulToggleField,
};

export type {
SingleSelectProps,
TextFieldProps,
UploadFileFieldProps,
TextAreaProps,
ToggleFieldProps,
StatefulToggleFieldProps,
};
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const ConfigureDestinationForm: FC<ConfigureDestinationFormProps> = ({
id="destinationDefinition"
name="definition"
label="Data destination"
additionalMessageOnLabel={null}
value={selectedDestinationDefinitionOption}
options={syncDestinationDefinitionOptions}
additionalOnChangeCb={destinationDefinitionOnChangeCb}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const ConfigureModelForm: FC<ConfigureModelFormProps> = ({
id="description"
name="description"
label="Description"
additionalMessageOnLabel={null}
description="Fill with a short description of your model"
value={values.description}
error={errors.description || null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const ConfigureModelInstanceForm: FC<ConfigureModelInstanceFormProps> = ({
id="repo"
name="repo"
label="GitHub repository"
additionalMessageOnLabel={null}
description="The URL of a GitHub repositories/organizations, e.g. 'instill-ai/yolov4'."
value={values.repo}
error={errors.repo || null}
Expand All @@ -58,6 +59,7 @@ const ConfigureModelInstanceForm: FC<ConfigureModelInstanceFormProps> = ({
id="tag"
name="tag"
label="Tag"
additionalMessageOnLabel={null}
description="Tag of the GitHub repository, e.g., 'v0.1.0'."
value={values.tag}
error={errors.tag || null}
Expand All @@ -73,6 +75,7 @@ const ConfigureModelInstanceForm: FC<ConfigureModelInstanceFormProps> = ({
id="repoUrl"
name="repoUrl"
label="GitHub repository URL"
additionalMessageOnLabel={null}
description="GitHub repository URL, e.g., 'https://github.com/instill-ai/protobufs/tree/v2.0.0'."
value={values.repoUrl}
error={errors.repoUrl || null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ const ConfigurePipelineForm: FC<ConfigurePipelineFormProps> = ({
setDeletePipelineModalIsOpen(false);
}, [pipeline, amplitudeIsInit, router, deletePipeline]);

const determinePipelineState = useCallback(
(values: ConfigurePipelineFormValue) => {
switch (values.state) {
case "STATE_ACTIVE":
return true;
default:
return false;
}
},
[]
);

return (
<>
<Formik
Expand Down Expand Up @@ -165,6 +177,8 @@ const ConfigurePipelineForm: FC<ConfigurePipelineFormProps> = ({
id="pipelineState"
name="state"
label="State"
value={determinePipelineState(values)}
additionalMessageOnLabel={null}
defaultChecked={true}
error={errors?.state || null}
additionalOnChangeCb={null}
Expand All @@ -177,6 +191,7 @@ const ConfigurePipelineForm: FC<ConfigurePipelineFormProps> = ({
id="pipelineDescription"
name="description"
label="Description"
additionalMessageOnLabel={null}
description="Fill with a short description of your model"
value={values.description}
error={errors.description || null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const ConfigureSourceForm: FC<ConfigureSourceFormProps> = ({ source }) => {
id="sourceDefinition"
name="sourceDefinition"
label="Data source"
additionalMessageOnLabel={null}
disabled={canEdit ? false : true}
readOnly={false}
options={syncSourceDefinitionOptions}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ const CreateDestinationForm: FC = () => {
id="destinationDefinition"
name="destinationDefinition"
label="Destination type"
additionalMessageOnLabel={null}
options={syncDestinationDefinitionOptions}
value={selectedSyncDestinationDefinitionOption}
additionalOnChangeCb={destinationDefinitionOnChange}
Expand Down
Loading

0 comments on commit 9358247

Please sign in to comment.