Skip to content

Commit

Permalink
fix(frontend,backend): fix parsing of hashtags into array, allow comm…
Browse files Browse the repository at this point in the history
…a separated
  • Loading branch information
spwoodcock committed Jun 27, 2024
1 parent af254a0 commit 5716b1a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
21 changes: 17 additions & 4 deletions src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ProjectIn(BaseModel):
xform_category: str
custom_tms_url: Optional[str] = None
organisation_id: Optional[int] = None
hashtags: Optional[List[str]] = None
hashtags: Optional[str] = None
task_split_type: Optional[TaskSplitType] = None
task_split_dimension: Optional[int] = None
task_num_buildings: Optional[int] = None
Expand Down Expand Up @@ -177,11 +177,24 @@ def project_name_prefix(self) -> str:

@field_validator("hashtags", mode="after")
@classmethod
def prepend_hash_to_tags(cls, hashtags: List[str]) -> Optional[List[str]]:
"""Add '#' to hashtag if missing. Also added default '#FMTM'."""
def validate_hashtags(cls, hashtags: Optional[str]) -> List[str]:
"""Validate hashtags.
- Receives a string and parsed as a list of tags.
- Commas or semicolons are replaced with spaces before splitting.
- Add '#' to hashtag if missing.
- Also add default '#FMTM' tag.
"""
if hashtags is None:
return ["#FMTM"]

hashtags = hashtags.replace(",", " ").replace(";", " ")
hashtags_list = hashtags.split()

# Add '#' to hashtag strings if missing
hashtags_with_hash = [
f"#{hashtag}" if hashtag and not hashtag.startswith("#") else hashtag
for hashtag in hashtags
for hashtag in hashtags_list
]

if "#FMTM" not in hashtags_with_hash:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ const ProjectDetailsForm = ({ flag }) => {
};
}, []);

// Checks if hashtag value starts with hotosm-fmtm'
const handleHashtagOnChange = (e) => {
let enteredText = e.target.value;
handleCustomChange('hashtags', enteredText);
};

const handleInputChanges = (e) => {
handleChange(e);
dispatch(CreateProjectActions.SetIsUnsavedChanges(true));
Expand Down Expand Up @@ -240,7 +234,7 @@ const ProjectDetailsForm = ({ flag }) => {
label="Hashtags"
value={values?.hashtags}
onChange={(e) => {
handleHashtagOnChange(e);
handleCustomChange('hashtags', e.target.value);
}}
fieldType="text"
errorMsg={errors.hashtag}
Expand Down
8 changes: 1 addition & 7 deletions src/frontend/src/components/createnewproject/SplitTasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ const SplitTasks = ({ flag, geojsonFile, setGeojsonFile, customDataExtractUpload
dispatch(CreateProjectActions.SetIsUnsavedChanges(false));

dispatch(CreateProjectActions.SetIndividualProjectDetailsData(formValues));
const hashtags = projectDetails.hashtags;
const arrayHashtag = hashtags
?.split('#')
.map((item) => item.trim())
.filter(Boolean);

// Project POST data
let projectData = {
project_info: {
Expand All @@ -116,7 +110,7 @@ const SplitTasks = ({ flag, geojsonFile, setGeojsonFile, customDataExtractUpload
task_split_type: splitTasksSelection,
form_ways: projectDetails.formWays,
// "uploaded_form": projectDetails.uploaded_form,
hashtags: arrayHashtag,
hashtags: projectDetails.hashtags,
data_extract_url: projectDetails.data_extract_url,
custom_tms_url: projectDetails.custom_tms_url,
};
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/store/types/ICreateProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type EditProjectResponseTypes = {
outline_geojson: GeoJSONFeatureTypes;
tasks: ProjectTaskTypes[];
xform_category: string;
hashtags: string[];
hashtags: string;
};
export type EditProjectDetailsTypes = {
name: string;
Expand Down

0 comments on commit 5716b1a

Please sign in to comment.