Skip to content

Commit

Permalink
FormattedMessage shows at least i18n key when no message is found
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-burel committed Dec 17, 2021
1 parent 21da0e5 commit b8d05a1
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
44 changes: 38 additions & 6 deletions packages/graphql/extendModel.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@ import {
} from "./server/resolvers";
// don't forget to reexport common code
export * from "./extendModel";
import extendModel, { GraphqlModelOptions } from "./extendModel";
import extendModel, {
GraphqlModelDefinition,
GraphqlModelOptions,
} from "./extendModel";
import cloneDeep from "lodash/cloneDeep";
import isEmpty from "lodash/isEmpty";

import merge from "lodash/merge";
/**
* This type is meant to be exposed server side
*  @server-only
*/
export interface GraphqlModelOptionsServer extends GraphqlModelOptions {
/** Custom query resolvers (single, multi). Set to "null" if you don't want Vulcan to set any resolvers. Leave undefined if you want to use default resolvers. */
queryResolvers?: Partial<QueryResolverDefinitions>;
queryResolvers?: Partial<QueryResolverDefinitions> | null;
/** Custom mutation resolvers (create, update, delete). Set to "null" if you don't want Vulcan to set any resolvers. Leave undefined if you want to use default resolvers. */
mutationResolvers?: Partial<MutationResolverDefinitions>;
mutationResolvers?: Partial<MutationResolverDefinitions> | null;
callbacks?: MutationCallbackDefinitions;
}

Expand Down Expand Up @@ -99,9 +104,10 @@ const extendSchemaServer = (
};

/**
* Adds server-only fields as well
* @param options
* @returns
* Plugin function that adds graphql options to a generic model,
* including server-side fields
*
* NOTE: should not be used directly, prefer calling "createGraphqlModelServer"
*/
export const extendModelServer =
(
Expand Down Expand Up @@ -145,8 +151,13 @@ export interface CreateGraphqlModelOptionsServer
extends CreateModelOptions<VulcanGraphqlSchemaServer> {
graphql: GraphqlModelOptionsServer;
}
/**
* Server-side definition of a model
*/
export type GraphqlModelDefinitionServer = CreateGraphqlModelOptionsServer;

/**
* Create a graphql model, accepting server-options
* @server-only
*/
export const createGraphqlModelServer = (
Expand All @@ -161,6 +172,27 @@ export const createGraphqlModelServer = (
return model;
};

/**
* Adds server only options to a model
* @param fullModelDef
* @param extensionModelDef
* @returns
*/
export const mergeModelDefinitionServer = (
fullModelDef: GraphqlModelDefinition,
/**
* Partial model definition, adding some server-only options to an existing model
*/
extensionModelDef: Omit<Partial<GraphqlModelDefinitionServer>, "graphql"> & {
graphql?: Partial<GraphqlModelDefinitionServer["graphql"]>;
}
) => {
return merge({}, fullModelDef, extensionModelDef);
};

/**
*/

//// CODE FROM CREATE COLLECTION
//import { Mongo } from "meteor/mongo";
//import SimpleSchema from "simpl-schema";
Expand Down
11 changes: 10 additions & 1 deletion packages/graphql/extendModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export interface GraphqlModelOptionsShared extends GraphqlModelOptions {
callbacks?: never;
}

// Reusable model extension function
/**
* Plugin function that adds graphql options to a generic model
*
* NOTE: should not be used directly, prefer calling "createGraphqlModel"
*/
const extendModel =
(options: GraphqlModelOptions) /*: ExtendModelFunc<VulcanGraphqlModel>*/ =>
(model: VulcanModel): VulcanGraphqlModel => {
Expand Down Expand Up @@ -93,6 +97,11 @@ export interface CreateGraphqlModelOptionsShared
// => it will display nicer messages when you try to mistakenly use a server-only field
graphql: GraphqlModelOptionsShared;
}
/**
* Definition of a model, to be passed to "createGraphqlModel"
*/
export type GraphqlModelDefinition = CreateGraphqlModelOptionsShared;

/**
* Let's you create a full-fledged graphql model
*
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/lib/modules/FormattedMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const FormattedMessage = ({
const cssClass = `i18n-message ${className}`;

// if message is empty (no message found AND no default message), use [id]
if (message === "") {
if (!message /* === ""*/) {
message = `[${id}]`;
}

Expand Down
28 changes: 28 additions & 0 deletions packages/i18n/lib/modules/tests/FormattedMessage.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from "react";
import { Story, Meta } from "@storybook/react";
import { FormattedMessage, FormattedMessageProps } from "../FormattedMessage";

export default {
component: FormattedMessage,
title: "FormattedMessage",
decorators: [
(Story) => (
// Replace by VulcanComponents if you need them
<div>
<Story />
</div>
),
],
args: {},
parameters: { actions: { argTypesRegex: "^.*Callback$" } },
} as Meta<FormattedMessageProps>;

const FormattedMessageTemplate: Story<FormattedMessageProps> = (args) => (
<FormattedMessage {...args} />
);
export const DefaultFormattedMessage = FormattedMessageTemplate.bind({});

export const UnknownId = FormattedMessageTemplate.bind({});
UnknownId.args = {
id: "unknown-id-should-be-displayed-as-is",
};
1 change: 1 addition & 0 deletions packages/react-ui/components/MutationButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export const MutationButton = (props: MutationButtonProps) => {
await successCallback(result);
}
} catch (error) {
// TODO: may not work because the mutationFunc may not throw in case of error
setError(error);
if (errorCallback) {
await errorCallback(error);
Expand Down

0 comments on commit b8d05a1

Please sign in to comment.