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

Feature/184 add tag field to document #211

Merged
merged 13 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/components/document-metadata-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { withSettings } from './settings-context';
import { withLanguage } from './language-context';
import LanguageSelect from './localization/language-select';
import { EyeOutlined, EditOutlined } from '@ant-design/icons';
import validators from '../utils/input-validators';
import LanguageNameProvider from '../data/language-name-provider';
import CountryFlagAndName from './localization/country-flag-and-name';
import { documentRevisionShape, translationProps, languageProps, settingsProps } from '../ui/default-prop-types';
Expand All @@ -19,6 +20,8 @@ const RadioGroup = Radio.Group;
const MODE_EDIT = 'edit';
const MODE_PREVIEW = 'preview';

const { isValidTag } = validators;

class DocumentMetadataEditor extends React.Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -55,17 +58,16 @@ class DocumentMetadataEditor extends React.Component {

handleTagsChange(selectedValue) {
const { onChanged, documentRevision } = this.props;
const invalidMetadata = selectedValue.length === 0 || selectedValue.some(tag => tag.length < 3 || tag.length > 30);
this.setState({ tagsValidationStatus: invalidMetadata ? 'error' : '' });
onChanged({ metadata: { ...documentRevision, tags: selectedValue }, invalidMetadata });
const areTagsValid = selectedValue.every(tag => isValidTag({ tag }));
this.setState({ tagsValidationStatus: areTagsValid ? '' : 'error' });
onChanged({ metadata: { ...documentRevision, tags: selectedValue }, invalidMetadata: !areTagsValid });
}

render() {
const { mode, tagsValidationStatus } = this.state;
const { documentRevision, languageNameProvider, language, t, settings } = this.props;

const mergedTags = new Set(settings.defaultTags);
documentRevision.tags.forEach(tag => mergedTags.add(tag));
const mergedTags = new Set([...settings.defaultTags, ...documentRevision.tags]);

let docLanguage;
let componentToShow;
Expand Down
4 changes: 2 additions & 2 deletions src/components/settings/default-tags-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DeleteOutlined, DownOutlined, PlusOutlined, UpOutlined } from '@ant-des

const FormItem = Form.Item;

const getRequiredValidateStatus = (allTags, tag) => inputValidators.isValidTag(allTags, tag) ? 'success' : 'error';
const getRequiredValidateStatus = (allTags, tag) => inputValidators.isValidTag({ allTags, tag }) ? 'success' : 'error';

const mapTableRowsToTags = rows => rows.map(row => row.tag);

Expand All @@ -19,7 +19,7 @@ function DefaultTagsSettings({ defaultTags, onChange }) {

const fireOnChange = rows => {
const tags = mapTableRowsToTags(rows);
onChange(tags, { isValid: tags.every(tag => inputValidators.isValidTag(tags, tag)) });
onChange(tags, { isValid: tags.every(tag => inputValidators.isValidTag({ tag, tags })) });
adrianaluchian marked this conversation as resolved.
Show resolved Hide resolved
};

const handleMoveClick = (index, offset) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/input-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function isValidPassword({ password, minLength = 8 }) {
return sanitizedPassword.length >= minLength && minOneLetterAndOneDigitPattern.test(sanitizedPassword);
}

function isValidTag(allTags, tag) {
function isValidTag({ tag, allTags = [] }) {
const trimmedTag = (tag || '').trim();

if (trimmedTag.length < 3 || trimmedTag.length > 30 || (/\s/).test(trimmedTag)) {
Expand Down
9 changes: 4 additions & 5 deletions src/utils/input-validators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ describe.only('input-validators', () => {

describe('isValidTag', () => {
let result;
const allOtherTags = ['tag1', 'tag2', 'tag3'];

const testCases = [
{ tag: null, expectedResult: false },
Expand All @@ -42,17 +41,17 @@ describe.only('input-validators', () => {
{ tag: ' tag ', expectedResult: true },
{ tag: 't a g', expectedResult: false },
{ tag: 't\tag', expectedResult: false },
{ tag: 'tag2', expectedResult: false },
{ tag: ' ', expectedResult: false },
{ tag: 'tag2', allTags: ['tag1', 'tag2'], expectedResult: true },
{ tag: 'tag2', allTags: ['tag1', 'tag2', 'tag2'], expectedResult: false },
{ tag: 'aPrettyLongTagToConsiderValid?', expectedResult: true },
{ tag: 'anEvenLongerTagToConsiderValid?', expectedResult: false }
];

testCases.forEach(({ tag, expectedResult }) => {
testCases.forEach(({ tag, allTags, expectedResult }) => {
describe(`when validating tag='${tag}'`, () => {
beforeEach(() => {
const allTags = [...allOtherTags, tag];
result = sut.isValidTag(allTags, tag);
result = sut.isValidTag({ tag, allTags });
});
it(`should return ${expectedResult}`, () => {
expect(result).toBe(expectedResult);
Expand Down