Skip to content

Commit 09feb6f

Browse files
gettinToastygettinToasty
andauthored
Fix widget tests (#5816)
* Fix widget tests * Fix superchat goal * Fix custom fields jank * Fix reactivity * Fix existing goal styling * Fix goal mutation * Add error handling * Fix duration * Comment out color inputs for now --------- Co-authored-by: gettinToasty <sbeyer@logitech.com>
1 parent ae8e430 commit 09feb6f

12 files changed

Lines changed: 101 additions & 47 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@import '../../styles/index';
2+
3+
.goal-row {
4+
.padding();
5+
6+
display: flex;
7+
justify-content: space-between;
8+
border-bottom: 1px solid var(--border);
9+
10+
&:last-of-type {
11+
.margin-bottom();
12+
}
13+
}

app/components-react/widgets/GenericGoal.tsx

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useState } from 'react';
2-
import { Button, Menu } from 'antd';
1+
import React, { useEffect, useRef, useState } from 'react';
2+
import { Button, Menu, message } from 'antd';
33
import { $t } from 'services/i18n';
44
import { IWidgetCommonState, useWidget, WidgetModule, WidgetParams } from './common/useWidget';
55
import { WidgetLayout } from './common/WidgetLayout';
@@ -9,6 +9,8 @@ import { metadata } from '../shared/inputs/metadata';
99
import { WidgetType } from 'services/widgets';
1010
import { authorizedHeaders, jfetch } from 'util/requests';
1111
import { Services } from 'components-react/service-provider';
12+
import styles from './GenericGoal.m.less';
13+
import { assertIsDefined } from 'util/properties-type-guards';
1214

