0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-26 02:50:28 +01:00

REFACTOR: abstract subscription logic to reduce code

Dieser Commit ist enthalten in:
merefield 2021-10-25 18:55:28 +01:00
Ursprung 7b129debac
Commit 9350db5424
3 geänderte Dateien mit 39 neuen und 128 gelöschten Zeilen

Datei anzeigen

@ -5,10 +5,8 @@ import { computed } from "@ember/object";
import I18n from "I18n"; import I18n from "I18n";
import wizardSchema, { import wizardSchema, {
customFieldsKlassesRequiringAdditionalSubscription, requiringAdditionalSubscription,
customFieldsKlassSubscriptionLevel, subscriptionLevel,
customFieldsTypesRequiringAdditionalSubscription,
customFieldsTypeSubscriptionLevel,
} from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema"; } from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema";
export default Component.extend({ export default Component.extend({
@ -41,15 +39,15 @@ export default Component.extend({
@discourseComputed("subscription") @discourseComputed("subscription")
customFieldTypes(subscription) { customFieldTypes(subscription) {
let unsubscribedCustomFields = customFieldsTypesRequiringAdditionalSubscription( let unsubscribedCustomFields = requiringAdditionalSubscription(
subscription subscription, "custom_fields", "types"
); );
return wizardSchema.custom_field.types.reduce((result, type) => { return wizardSchema.custom_field.types.reduce((result, type) => {
let disabled = unsubscribedCustomFields.includes(type); let disabled = unsubscribedCustomFields.includes(type);
result.push({ result.push({
id: type, id: type,
name: I18n.t(`admin.wizard.custom_field.type.${type}`), name: I18n.t(`admin.wizard.custom_field.type.${type}`),
subscription: customFieldsTypeSubscriptionLevel(type), subscription: subscriptionLevel(type, "custom_fields", "types"),
disabled: disabled, disabled: disabled,
}); });
return result; return result;
@ -58,15 +56,15 @@ export default Component.extend({
@discourseComputed("subscription") @discourseComputed("subscription")
customFieldKlasses(subscription) { customFieldKlasses(subscription) {
let unsubscribedCustomFields = customFieldsKlassesRequiringAdditionalSubscription( let unsubscribedCustomFields = requiringAdditionalSubscription(
subscription subscription, "custom_fields", "klasses"
); );
return wizardSchema.custom_field.klasses.reduce((result, klass) => { return wizardSchema.custom_field.klasses.reduce((result, klass) => {
let disabled = unsubscribedCustomFields.includes(klass); let disabled = unsubscribedCustomFields.includes(klass);
result.push({ result.push({
id: klass, id: klass,
name: I18n.t(`admin.wizard.custom_field.klass.${klass}`), name: I18n.t(`admin.wizard.custom_field.klass.${klass}`),
subscription: customFieldsKlassSubscriptionLevel(klass), subscription: subscriptionLevel(klass, "custom_fields", "klasses"),
disabled: disabled, disabled: disabled,
}); });
return result; return result;

Datei anzeigen

@ -1,8 +1,7 @@
import { default as discourseComputed } from "discourse-common/utils/decorators"; import { default as discourseComputed } from "discourse-common/utils/decorators";
import wizardSchema, { import wizardSchema, {
actionsRequiringAdditionalSubscription,
requiringAdditionalSubscription, requiringAdditionalSubscription,
actionSubscriptionLevel, subscriptionLevel,
} from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema"; } from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema";
import { empty, equal, or } from "@ember/object/computed"; import { empty, equal, or } from "@ember/object/computed";
import { notificationLevels, selectKitContent } from "../lib/wizard"; import { notificationLevels, selectKitContent } from "../lib/wizard";
@ -98,19 +97,15 @@ export default Component.extend(UndoChanges, {
return apis.find((a) => a.name === api).endpoints; return apis.find((a) => a.name === api).endpoints;
}, },
@discourseComputed("subscribed", "subscription") @discourseComputed("subscription")
actionTypes(subscribed, subscription) { actionTypes(subscription) {
let unsubscribedActions = requiringAdditionalSubscription (subscription, "actions"); let unsubscribedActions = requiringAdditionalSubscription (subscription, "actions", "");
debugger;
let unsubscribedActionslong = actionsRequiringAdditionalSubscription(
subscription
);
return Object.keys(wizardSchema.action.types).reduce((result, type) => { return Object.keys(wizardSchema.action.types).reduce((result, type) => {
let disabled = unsubscribedActions.includes(type); let disabled = unsubscribedActions.includes(type);
result.push({ result.push({
id: type, id: type,
name: I18n.t(`admin.wizard.action.${type}.label`), name: I18n.t(`admin.wizard.action.${type}.label`),
subscription: actionSubscriptionLevel(type), subscription: subscriptionLevel(type, "actions", ""),
disabled: disabled, disabled: disabled,
}); });
return result; return result;

Datei anzeigen

@ -193,17 +193,6 @@ const action = {
"members_visibility_level", "members_visibility_level",
], ],
required: ["id", "type"], required: ["id", "type"],
subscriptionTypes: [
"send_message",
"add_to_group",
"create_category",
"create_group",
"send_to_api",
],
actionTypesWithSubscription: {
standard: ["send_message", "add_to_group", "watch_categories"],
business: ["create_category", "create_group", "send_to_api"],
},
dependent: {}, dependent: {},
objectArrays: {}, objectArrays: {},
}; };
@ -211,14 +200,6 @@ const action = {
const custom_field = { const custom_field = {
klasses: ["topic", "post", "group", "category"], klasses: ["topic", "post", "group", "category"],
types: ["string", "boolean", "integer", "json"], types: ["string", "boolean", "integer", "json"],
customFieldKlassWithSubscription: {
standard: [],
business: ["group", "category"],
},
customFieldTypeWithSubscription: {
standard: ["json"],
business: [],
},
} }
const subscription_levels = { const subscription_levels = {
@ -249,8 +230,10 @@ const wizardSchema = {
} }
export function requiringAdditionalSubscription( export function requiringAdditionalSubscription(
currentSubscription, category currentSubscription, category, subCategory
) { ) {
switch (category) {
case "actions":
switch (currentSubscription) { switch (currentSubscription) {
case "business": case "business":
return []; return [];
@ -261,10 +244,24 @@ export function requiringAdditionalSubscription(
subscription_levels["business"].[category] subscription_levels["business"].[category]
); );
} }
case "custom_fields":
switch (currentSubscription) {
case "business":
return [];
case "standard":
return subscription_levels["business"].[category].[subCategory];
default:
return subscription_levels["standard"].[category].[subCategory].concat(
subscription_levels["business"].[category].[subCategory]
);
}
default:
return [];
}
} }
export function SubscriptionLevel(category, type, subCategory) { export function subscriptionLevel(type, category, subCategory) {
switch (category) { switch (category) {
case "actions": case "actions":
if (subscription_levels.["business"].actions.includes(type)) { if (subscription_levels.["business"].actions.includes(type)) {
@ -291,85 +288,6 @@ export function SubscriptionLevel(category, type, subCategory) {
} }
} }
export function actionsRequiringAdditionalSubscription(
currentSubscription
) {
switch (currentSubscription) {
case "business":
return [];
case "standard":
return action.actionTypesWithSubscription["business"];
default:
return action.actionTypesWithSubscription["standard"].concat(
action.actionTypesWithSubscription["business"]
);
}
}
export function actionSubscriptionLevel(type) {
if (action.actionTypesWithSubscription["business"].includes(type)) {
return "business"
} else {
if (action.actionTypesWithSubscription["standard"].includes(type)) {
return "standard"
} else {
return ""
}
}
}
export function customFieldsKlassesRequiringAdditionalSubscription(
currentSubscription
) {
switch (currentSubscription) {
case "business":
return [];
case "standard":
return custom_field.customFieldKlassWithSubscription["business"];
default:
return custom_field.customFieldKlassWithSubscription["business"].concat(custom_field.customFieldKlassWithSubscription["standard"]);
}
}
export function customFieldsKlassSubscriptionLevel(type) {
if (custom_field.customFieldKlassWithSubscription["business"].includes(type)) {
return "business"
} else {
if (custom_field.customFieldKlassWithSubscription["standard"].includes(type)) {
return "standard"
} else {
return ""
}
}
}
export function customFieldsTypesRequiringAdditionalSubscription(
currentSubscription
) {
switch (currentSubscription) {
case "business":
return [];
case "standard":
return custom_field.customFieldTypeWithSubscription["business"];
default:
return custom_field.customFieldTypeWithSubscription["business"].concat(custom_field.customFieldTypeWithSubscription["standard"]);
}
}
export function customFieldsTypeSubscriptionLevel(type) {
if (custom_field.customFieldTypeWithSubscription["business"].includes(type)) {
return "business"
} else {
if (custom_field.customFieldTypeWithSubscription["standard"].includes(type)) {
return "standard"
} else {
return ""
}
}
}
export function buildFieldTypes(types) { export function buildFieldTypes(types) {
wizardSchema.field.types = types; wizardSchema.field.types = types;
} }