Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix form builder settings after switching to new locale #3546

Open
wants to merge 9 commits into
base: next
Choose a base branch
from
4 changes: 3 additions & 1 deletion packages/api-form-builder/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import createCruds from "./plugins/crud";
import graphql from "./plugins/graphql";
import triggerHandlers from "./plugins/triggers";
import settings from "./plugins/settings";
import validators from "./plugins/validators";
import formsGraphQL from "./plugins/graphql/form";
import formSettingsGraphQL from "./plugins/graphql/formSettings";
Expand All @@ -17,6 +18,7 @@ export const createFormBuilder = (params: CreateFormBuilderParams) => {
triggerHandlers,
validators,
formsGraphQL,
formSettingsGraphQL
formSettingsGraphQL,
settings
];
};
12 changes: 8 additions & 4 deletions packages/api-form-builder/src/plugins/crud/settings.crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const createSettingsCrud = (params: CreateSettingsCrudParams): SettingsCR
onSettingsBeforeDelete,
onSettingsAfterDelete,
async getSettings(this: FormBuilder, params) {
const { auth, throwOnNotFound } = params || {};
const { auth, throwOnNotFound, locale } = params || {};

if (auth !== false) {
await settingsPermissions.ensure();
Expand All @@ -70,7 +70,7 @@ export const createSettingsCrud = (params: CreateSettingsCrudParams): SettingsCR
try {
settings = await this.storageOperations.getSettings({
tenant: getTenant().id,
locale: getLocale().code
locale: locale || getLocale().code
});
} catch (ex) {
throw new WebinyError(
Expand All @@ -89,7 +89,11 @@ export const createSettingsCrud = (params: CreateSettingsCrudParams): SettingsCR

const data = await formBuilderSettings.toJSON();

const original = await this.getSettings({ auth: false });
const original = await this.getSettings({
auth: false,
locale: input.locale
});

if (original) {
throw new WebinyError(
`"Form Builder" settings already exist.`,
Expand All @@ -106,7 +110,7 @@ export const createSettingsCrud = (params: CreateSettingsCrudParams): SettingsCR
domain: data.domain,
reCaptcha: data.reCaptcha,
tenant: getTenant().id,
locale: getLocale().code
locale: input.locale || getLocale().code
};
try {
await onSettingsBeforeCreate.publish({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ContextPlugin } from "@webiny/api";
import { FormBuilderContext } from "~/types";

export const createSettingsForNewLocale = new ContextPlugin<FormBuilderContext>(context => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once a locale has been deleted, let's also delete settings.

context.i18n.locales.onLocaleAfterCreate.subscribe(async ({ locale }) => {
const existingSettings = await context.formBuilder.getSettings({
auth: false,
locale: locale.code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need locale here? Wouldn't the current locale automatically be pulled above?

If there's no really a need to expose locale param, let's maybe not do it.

});

if (!existingSettings) {
await context.formBuilder.createSettings({ locale: locale.code });
}
});
});
3 changes: 3 additions & 0 deletions packages/api-form-builder/src/plugins/settings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createSettingsForNewLocale } from "./createSettingsForNewLocale";

export default [createSettingsForNewLocale];
1 change: 1 addition & 0 deletions packages/api-form-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ export interface Settings {
export interface SettingsCRUDGetParams {
auth?: boolean;
throwOnNotFound?: boolean;
locale?: string;
}

/**
Expand Down