Skip to content
This repository has been archived by the owner on Mar 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #225 from RaenonX-DL/dev
Browse files Browse the repository at this point in the history
v2.11.1 Release
  • Loading branch information
RaenonX authored Jul 29, 2021
2 parents b6fcf82 + 7781335 commit fb4ecb8
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 68 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dragalia-site-front",
"description": "Frontend of DL info website by OM.",
"repository": "https://github.com/RaenonX-DL/dragalia-site-front",
"version": "2.11.0",
"version": "2.11.1",
"engines": {
"node": "14.x",
"npm": "^7.12.0"
Expand Down
2 changes: 1 addition & 1 deletion src/components/elements/posts/analysis/form/chara/body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const CharaAnalysisForm = <P extends CharaAnalysisPayload>({
minLength={2}
getArray={(payload) => payload.skills}
setArray={setSkills}
updateElementValue={(element, key, value) => element[key] = value}
getUpdatedElement={(element, key, value) => ({...element, [key]: value})}
generateNewElement={generateNewCharaSkill}
renderEntries={(skill, onChange) => (
<div className="mt-2">
Expand Down
31 changes: 30 additions & 1 deletion src/components/elements/posts/analysis/form/chara/new.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ describe('New character analysis form', () => {
backup: {
analysis: {
chara: {
uid: 'uid',
unitId: 10950101,
type: UnitType.CHARACTER,
lang: SupportedLanguages.CHT,
Expand Down Expand Up @@ -204,4 +203,34 @@ describe('New character analysis form', () => {

await waitFor(() => expect(fnClear).toHaveBeenCalled());
});

it('should not backup UID', async () => {
const fnBackup = jest.spyOn(backupDispatchers, BackupDispatcherName.BACKUP_CHARA_ANALYSIS);

const {rerender} = renderReact(
() => <AnalysisFormCharaNew/>,
{
user: {isAdmin: true},
preloadState: {
backup: {
analysis: {
chara: {
...generatePayload(SupportedLanguages.EN, 'admin'),
summary: 'sum',
},
dragon: null,
},
quest: null,
},
},
},
);

const summary = screen.getByText('sum');
typeInput(summary, 'summary', {rerender});

// @ts-ignore
const {uid} = fnBackup.mock.calls[0][0];
expect(uid).toBeUndefined();
});
});
9 changes: 5 additions & 4 deletions src/components/elements/posts/analysis/form/chara/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ export const AnalysisFormCharaNew = () => {
return (
<AnalysisFormChara
formState={formState}
setFormState={(newState) => {
dispatch(backupDispatchers.backupCharaAnalysis(newState.payload));
setFormState(newState);
}}
setFormState={setFormState}
fnSendRequest={ApiRequestSender.analysisPublishChara}
onSubmitSuccess={() => dispatch(backupDispatchers.clearCharaAnalysis())}
onUpdated={(payload) => {
const {uid, ...rest} = payload;
dispatch(backupDispatchers.backupCharaAnalysis(rest));
}}
/>
);
};
31 changes: 30 additions & 1 deletion src/components/elements/posts/analysis/form/dragon/new.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ describe('New dragon analysis form', () => {
analysis: {
chara: null,
dragon: {
uid: '',
unitId: 20950101,
lang: SupportedLanguages.EN,
type: UnitType.DRAGON,
Expand Down Expand Up @@ -204,4 +203,34 @@ describe('New dragon analysis form', () => {

await waitFor(() => expect(fnClear).toHaveBeenCalled());
});

it('should not backup UID', async () => {
const fnBackup = jest.spyOn(backupDispatchers, BackupDispatcherName.BACKUP_DRAGON_ANALYSIS);

const {rerender} = renderReact(
() => <AnalysisFormDragonNew/>,
{
user: {isAdmin: true},
preloadState: {
backup: {
analysis: {
chara: null,
dragon: {
...generatePayload(SupportedLanguages.EN, 'admin'),
summary: 'sum',
},
},
quest: null,
},
},
},
);

const summary = screen.getByText('sum');
typeInput(summary, 'summary', {rerender});

// @ts-ignore
const {uid} = fnBackup.mock.calls[0][0];
expect(uid).toBeUndefined();
});
});
9 changes: 5 additions & 4 deletions src/components/elements/posts/analysis/form/dragon/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ export const AnalysisFormDragonNew = () => {
return (
<AnalysisFormDragon
formState={formState}
setFormState={(newState) => {
dispatch(backupDispatchers.backupDragonAnalysis(newState.payload));
setFormState(newState);
}}
setFormState={setFormState}
fnSendRequest={ApiRequestSender.analysisPublishDragon}
onSubmitSuccess={() => dispatch(backupDispatchers.clearDragonAnalysis())}
onUpdated={(payload) => {
const {uid, ...rest} = payload;
dispatch(backupDispatchers.backupDragonAnalysis(rest));
}}
/>
);
};
2 changes: 1 addition & 1 deletion src/components/elements/posts/quest/form/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const QuestPostForm = <P extends QuestPostPublishPayload, R extends Quest
<hr/>
<FormGeneralInfo formState={formState} setPayload={setPayload}/>
<hr/>
<FormPositional formState={formState} setState={setFormState}/>
<FormPositional formState={formState} setPayload={setPayload}/>
<hr/>
<FormAddendum formState={formState} setPayload={setPayload}/>
</>
Expand Down
25 changes: 24 additions & 1 deletion src/components/elements/posts/quest/form/new.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ describe('New quest post form', () => {
analysis: {chara: null, dragon: null},
quest: {
seqId: 7,
uid: 'googleUid',
lang: SupportedLanguages.CHT,
title: 'ttl',
general: 'gen',
Expand Down Expand Up @@ -201,4 +200,28 @@ describe('New quest post form', () => {

await waitFor(() => expect(fnClear).toHaveBeenCalled());
});

it('should not backup UID', async () => {
const fnBackup = jest.spyOn(backupDispatchers, BackupDispatcherName.BACKUP_QUEST_GUIDE);

const {rerender} = renderReact(
() => <QuestNewForm/>,
{
user: {isAdmin: true},
preloadState: {
backup: {
analysis: {chara: null, dragon: null},
quest: {...generatePayload(SupportedLanguages.EN, 'admin'), addendum: 'add'},
},
},
},
);

const addendum = screen.getByText('add');
typeInput(addendum, 'addendum', {rerender});

// @ts-ignore
const {uid} = fnBackup.mock.calls[0][0];
expect(uid).toBeUndefined();
});
});
9 changes: 5 additions & 4 deletions src/components/elements/posts/quest/form/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ export const QuestNewForm = () => {
return (
<QuestPostForm
formState={formState}
setFormState={(newState) => {
dispatch(backupDispatchers.backupQuestGuide(newState.payload));
setFormState(newState);
}}
setFormState={setFormState}
fnSendRequest={ApiRequestSender.questPublish}
onSubmitSuccess={() => dispatch(backupDispatchers.clearQuestGuide())}
onUpdated={(payload) => {
const {uid, ...rest} = payload;
dispatch(backupDispatchers.backupQuestGuide(rest));
}}
/>
);
};
26 changes: 5 additions & 21 deletions src/components/elements/posts/quest/form/position.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,27 @@
import React from 'react';

import {PositionalInfo, QuestPostPublishPayload} from '../../../../../api-def/api';
import {QuestPostPublishPayload} from '../../../../../api-def/api';
import {useI18n} from '../../../../../i18n/hook';
import {generateNewPositionInfo} from '../../../../../utils/services/api/utils';
import {ArrayDataForm} from '../../shared/form/array';
import {PostFormDataProps, PostFormState} from '../../shared/form/types';
import {PostFormDataProps} from '../../shared/form/types';
import {QuestPositionUnit} from './positionUnit';

type FormPositionalProps<P extends QuestPostPublishPayload> = Pick<PostFormDataProps<P>, 'formState'> & {
setState: (newState: PostFormState<P>) => void,
}

export const FormPositional = <P extends QuestPostPublishPayload>({
formState,
setState,
}: FormPositionalProps<P>) => {
export const FormPositional = <P extends QuestPostPublishPayload>({formState, setPayload}: PostFormDataProps<P>) => {
const {t} = useI18n();

const {payload} = formState;

const setSkills = (positional: Array<PositionalInfo>) => {
setState({
...formState,
payload: {
...payload,
positional,
},
});
};

return (
<>
<h5>{t((t) => t.posts.quest.positional)}</h5>
<ArrayDataForm
payload={payload}
minLength={1}
getArray={(payload) => payload.positional}
setArray={setSkills}
updateElementValue={(element, key, value) => element[key] = value}
setArray={(positional) => setPayload('positional', positional)}
getUpdatedElement={(element, key, value) => ({...element, [key]: value})}
generateNewElement={generateNewPositionInfo}
renderEntries={(posInfo, onChange) => (
<div className="mt-2">
Expand Down
10 changes: 5 additions & 5 deletions src/components/elements/posts/shared/form/array.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ describe('Array data form', () => {
let payload: Payload;
let getArrayFunc: jest.Mock<Array<Enum>, [Payload]>;
let setArrayFunc: jest.Mock<void, [Array<Enum>]>;
let updateElemValueFunc: jest.Mock<void, [Enum, 'code', string]>;
let getUpdatedElemFunc: jest.Mock<Enum, [Enum, 'code', string]>;
let generateNewElemFunc: jest.Mock<Enum, []>;
let renderEntriesFunc: jest.Mock;

beforeEach(() => {
getArrayFunc = jest.fn().mockImplementation((payload) => payload.enums);
setArrayFunc = jest.fn().mockImplementation((array) => payload.enums = array);
updateElemValueFunc = jest.fn().mockImplementation((elem, key, value) => elem[key] = value);
getUpdatedElemFunc = jest.fn().mockImplementation((element, key, value) => ({...element, [key]: value}));
generateNewElemFunc = jest.fn().mockImplementation(() => ({code: 7}));
renderEntriesFunc = jest.fn();
});
Expand All @@ -51,7 +51,7 @@ describe('Array data form', () => {
minLength={2}
getArray={getArrayFunc}
setArray={setArrayFunc}
updateElementValue={updateElemValueFunc}
getUpdatedElement={getUpdatedElemFunc}
generateNewElement={generateNewElemFunc}
renderEntries={renderEntriesFunc}
/>
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('Array data form', () => {
minLength={2}
getArray={getArrayFunc}
setArray={setArrayFunc}
updateElementValue={updateElemValueFunc}
getUpdatedElement={getUpdatedElemFunc}
generateNewElement={generateNewElemFunc}
renderEntries={renderEntriesFunc}
/>
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('Array data form', () => {
minLength={2}
getArray={getArrayFunc}
setArray={setArrayFunc}
updateElementValue={updateElemValueFunc}
getUpdatedElement={getUpdatedElemFunc}
generateNewElement={generateNewElemFunc}
renderEntries={renderEntriesFunc}
/>
Expand Down
10 changes: 4 additions & 6 deletions src/components/elements/posts/shared/form/array.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type CharaAnalysisFormProps<P extends PostMeta, E> = {
minLength: number,
getArray: (payload: P) => Array<E>,
setArray: (newArray: Array<E>) => void,
updateElementValue: (element: E, key: keyof E, value: string) => void,
getUpdatedElement: (element: E, key: keyof E, value: string) => E,
generateNewElement: () => E,
renderEntries: (
element: E,
Expand All @@ -17,12 +17,12 @@ type CharaAnalysisFormProps<P extends PostMeta, E> = {
}


export const ArrayDataForm = <P extends PostMeta, E>({
export const ArrayDataForm = <P extends PostMeta, E extends object>({
payload,
minLength,
getArray,
setArray,
updateElementValue,
getUpdatedElement,
generateNewElement,
renderEntries,
}: CharaAnalysisFormProps<P, E>) => {
Expand All @@ -41,9 +41,7 @@ export const ArrayDataForm = <P extends PostMeta, E>({
return elem;
}

// Update the data
updateElementValue(elem, key, newValue);
return elem;
return getUpdatedElement(elem, key, newValue);
}));
};
const onAdded = () => {
Expand Down
12 changes: 8 additions & 4 deletions src/components/elements/posts/shared/form/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const PostFormBase = <P extends PostMeta, R extends PostEditResponse>({
fnGetRedirectId,
fnProcessPayload,
onSubmitSuccess,
onUpdated,
}: PostFormBaseInternalProps<P, R>) => {
const {t} = useI18n();
const dispatch = useDispatch();
Expand All @@ -49,10 +50,13 @@ export const PostFormBase = <P extends PostMeta, R extends PostEditResponse>({
};
});

const setPayload = <K extends keyof P>(key: K, newValue: P[K]) => setFormState({
...formState,
payload: {...formState.payload, [key]: newValue},
});
const setPayload = <K extends keyof P>(key: K, newValue: P[K]) => {
const payload: P = {...formState.payload, [key]: newValue};
if (onUpdated) {
onUpdated(payload);
}
setFormState({...formState, payload});
};

const setAvailability = (availability: boolean) => setFormState({
...formState,
Expand Down
6 changes: 4 additions & 2 deletions src/components/elements/posts/shared/form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type PostFormFetchProps<P extends PostMeta, R extends PostEditResponse> =
*/
export type PostFormBaseProps<P extends PostMeta, R extends PostEditResponse> = PostFormFetchProps<P, R> & {
formState: PostFormState<P>,
setFormState: (newState: PostFormState<P>) => void,
setFormState: React.Dispatch<React.SetStateAction<PostFormState<P>>>,
renderMain: (
setPayload: PostFormSetPayloadFunc<P>,
setAvailability: (availability: boolean) => void,
Expand All @@ -51,13 +51,15 @@ export type PostFormBaseProps<P extends PostMeta, R extends PostEditResponse> =
setPayload: PostFormSetPayloadFunc<P>,
) => React.ReactElement,
onSubmitSuccess?: () => void,
onUpdated?: (payload: P) => void,
}

/**
* Props of the main post form.
*/
export type PostFormProps<P extends PostMeta, R extends PostEditResponse> = Pick<PostFormBaseProps<P, R>,
'formState' | 'setFormState' | 'fnSendRequest' | 'fnProcessPayload' | 'renderOnPreloaded' | 'onSubmitSuccess'>;
'formState' | 'setFormState' | 'fnSendRequest' | 'fnProcessPayload' | 'renderOnPreloaded' | 'onSubmitSuccess' |
'onUpdated'>;

export const isFormStateValid = <P extends PostMeta>({isIdAvailable, isPreloaded}: PostFormState<P>) => {
return isIdAvailable || isPreloaded;
Expand Down
Loading

0 comments on commit fb4ecb8

Please sign in to comment.