import React, {useState, useEffect} from 'react';
import {
View,
Text,
TouchableOpacity,
Dimensions,
TextInput,
} from 'react-native';
import {
SIZE,
pv,
WEIGHT,
opacity,
COLOR_SCHEME_DARK,
COLOR_SCHEME_LIGHT,
COLOR_SCHEME,
ACCENT,
} from '../../common/common';
import Icon from 'react-native-vector-icons/Feather';
import NavigationService from '../../services/NavigationService';
import {db} from '../../../App';
import FastStorage from 'react-native-fast-storage';
import {_recieveEvent, _unSubscribeEvent} from '../DialogManager';
const w = Dimensions.get('window').width;
const h = Dimensions.get('window').height;
let tagsInputRef;
export const ActionSheetComponent = ({
close = () => {},
item,
setWillRefresh = value => {},
hasColors = false,
hasTags = false,
rowItems = [],
columnItems = [],
}) => {
const {colors, updateDB, changeColorScheme} = useAppContext();
// Todo
<<<<<<< HEAD
function changeColorScheme(colors = COLOR_SCHEME, accent = ACCENT) {
let newColors = setColorScheme(colors, accent);
StatusBar.setBarStyle(newColors.night ? 'light-content' : 'dark-content');
dispatch({type: ACTIONS.THEME, colors: newColors});
}
=======
>>>>>>> cfee96fb230bdd33c710a5e72a80ccf2b82a8646
const [focused, setFocused] = useState(false);
const [note, setNote] = useState(
item
? item
: {
colors: [],
tags: [],
pinned: false,
favorite: false,
locked: false,
content: {
text: '',
delta: {},
},
dateCreated: null,
},
);
useEffect(() => {
if (item.dateCreated !== null) {
setNote({...item});
}
}, [item]);
let tagToAdd = null;
let backPressCount = 0;
const _onSubmit = () => {
if (!tagToAdd || tagToAdd === '#') return;
let tag = tagToAdd;
if (tag[0] !== '#') {
tag = '#' + tag;
}
if (tag.includes(' ')) {
tag = tag.replace(' ', '_');
}
let oldProps = {...note};
if (oldProps.tags.includes(tag)) {
return;
} else {
oldProps.tags.push(tag);
}
tagsInputRef.setNativeProps({
text: '#',
});
db.addNote({
dateCreated: note.dateCreated,
content: note.content,
title: note.title,
tags: oldProps.tags,
});
setNote({...db.getNote(note.dateCreated)});
tagToAdd = '';
setTimeout(() => {
//tagsInputRef.focus();
}, 300);
};
const _onKeyPress = event => {
if (event.nativeEvent.key === 'Backspace') {
if (backPressCount === 0 && !tagToAdd) {
backPressCount = 1;
return;
}
if (backPressCount === 1 && !tagToAdd) {
backPressCount = 0;
let tagInputValue = note.tags[note.tags.length - 1];
let oldProps = {...note};
if (oldProps.tags.length === 1) return;
oldProps.tags.splice(oldProps.tags.length - 1);
db.addNote({
dateCreated: note.dateCreated,
content: note.content,
title: note.title,
tags: oldProps.tags,
});
setNote({...db.getNote(note.dateCreated)});
tagsInputRef.setNativeProps({
text: tagInputValue,
});
setTimeout(() => {
tagsInputRef.focus();
}, 300);
}
}
};
const localRefresh = type => {
if (!note || !note.dateCreated) return;
let toAdd;
switch (type) {
case 'note': {
toAdd = db.getNote(note.dateCreated);
break;
}
case 'notebook': {
toAdd = db.getNotebook(note.dateCreated);
break;
}
case 'topic': {
toAdd = db.getTopic(topic.notebookId, topic.title);
break;
}
}
updateDB();
setNote({...toAdd});
};
const rowItemsData = [
{
name: 'Add to',
icon: 'book',
func: () => {
close();
NavigationService.push('Folders', {
note: note,
title: 'Choose a notebook',
isMove: true,
hideMore: true,
canGoBack: true,
});
},
},
{
name: 'Share',
icon: 'share-2',
func: () => {
close();
},
},
{
name: 'Export',
icon: 'external-link',
func: () => {
close();
},
},
{
name: 'Delete',
icon: 'trash',
func: () => close('delete'),
},
{
name: 'Edit Notebook',
icon: 'trash',
func: () => {
close('topic');
},
},
{
name: 'Edit Topic',
icon: 'trash',
func: () => {
close('topic');
},
},
{
name: 'Restore',
icon: 'trash',
func: () => {
db.restoreItem(item.dateCreated);
ToastEvent.show(
item.type == 'note' ? 'Note restored' : 'Notebook restored',
'success',
1000,
() => {},
'',
);
close();
},
},
{
name: 'Remove',
icon: 'trash',
func: () => {
close();
},
},
];
columnItemsData = [
{
name: 'Dark Mode',
icon: 'moon',
func: () => {
if (!colors.night) {
FastStorage.setItem('theme', JSON.stringify({night: true}));
changeColorScheme(COLOR_SCHEME_DARK);
} else {
FastStorage.setItem('theme', JSON.stringify({night: false}));
changeColorScheme(COLOR_SCHEME_LIGHT);
}
},
switch: true,
on: colors.night ? true : false,
close: false,
},
{
name: 'Pin',
icon: 'tag',
func: () => {
if (!note.dateCreated) return;
db.pinItem(note.type, note.dateCreated);
localRefresh(item.type);
},
close: false,
check: true,
on: note.pinned,
},
{
name: 'Favorite',
icon: 'star',
func: () => {
if (!note.dateCreated) return;
db.favoriteItem(note.type, note.dateCreated);
localRefresh(item.type);
},
close: false,
check: true,
on: note.favorite,
},
{
name: 'Add to Vault',
icon: 'lock',
func: () => {
if (!note.dateCreated) return;
note.locked ? close('unlock') : close('lock');
},
close: true,
check: true,
on: note.locked,
},
];
const _renderTag = tag => (
{
let oldProps = {...note};
oldProps.tags.splice(oldProps.tags.indexOf(tag), 1);
db.addNote({
dateCreated: note.dateCreated,
content: note.content,
title: note.title,
tags: oldProps.tags,
});
localRefresh(item.type);
}}
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
margin: 1,
paddingHorizontal: 5,
paddingVertical: 2.5,
}}>
{tag.slice(0, 1)}
{tag.slice(1)}
);
const _renderColor = color => (
{
let noteColors = note.colors;
if (noteColors.includes(color)) {
noteColors.splice(color, 1);
} else {
noteColors.push(color);
}
db.addNote({
dateCreated: note.dateCreated,
colors: noteColors,
content: note.content,
title: note.title,
});
localRefresh(item.type);
}}
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
borderColor: colors.nav,
}}>
{note && note.colors && note.colors.includes(color) ? (
) : null}
);
const _renderRowItem = rowItem =>
rowItems.includes(rowItem.name) ? (
{rowItem.name}
) : null;
const _renderColumnItem = item =>
columnItems.includes(item.name) ? (
{
item.func();
}}
style={{
width: '100%',
alignSelf: 'center',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'flex-end',
paddingHorizontal: 12,
paddingVertical: pv + 5,
}}>
{item.name}
{item.switch ? (
) : (
undefined
)}
{item.check ? (
{item.on ? (
) : null}
) : null}
) : null;
return (
{rowItemsData.map(_renderRowItem)}
{hasColors ? (
{['red', 'yellow', 'green', 'blue', 'purple', 'orange', 'gray'].map(
_renderColor,
)}
) : null}
{hasTags ? (
{note && note.tags ? note.tags.map(_renderTag) : null}
(tagsInputRef = ref)}
placeholderTextColor={colors.icon}
onFocus={() => {
setFocused(true);
}}
selectionColor={colors.accent}
onBlur={() => {
setFocused(false);
}}
placeholder="#hashtag"
onChangeText={value => {
tagToAdd = value;
if (tagToAdd.length > 0) backPressCount = 0;
}}
onSubmitEditing={_onSubmit}
onKeyPress={_onKeyPress}
/>
) : null}
{columnItems.length > 0 ? (
{columnItemsData.map(_renderColumnItem)}
) : null}
);
};