-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import React, { useEffect } from "react"; | ||
import { Checkbox, FormControlLabel } from "@mui/material"; | ||
import { ApiHelper, GroupInterface } from "@churchapps/apphelper"; | ||
|
||
|
||
interface Props { | ||
group: GroupInterface; | ||
onUpdate: (array: string[]) => void; | ||
} | ||
|
||
export const GroupLabelsEdit: React.FC<Props> = (props) => { | ||
const a = 1; | ||
|
||
const [allLabels, setAllLabels] = React.useState<string[]>(["Small Group", "Sunday School Class"]); | ||
const groupLabels = props.group?.labelArray; | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const val = e.currentTarget.name; | ||
const idx = groupLabels.indexOf(val); | ||
if (idx === -1) groupLabels.push(val); | ||
else groupLabels.splice(idx, 1); | ||
props.onUpdate(groupLabels); | ||
}; | ||
|
||
const handleAdd = (e:React.MouseEvent) => { | ||
e.preventDefault(); | ||
const newLabel = prompt("Enter a new label"); | ||
if (newLabel) { | ||
const val = newLabel.trim(); | ||
if (val.length > 0) { | ||
const idx = allLabels.indexOf(val); | ||
if (idx === -1) { | ||
allLabels.push(val); | ||
setAllLabels(allLabels.sort()); | ||
groupLabels.push(val); | ||
props.onUpdate(groupLabels); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const loadData = () => { | ||
ApiHelper.get("/groups", "MembershipApi").then((groups) => { | ||
const result:string[] = []; | ||
groups.forEach((group: GroupInterface) => { | ||
group.labelArray.forEach((label) => { | ||
if (!result.includes(label)) result.push(label); | ||
}); | ||
}) | ||
setAllLabels(result.sort()); | ||
}); | ||
|
||
}; | ||
|
||
useEffect(loadData, []); | ||
|
||
const getLabelCheck = (key:string) => { | ||
const isChecked = groupLabels.includes(key); | ||
return <FormControlLabel | ||
key={key} | ||
control={<Checkbox name={key} checked={isChecked} onChange={handleChange} />} | ||
label={key} | ||
/> | ||
} | ||
|
||
const getItems = () => allLabels.map((key) => getLabelCheck(key)) | ||
|
||
return <> | ||
<div style={{marginTop:10}}>Labels</div> | ||
{getItems()} | ||
<a href="about:blank" onClick={handleAdd}>Add New Label</a> | ||
|
||
</> | ||
}; |