Skip to content

Commit f7651d0

Browse files
Revert "Enhance ReactOnRails options management"
This reverts commit 2f99ea9.
1 parent 3a26fd1 commit f7651d0

File tree

3 files changed

+22
-78
lines changed

3 files changed

+22
-78
lines changed

node_package/src/ReactOnRails.client.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import type {
1515
AuthenticityHeaders,
1616
Store,
1717
StoreGenerator,
18-
ReactOnRailsOptions
1918
} from './types';
2019
import reactHydrateOrRender from './reactHydrateOrRender';
2120

@@ -34,14 +33,13 @@ Check your Webpack configuration. Read more at https://github.com/shakacode/reac
3433
/* eslint-enable @typescript-eslint/no-base-to-string */
3534
}
3635

37-
const DEFAULT_OPTIONS: ReactOnRailsOptions = {
36+
const DEFAULT_OPTIONS = {
3837
traceTurbolinks: false,
3938
turbo: false,
40-
rscPayloadGenerationUrlPath: '/rsc_payload',
4139
};
4240

4341
ctx.ReactOnRails = {
44-
options: { ...DEFAULT_OPTIONS },
42+
options: {},
4543
/**
4644
* Main entry point to using the react-on-rails npm package. This is how Rails will be able to
4745
* find you components for rendering.
@@ -119,29 +117,27 @@ ctx.ReactOnRails = {
119117
* Available Options:
120118
* `traceTurbolinks: true|false Gives you debugging messages on Turbolinks events
121119
* `turbo: true|false Turbo (the follower of Turbolinks) events will be registered, if set to true.
122-
* `rscPayloadGenerationUrlPath: string The path or url of the endpoint that will generate the RSC payload.
123120
*/
124-
setOptions(newOptions: Partial<ReactOnRailsOptions>): void {
125-
if (!newOptions || typeof newOptions !== 'object') {
126-
throw new Error('Error calling ReactOnRails.setOptions: newOptions must be a plain object.');
121+
setOptions(newOptions: {traceTurbolinks?: boolean, turbo?: boolean }): void {
122+
if (typeof newOptions.traceTurbolinks !== 'undefined') {
123+
this.options.traceTurbolinks = newOptions.traceTurbolinks;
124+
125+
// eslint-disable-next-line no-param-reassign
126+
delete newOptions.traceTurbolinks;
127127
}
128128

129-
const validOptionKeys = Object.keys(DEFAULT_OPTIONS);
130-
const providedOptionKeys = Object.keys(newOptions);
131-
132-
const invalidOptions = providedOptionKeys.filter(key => !validOptionKeys.includes(key));
133-
if (invalidOptions.length > 0) {
129+
if (typeof newOptions.turbo !== 'undefined') {
130+
this.options.turbo = newOptions.turbo;
131+
132+
// eslint-disable-next-line no-param-reassign
133+
delete newOptions.turbo;
134+
}
135+
136+
if (Object.keys(newOptions).length > 0) {
134137
throw new Error(
135-
`Invalid options passed to ReactOnRails.options: ${JSON.stringify(invalidOptions)}`,
138+
`Invalid options passed to ReactOnRails.options: ${JSON.stringify(newOptions)}`,
136139
);
137140
}
138-
139-
// Filter out undefined values before merging
140-
const definedOptions = Object.fromEntries(
141-
Object.entries(newOptions).filter(([_, value]) => value !== undefined)
142-
);
143-
144-
this.options = { ...this.options, ...definedOptions };
145141
},
146142

147143
/**
@@ -190,7 +186,7 @@ ctx.ReactOnRails = {
190186
* @param key
191187
* @returns option value
192188
*/
193-
option(key: keyof ReactOnRailsOptions): string | number | boolean | undefined {
189+
option(key: string): string | number | boolean | undefined {
194190
return this.options[key];
195191
},
196192

@@ -343,12 +339,12 @@ ctx.ReactOnRails = {
343339
},
344340

345341
resetOptions(): void {
346-
this.options = { ...DEFAULT_OPTIONS };
342+
this.options = Object.assign({}, DEFAULT_OPTIONS);
347343
},
348-
349-
isRSCBundle: false,
350344
};
351345

346+
ctx.ReactOnRails.resetOptions();
347+
352348
ClientStartup.clientStartup(ctx);
353349

354350
export * from './types';

node_package/src/types/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,6 @@ export interface Root {
169169
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type -- inherited from React 16/17, can't avoid here
170170
export type RenderReturnType = void | Element | Component | Root;
171171

172-
export interface ReactOnRailsOptions {
173-
traceTurbolinks: boolean;
174-
turbo: boolean;
175-
rscPayloadGenerationUrlPath: string;
176-
}
177-
178172
export interface ReactOnRails {
179173
register(components: Record<string, ReactComponentOrRenderFunction>): void;
180174
/** @deprecated Use registerStoreGenerators instead */

node_package/tests/ReactOnRails.test.jsx

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -55,53 +55,7 @@ describe('ReactOnRails', () => {
5555
it('setOptions method throws error for invalid options', () => {
5656
ReactOnRails.resetOptions();
5757
expect.assertions(1);
58-
expect(() => ReactOnRails.setOptions({ foobar: true })).toThrow(/Invalid options/);
59-
});
60-
61-
it('setOptions allows setting multiple options at once', () => {
62-
ReactOnRails.resetOptions();
63-
ReactOnRails.setOptions({
64-
traceTurbolinks: true,
65-
rscPayloadGenerationUrlPath: '/custom_rsc'
66-
});
67-
expect(ReactOnRails.option('traceTurbolinks')).toBe(true);
68-
expect(ReactOnRails.option('rscPayloadGenerationUrlPath')).toBe('/custom_rsc');
69-
});
70-
71-
it('setOptions preserves unspecified options when setting specific ones', () => {
72-
ReactOnRails.resetOptions();
73-
74-
ReactOnRails.setOptions({
75-
traceTurbolinks: true,
76-
turbo: true,
77-
rscPayloadGenerationUrlPath: '/custom_rsc'
78-
});
79-
80-
ReactOnRails.setOptions({
81-
rscPayloadGenerationUrlPath: '/different_path'
82-
});
83-
84-
expect(ReactOnRails.option('rscPayloadGenerationUrlPath')).toBe('/different_path');
85-
86-
expect(ReactOnRails.option('traceTurbolinks')).toBe(true);
87-
expect(ReactOnRails.option('turbo')).toBe(true);
88-
});
89-
90-
it('setOptions ignores undefined values', () => {
91-
ReactOnRails.resetOptions();
92-
ReactOnRails.setOptions({
93-
traceTurbolinks: true,
94-
turbo: true,
95-
rscPayloadGenerationUrlPath: '/custom_rsc'
96-
});
97-
98-
ReactOnRails.setOptions({
99-
rscPayloadGenerationUrlPath: undefined
100-
});
101-
102-
expect(ReactOnRails.option('rscPayloadGenerationUrlPath')).toBe('/custom_rsc');
103-
expect(ReactOnRails.option('traceTurbolinks')).toBe(true);
104-
expect(ReactOnRails.option('turbo')).toBe(true);
58+
expect(() => ReactOnRails.setOptions({ foobar: true })).toThrow(/Invalid option/);
10559
});
10660

10761
it('registerStore throws if passed a falsey object (null, undefined, etc)', () => {

0 commit comments

Comments
 (0)