notifications-settings.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { Text } from 'app/design/typography'
  2. import { View, Row } from 'app/design/view'
  3. import { useState } from 'react';
  4. import Switch from 'app/ui/atoms/switcher'
  5. import { fetcher } from 'app/lib/fetcher';
  6. import { NeoButton } from 'app/design/controls'
  7. import { firstLetterCap } from 'app/lib/util';
  8. import { useTranslation } from 'react-i18next';
  9. import { BlockWrapper } from 'app/components/block-wrapper'
  10. export default function (props, blockWrapperProps) {
  11. const [activeIndex, setActiveIndex] = useState(0);
  12. let aData = [];
  13. const { t } = useTranslation();
  14. props.data.data.forEach((item, index) => {
  15. let aItems = [];
  16. item.items.forEach((item2, index) => {
  17. if (typeof item2 === 'string') {
  18. aItems.push({ type: 'header', title: item2 });
  19. }
  20. else {
  21. aItems.push({ type: 'item', title: item2.title.value, value: item2.switcher.data, id: item2.checkbox.data });
  22. }
  23. });
  24. aData.push({ title: item.delivery, items: aItems })
  25. })
  26. const [data, setData] = useState(aData);
  27. const toggleSwitch = (id, value) => {
  28. fetcher(props.request_url + JSON.stringify({ id, value }));
  29. setData(prevData => prevData.map((item, index) => index === activeIndex ? {
  30. ...item,
  31. items: item.items.map(item => item.id === id ? { ...item, value: item.value === 1 ? 0 : 1 } : item)
  32. } : item));
  33. }
  34. return <BlockWrapper {...blockWrapperProps}>
  35. <Row className='p-2'>
  36. {
  37. data.map((item, index) => {
  38. return <View className='mr-2 ' key={'tab' + index}><NeoButton
  39. style={index == activeIndex ? undefined : 'borderless'}
  40. selected={index == activeIndex}
  41. label={t(firstLetterCap(item.title))}
  42. borderShape="capsule"
  43. controlSize="small"
  44. onPress={() => setActiveIndex(index)}
  45. /></View>
  46. })
  47. }
  48. </Row>
  49. {
  50. data[activeIndex].items.map((item, index) => {
  51. if (item.type == 'header') {
  52. return (<Row className='p-2' key={"row" + index}>
  53. <Text className="text-popover-foreground text-sm font-semibold">{item.title}</Text>
  54. </Row>)
  55. }
  56. else {
  57. return (
  58. <Row className='px-2 py-1' key={"row" + index}>
  59. <Switch
  60. onValueChange={() => toggleSwitch(item.id, item.value == 1 ? 0 : 1)}
  61. value={item.value == 1 ? true : false}
  62. />
  63. <Text className="ml-2 text-popover-foreground text-sm">{item.title}</Text>
  64. </Row>
  65. )
  66. }
  67. })
  68. }
  69. </BlockWrapper>
  70. }