Skip to content

Commit

Permalink
Fidgets live update (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
glebovsky authored Jul 28, 2024
1 parent 9d2bac5 commit 4253ec9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/common/components/organisms/FidgetSettingsEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type FidgetSettingsEditorProps = {
fidgetId: string;
readonly properties: FidgetProperties;
settings: FidgetSettings;
onSave: (settings: FidgetSettings) => void;
onSave: (settings: FidgetSettings, shouldUnselect?: boolean) => void;
unselect: () => void;
removeFidget: (fidgetId: string) => void;
};
Expand Down Expand Up @@ -97,7 +97,8 @@ const FidgetSettingsGroup: React.FC<{
fields: FidgetFieldConfig[];
state: FidgetSettings;
setState: (state: FidgetSettings) => void;
}> = ({ fields, state, setState, fidgetId }) => {
onSave: (state: FidgetSettings) => void;
}> = ({ fields, state, setState, onSave, fidgetId }) => {
return (
<>
{fields.map((field, i) => (
Expand All @@ -107,10 +108,13 @@ const FidgetSettingsGroup: React.FC<{
id={`${fidgetId}-${i}-${field.fieldName}`}
value={state[field.fieldName]}
onChange={(val) => {
setState({
const data = {
...state,
[field.fieldName]: val,
});
};

setState(data);
onSave(data);
}}
hide={field.disabledIf && field.disabledIf(state)}
/>
Expand All @@ -136,7 +140,7 @@ export const FidgetSettingsEditor: React.FC<FidgetSettingsEditorProps> = ({

const _onSave = (e) => {
e.preventDefault();
onSave(state);
onSave(state, true);
analytics.track(AnalyticsEvent.EDIT_FIDGET, {
fidgetType: properties.fidgetName,
});
Expand Down Expand Up @@ -194,6 +198,7 @@ export const FidgetSettingsEditor: React.FC<FidgetSettingsEditorProps> = ({
fields={groupedFields.settings}
state={state}
setState={setState}
onSave={onSave}
/>
</TabsContent>
{groupedFields.style.length > 0 && (
Expand All @@ -203,6 +208,7 @@ export const FidgetSettingsEditor: React.FC<FidgetSettingsEditorProps> = ({
fields={groupedFields.style}
state={state}
setState={setState}
onSave={onSave}
/>
</TabsContent>
)}
Expand All @@ -213,6 +219,7 @@ export const FidgetSettingsEditor: React.FC<FidgetSettingsEditorProps> = ({
fields={groupedFields.code}
state={state}
setState={setState}
onSave={onSave}
/>
</TabsContent>
)}
Expand Down
9 changes: 7 additions & 2 deletions src/common/fidgets/FidgetWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ export function FidgetWrapper({

const [doubleCheck, setDoubleCheck] = useState(false);

const onSave = async (newSettings: FidgetSettings) => {
const onSave = async (
newSettings: FidgetSettings,
shouldUnselect?: boolean,
) => {
try {
await saveConfig({
...bundle.config,
Expand All @@ -98,7 +101,9 @@ export function FidgetWrapper({
toast.error("Failed to save fidget settings", { duration: 1000 });
}

unselect();
if (shouldUnselect) {
unselect();
}
};

function unselect() {
Expand Down

0 comments on commit 4253ec9

Please sign in to comment.