-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* your name translation fixed * do translation outside Components as that is needed by all components. Allows the code to stay DRY --------- Co-authored-by: Hariom <[email protected]>
- Loading branch information
1 parent
9a473d5
commit d0a7eb1
Showing
6 changed files
with
68 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type z from "zod"; | ||
|
||
import type { useLocale } from "@calcom/lib/hooks/useLocale"; | ||
|
||
import { fieldTypesConfigMap } from "../fieldTypes"; | ||
import type { fieldSchema } from "../schema"; | ||
|
||
type ConfigVariants = NonNullable<ReturnType<typeof getConfig>>["variants"]; | ||
type Field = z.infer<typeof fieldSchema>; | ||
type Translate = ReturnType<typeof useLocale>["t"]; | ||
|
||
function getTranslatedConfigVariants(configVariants: ConfigVariants, translate: Translate) { | ||
return Object.entries(configVariants).reduce((variantsConfigVariants, [variantName, variant]) => { | ||
const translatedFields = variant.fields.map((field) => { | ||
const label = field.label ?? ""; | ||
const placeholder = field.placeholder ?? ""; | ||
return { | ||
...field, | ||
label: translate(label), | ||
placeholder: translate(placeholder), | ||
}; | ||
}); | ||
variantsConfigVariants[variantName] = { | ||
...variant, | ||
fields: translatedFields, | ||
}; | ||
|
||
return variantsConfigVariants; | ||
}, {} as typeof configVariants); | ||
} | ||
|
||
/** | ||
* Get's the field's variantsConfig and if not available, then it will get the default variantsConfig from the fieldTypesConfigMap | ||
*/ | ||
export const getConfig = (field: Pick<Field, "variantsConfig" | "type">) => { | ||
const fieldVariantsConfig = field.variantsConfig; | ||
const fieldTypeConfig = fieldTypesConfigMap[field.type as keyof typeof fieldTypesConfigMap]; | ||
|
||
if (!fieldTypeConfig) throw new Error(`Invalid field.type ${field.type}}`); | ||
|
||
const defaultVariantsConfig = fieldTypeConfig?.variantsConfig?.defaultValue; | ||
const variantsConfig = fieldVariantsConfig || defaultVariantsConfig; | ||
|
||
if (fieldTypeConfig.propsType === "variants" && !variantsConfig) { | ||
throw new Error(`propsType variants must have variantsConfig`); | ||
} | ||
return variantsConfig; | ||
}; | ||
|
||
export const getTranslatedConfig = (field: Pick<Field, "variantsConfig" | "type">, translate: Translate) => { | ||
const variantsConfig = getConfig(field); | ||
if (!variantsConfig) return variantsConfig; | ||
const newVariantsConfigVariants = getTranslatedConfigVariants(variantsConfig.variants, translate); | ||
|
||
return { | ||
...variantsConfig, | ||
variants: newVariantsConfigVariants, | ||
}; | ||
}; |