1315
interface IGoalState extends IWidgetCommonState {
1416
data: {
@@ -53,6 +55,7 @@ export function GenericGoal() {
5355
setSelectedTab,
5456
selectedTab,
5557
saveGoal,
58+
resetGoal,
5659
type,
5760
} = useGenericGoal();
5861

@@ -67,6 +70,10 @@ export function GenericGoal() {
6770
ends_at: '',
6871
});
6972

73+
useEffect(() => {
74+
message.config({ top: 270 });
75+
}, []);
76+
7077
function updateGoalCreate(key: string) {
7178
return (val: TInputValue) => {
7279
setGoalCreateValues({ ...goalCreateValues, [key]: val });
@@ -87,12 +94,18 @@ export function GenericGoal() {
8794
values={goalCreateValues}
8895
onChange={updateGoalCreate}
8996
/>
90-
<Button className="button button--action" onClick={() => saveGoal(goalCreateValues)}>
91-
{$t('Save Goal')}
97+
<Button
98+
className="button button--action"
99+
onClick={() => saveGoal(goalCreateValues)}
100+
style={{ marginBottom: 16 }}
101+
>
102+
{$t('Start Goal')}
92103
</Button>
93104
</>
94105
)}
95-
{!isLoading && selectedTab === 'goal' && hasGoal && <DisplayGoal goal={goalSettings} />}
106+
{!isLoading && selectedTab === 'goal' && hasGoal && (
107+
<DisplayGoal goal={goalSettings} resetGoal={resetGoal} />
108+
)}
96109
{!isLoading && selectedTab === 'general' && (
97110
<FormFactory
98111
metadata={visualMeta}
@@ -106,29 +119,31 @@ export function GenericGoal() {
106119
);
107120
}
108121

109-
function DisplayGoal(p: { goal: IGoalState['data']['goal'] }) {
110-
const { resetGoal } = useGenericGoal();
111-
122+
function DisplayGoal(p: { goal: IGoalState['data']['goal']; resetGoal: () => void }) {
112123
if (!p.goal) return <></>;
113124
return (
114125
<div className="section__body">
115-
<div className="goal-row">
126+
<div className={styles.goalRow}>
116127
<span>{$t('Title')}</span>
117128
<span>{p.goal.title}</span>
118129
</div>
119-
<div className="goal-row">
130+
<div className={styles.goalRow}>
120131
<span>{$t('Goal Amount')}</span>
121132
<span>{p.goal.goal_amount}</span>
122133
</div>
123-
<div className="goal-row">
134+
<div className={styles.goalRow}>
124135
<span>{$t('Current Amount')}</span>
125136
<span>{p.goal.current_amount}</span>
126137
</div>
127-
<div className="goal-row">
138+
<div className={styles.goalRow}>
128139
<span>{$t('Days Remaining')}</span>
129140
<span>{p.goal.to_go}</span>
130141
</div>
131-
<Button className="button button--soft-warning" onClick={resetGoal}>
142+
<Button
143+
className="button button--soft-warning"
144+
onClick={p.resetGoal}
145+
style={{ marginBottom: 16 }}
146+
>
132147
{$t('End Goal')}
133148
</Button>
134149
</div>
@@ -211,18 +226,31 @@ export class GenericGoalModule extends WidgetModule<IGoalState> {
211226
const url = this.config.goalUrl;
212227
if (!url) return;
213228
jfetch(new Request(url, { method: 'DELETE', headers: this.headers }));
229+
this.setGoalData(null);
214230
}
215231

216-
saveGoal(options: Dictionary<TInputValue>) {
232+
async saveGoal(options: Dictionary<TInputValue>) {
217233
const url = this.config.goalUrl;
218234
if (!url) return;
219-
jfetch(
220-
new Request(url, {
221-
method: 'POST',
222-
headers: this.headers,
223-
body: JSON.stringify(options),
224-
}),
225-
);
235+
try {
236+
const resp: IGoalState['data'] = await jfetch(
237+
new Request(url, {
238+
method: 'POST',
239+
headers: this.headers,
240+
body: JSON.stringify(options),
241+
}),
242+
);
243+
this.setGoalData(resp.goal);
244+
} catch (e: unknown) {
245+
message.error({ content: (e as any).result.message, duration: 2 });
246+
}
247+
}
248+
249+
private setGoalData(goal: IGoalState['data']['goal']) {
250+
assertIsDefined(this.state.widgetData.data);
251+
this.state.mutate(state => {
252+
state.widgetData.data.goal = goal;
253+
});
226254
}
227255

228256
patchAfterFetch(data: IGoalState['data']): IGoalState['data'] {

app/components-react/widgets/common/WidgetLayout.m.less

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
@import "../../../styles/index.less";
1+
@import '../../../styles/index.less';
22

33
// TODO: This is also used by the new filters window
44
// We should probably make these menu styles global
55
// across the entire app.
66
.widget-layout {
7-
87
height: 100%;
98

109
// collapse
@@ -19,7 +18,7 @@
1918
}
2019

2120
:global(.ant-collapse-content),
22-
:global(.ant-collapse-item){
21+
:global(.ant-collapse-item) {
2322
background-color: var(--section-alt);
2423
}
2524

@@ -33,6 +32,7 @@
3332
width: 270px;
3433
border-right: 1px solid var(--border);
3534
background-color: var(--section);
35+
height: 100%;
3636
}
3737

3838
:global(.ant-collapse-content-box .ant-menu-root) {
@@ -55,7 +55,7 @@
5555
:global(.ant-menu-item .anticon-question-circle) {
5656
visibility: hidden;
5757
}
58-
:global(.ant-menu-item-selected .anticon-question-circle){
58+
:global(.ant-menu-item-selected .anticon-question-circle) {
5959
visibility: visible;
6060
}
6161

@@ -108,4 +108,8 @@
108108
height: 100%;
109109
overflow: auto;
110110
}
111+
112+
:global(.ant-row) {
113+
flex-flow: nowrap;
114+
}
111115
}

app/components-react/widgets/common/WidgetLayout.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ const PREVIEW_HEIGHT = 250;
2424
* If "basic" layout selected then display 1 column or 2 columns depending
2525
* on how many children have been provided to props
2626
*/
27-
export function WidgetLayout(p: { layout?: TWidgetLayoutType; children: TLayoutChildren; showDisplay?: boolean }) {
27+
export function WidgetLayout(p: {
28+
layout?: TWidgetLayoutType;
29+
children: TLayoutChildren;
30+
showDisplay?: boolean;
31+
}) {
2832
const layout = p.layout || 'basic';
2933
switch (layout) {
3034
case 'basic':
@@ -173,4 +177,4 @@ function getLayoutPanels(layoutChildren: TLayoutChildren) {
173177
[MenuPanel, ContentPanel] = [null, layoutChildren];
174178
}
175179
return { MenuPanel, ContentPanel };
176-
}
180+
}

app/components-react/widgets/common/WidgetWindow.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const components = {
3737
StarsGoal: [GenericGoal, GenericGoalModule],
3838
SubGoal: [GenericGoal, GenericGoalModule],
3939
SubscriberGoal: [GenericGoal, GenericGoalModule],
40+
SuperchatGoal: [GenericGoal, GenericGoalModule],
4041
ChatBox: [ChatBox, ChatBoxModule],
4142
// ChatHighlight
4243
// Credits

app/components-react/widgets/common/useWidget.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface IWidgetCommonState {
3737
export interface IWidgetState {
3838
data: {
3939
settings: any;
40+
goal?: any;
4041
};
4142
}
4243

app/services/sources/sources.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ export class SourcesService extends StatefulService<ISourcesState> {
816816
'StarsGoal',
817817
'SubGoal',
818818
'SubscriberGoal',
819+
'SuperchatGoal',
819820
'ChatBox',
820821
// TODO:
821822
// 'ChatHighlight',

app/styles/antd/antd.less

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ body {
1313
margin: 0;
1414
padding: 0;
1515
background-color: var(--background);
16+
position: relative;
1617
}
1718

1819
#mainWrapper .react {
1920
// justify all inputs of form to the right in the modal footer
20-
.ant-modal-footer .ant-form-inline {
21+
.ant-modal-footer .ant-form-inline {
2122
justify-content: flex-end;
2223
}
2324

@@ -76,7 +77,7 @@ body {
7677

7778
// disable unnecessary animation in checkboxes
7879
.ant-checkbox-inner,
79-
.ant-checkbox-inner::after, {
80+
.ant-checkbox-inner::after {
8081
transition: none;
8182
}
8283
.ant-checkbox-checked::after {
@@ -97,11 +98,10 @@ body {
9798
}
9899

99100
// set a "text-style" cursor for file inputs instead of "disabled"
100-
.ant-input.ant-input-disabled[data-type="file"] {
101+
.ant-input.ant-input-disabled[data-type='file'] {
101102
cursor: text;
102103
}
103104

104-
105105
// fix list input label wrapping with long names
106106
.ant-select-selection-item {
107107
.ant-row {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { TextInputController } from './text';
2+
3+
export class ColorInputController extends TextInputController {}

test/helpers/modules/forms/inputs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ export { BoolButtonInputController } from './bool-button';
99
export { SliderInputController } from './slider';
1010
export { FileInputController } from './file';
1111
export { RadioInputController } from './radio';
12+
export { ColorInputController } from './color';

0 commit comments

Comments
 (0)