2021-06-14 08:38:03 +05:00
|
|
|
import React, {useEffect, useRef, useState} from 'react';
|
2021-06-15 13:50:48 +05:00
|
|
|
import {
|
|
|
|
|
ActivityIndicator,
|
|
|
|
|
TouchableOpacity,
|
|
|
|
|
View,
|
|
|
|
|
} from 'react-native';
|
2021-06-14 14:41:57 +05:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
|
2021-09-15 12:24:10 +05:00
|
|
|
import Clipboard from "@react-native-clipboard/clipboard"
|
2021-06-14 14:41:57 +05:00
|
|
|
import {useTracked} from '../../provider';
|
2021-06-14 08:38:03 +05:00
|
|
|
import {
|
|
|
|
|
eSubscribeEvent,
|
|
|
|
|
eUnSubscribeEvent,
|
|
|
|
|
ToastEvent,
|
|
|
|
|
} from '../../services/EventManager';
|
2021-06-14 14:41:57 +05:00
|
|
|
import Navigation from '../../services/Navigation';
|
|
|
|
|
import {db} from '../../utils/DB';
|
2021-06-14 08:38:03 +05:00
|
|
|
import {
|
|
|
|
|
eClosePublishNoteDialog,
|
|
|
|
|
eOpenPublishNoteDialog,
|
|
|
|
|
} from '../../utils/Events';
|
2021-06-14 14:41:57 +05:00
|
|
|
import {openLinkInBrowser} from '../../utils/functions';
|
2021-06-14 08:38:03 +05:00
|
|
|
import {SIZE} from '../../utils/SizeUtils';
|
2021-06-14 14:41:57 +05:00
|
|
|
import {ActionIcon} from '../ActionIcon';
|
2021-06-14 08:38:03 +05:00
|
|
|
import ActionSheetWrapper from '../ActionSheetComponent/ActionSheetWrapper';
|
|
|
|
|
import {Button} from '../Button';
|
|
|
|
|
import DialogHeader from '../Dialog/dialog-header';
|
|
|
|
|
import Input from '../Input';
|
|
|
|
|
import Seperator from '../Seperator';
|
|
|
|
|
import Heading from '../Typography/Heading';
|
|
|
|
|
import Paragraph from '../Typography/Paragraph';
|
|
|
|
|
|
|
|
|
|
let passwordValue = null;
|
|
|
|
|
const PublishNoteDialog = () => {
|
|
|
|
|
const [state] = useTracked();
|
|
|
|
|
const colors = state.colors;
|
|
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
|
const actionSheetRef = useRef();
|
2021-06-14 14:41:57 +05:00
|
|
|
const [selfDestruct, setSelfDestruct] = useState(false);
|
2021-06-14 08:38:03 +05:00
|
|
|
const [isLocked, setIsLocked] = useState(false);
|
|
|
|
|
const [note, setNote] = useState(null);
|
2021-06-15 13:50:48 +05:00
|
|
|
const [publishing, setPublishing] = useState(false);
|
|
|
|
|
const publishUrl =
|
|
|
|
|
note &&
|
|
|
|
|
`https://monograph.notesnook.com/${db?.monographs.monograph(note?.id)}`;
|
|
|
|
|
const isPublished = note && db?.monographs.isPublished(note?.id);
|
2021-08-17 11:49:02 +05:00
|
|
|
const pwdInput = useRef();
|
2021-06-14 08:38:03 +05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
eSubscribeEvent(eOpenPublishNoteDialog, open);
|
|
|
|
|
eSubscribeEvent(eClosePublishNoteDialog, close);
|
|
|
|
|
return () => {
|
|
|
|
|
eUnSubscribeEvent(eOpenPublishNoteDialog, open);
|
|
|
|
|
eUnSubscribeEvent(eClosePublishNoteDialog, close);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2021-06-14 14:41:57 +05:00
|
|
|
const open = item => {
|
2021-06-14 08:38:03 +05:00
|
|
|
setNote(item);
|
2021-06-15 13:50:48 +05:00
|
|
|
setPublishing(false);
|
|
|
|
|
setSelfDestruct(false);
|
|
|
|
|
setIsLocked(false);
|
2021-06-14 08:38:03 +05:00
|
|
|
setVisible(true);
|
2021-06-15 13:50:48 +05:00
|
|
|
passwordValue = null;
|
2021-06-14 08:38:03 +05:00
|
|
|
};
|
2021-06-15 13:50:48 +05:00
|
|
|
|
2021-06-14 08:38:03 +05:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (visible) {
|
|
|
|
|
actionSheetRef.current?.show();
|
|
|
|
|
}
|
|
|
|
|
}, [visible]);
|
|
|
|
|
|
|
|
|
|
const close = () => {
|
2021-06-14 14:41:57 +05:00
|
|
|
passwordValue = null;
|
2021-06-14 08:38:03 +05:00
|
|
|
actionSheetRef.current?.hide();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const publishNote = async () => {
|
2021-06-15 13:50:48 +05:00
|
|
|
if (publishing) return;
|
|
|
|
|
setPublishing(true);
|
|
|
|
|
try {
|
|
|
|
|
if (note?.id) {
|
|
|
|
|
if (isLocked && !passwordValue) return;
|
|
|
|
|
await db.monographs.publish(note.id, {
|
|
|
|
|
selfDestruct: selfDestruct,
|
|
|
|
|
password: isLocked && passwordValue,
|
|
|
|
|
});
|
|
|
|
|
setNote(db.notes.note(note.id)?.data);
|
|
|
|
|
Navigation.setRoutesToUpdate([
|
|
|
|
|
Navigation.routeNames.Notes,
|
|
|
|
|
Navigation.routeNames.NotesPage,
|
|
|
|
|
Navigation.routeNames.Favorites,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Could not publish note',
|
|
|
|
|
message: e.message,
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
2021-06-14 08:38:03 +05:00
|
|
|
});
|
|
|
|
|
}
|
2021-06-15 13:50:48 +05:00
|
|
|
setPublishing(false);
|
2021-06-14 08:38:03 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const deletePublishedNote = async () => {
|
2021-06-15 13:50:48 +05:00
|
|
|
if (publishing) return;
|
|
|
|
|
setPublishing(true);
|
|
|
|
|
try {
|
|
|
|
|
if (note?.id) {
|
|
|
|
|
await db.monographs.unpublish(note.id);
|
|
|
|
|
setNote(db.notes.note(note.id)?.data);
|
|
|
|
|
Navigation.setRoutesToUpdate([
|
|
|
|
|
Navigation.routeNames.Notes,
|
|
|
|
|
Navigation.routeNames.NotesPage,
|
|
|
|
|
Navigation.routeNames.Favorites,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Could not unpublish note',
|
|
|
|
|
message: e.message,
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2021-06-14 08:38:03 +05:00
|
|
|
}
|
2021-06-15 13:50:48 +05:00
|
|
|
actionSheetRef.current?.hide();
|
|
|
|
|
setPublishing(false);
|
2021-06-14 08:38:03 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return !visible ? null : (
|
|
|
|
|
<ActionSheetWrapper
|
|
|
|
|
centered={false}
|
|
|
|
|
fwdRef={actionSheetRef}
|
2021-06-15 13:50:48 +05:00
|
|
|
closeOnTouchBackdrop={!publishing}
|
2021-06-14 08:38:03 +05:00
|
|
|
onClose={async () => {
|
|
|
|
|
passwordValue = null;
|
|
|
|
|
setVisible(false);
|
|
|
|
|
}}>
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
alignSelf: 'center',
|
|
|
|
|
paddingHorizontal: 12,
|
|
|
|
|
}}>
|
|
|
|
|
<DialogHeader
|
|
|
|
|
title="Publish note"
|
|
|
|
|
paragraph={`Anyone with the link${
|
|
|
|
|
isLocked ? ' and password' : ''
|
|
|
|
|
} of a published note can view it.`}
|
|
|
|
|
/>
|
|
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
{publishing ? (
|
2021-06-14 08:38:03 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
2021-06-15 13:50:48 +05:00
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignContent: 'center',
|
|
|
|
|
height: 150,
|
|
|
|
|
width: '100%',
|
2021-06-14 08:38:03 +05:00
|
|
|
}}>
|
2021-06-15 13:50:48 +05:00
|
|
|
<ActivityIndicator size={25} color={colors.accent} />
|
|
|
|
|
<Paragraph
|
2021-06-14 08:38:03 +05:00
|
|
|
style={{
|
2021-06-15 13:50:48 +05:00
|
|
|
textAlign: 'center',
|
2021-06-14 08:38:03 +05:00
|
|
|
}}>
|
2021-06-15 13:50:48 +05:00
|
|
|
Please wait...
|
|
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{isPublished && (
|
|
|
|
|
<View
|
2021-06-14 08:38:03 +05:00
|
|
|
style={{
|
2021-06-15 13:50:48 +05:00
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
marginTop: 15,
|
|
|
|
|
backgroundColor: colors.shade,
|
|
|
|
|
padding: 10,
|
|
|
|
|
borderRadius: 5,
|
|
|
|
|
borderWidth: 1,
|
|
|
|
|
borderColor: colors.accent,
|
2021-06-14 08:38:03 +05:00
|
|
|
}}>
|
2021-06-15 13:50:48 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
flexShrink: 1,
|
|
|
|
|
}}>
|
|
|
|
|
<Heading size={SIZE.md}>This Note is published</Heading>
|
|
|
|
|
<Paragraph numberOfLines={1}>{publishUrl}</Paragraph>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<Paragraph
|
|
|
|
|
onPress={async () => {
|
|
|
|
|
try {
|
|
|
|
|
await openLinkInBrowser(publishUrl, colors.accent);
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
}}
|
|
|
|
|
style={{
|
|
|
|
|
marginTop: 5,
|
|
|
|
|
color: colors.accent,
|
|
|
|
|
}}>
|
|
|
|
|
<Icon name="open-in-new" /> Open in browser
|
|
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<ActionIcon
|
|
|
|
|
onPress={() => {
|
|
|
|
|
Clipboard.setString(publishUrl);
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Note publish url copied',
|
|
|
|
|
type: 'success',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
color={colors.icon}
|
|
|
|
|
size={SIZE.lg}
|
|
|
|
|
name="content-copy"
|
|
|
|
|
/>
|
|
|
|
|
</View>
|
|
|
|
|
)}
|
2021-06-15 14:48:51 +05:00
|
|
|
<Seperator />
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<TouchableOpacity
|
|
|
|
|
onPress={() => {
|
|
|
|
|
if (publishing) return;
|
|
|
|
|
setIsLocked(!isLocked);
|
|
|
|
|
}}
|
|
|
|
|
activeOpacity={0.9}
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
marginBottom: 10,
|
|
|
|
|
}}>
|
|
|
|
|
<ActionIcon
|
|
|
|
|
onPress={() => {
|
|
|
|
|
if (publishing) return;
|
|
|
|
|
setIsLocked(!isLocked);
|
|
|
|
|
}}
|
|
|
|
|
color={isLocked ? colors.accent : colors.icon}
|
|
|
|
|
size={SIZE.lg}
|
|
|
|
|
name={
|
|
|
|
|
isLocked
|
|
|
|
|
? 'check-circle-outline'
|
|
|
|
|
: 'checkbox-blank-circle-outline'
|
|
|
|
|
}
|
|
|
|
|
/>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
flexShrink: 1,
|
|
|
|
|
}}>
|
2021-06-16 11:45:42 +05:00
|
|
|
<Heading size={SIZE.md}>Password protection</Heading>
|
2021-06-15 13:50:48 +05:00
|
|
|
<Paragraph>
|
|
|
|
|
Published note can only be viewed by someone with the
|
|
|
|
|
password.
|
|
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
|
|
|
|
</TouchableOpacity>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<TouchableOpacity
|
|
|
|
|
onPress={() => {
|
|
|
|
|
setSelfDestruct(!selfDestruct);
|
|
|
|
|
}}
|
|
|
|
|
activeOpacity={0.9}
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
}}>
|
|
|
|
|
<ActionIcon
|
|
|
|
|
onPress={() => {
|
|
|
|
|
setSelfDestruct(!selfDestruct);
|
|
|
|
|
}}
|
|
|
|
|
color={selfDestruct ? colors.accent : colors.icon}
|
|
|
|
|
size={SIZE.lg}
|
|
|
|
|
name={
|
|
|
|
|
selfDestruct
|
|
|
|
|
? 'check-circle-outline'
|
|
|
|
|
: 'checkbox-blank-circle-outline'
|
|
|
|
|
}
|
|
|
|
|
/>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
flexShrink: 1,
|
|
|
|
|
}}>
|
2021-06-16 11:45:42 +05:00
|
|
|
<Heading size={SIZE.md}>Self destruct</Heading>
|
2021-06-15 13:50:48 +05:00
|
|
|
<Paragraph>
|
|
|
|
|
Published note link will be automatically deleted once it is
|
|
|
|
|
viewed by someone.
|
|
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
|
|
|
|
</TouchableOpacity>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
alignSelf: 'center',
|
|
|
|
|
marginTop: 10,
|
|
|
|
|
}}>
|
2021-06-15 14:48:51 +05:00
|
|
|
{isLocked ? (
|
|
|
|
|
<>
|
|
|
|
|
<Input
|
2021-08-17 11:49:02 +05:00
|
|
|
fwdRef={pwdInput}
|
2021-06-15 14:48:51 +05:00
|
|
|
onChangeText={value => (passwordValue = value)}
|
|
|
|
|
blurOnSubmit
|
|
|
|
|
secureTextEntry
|
|
|
|
|
defaultValue={passwordValue}
|
|
|
|
|
placeholder="Enter Password"
|
|
|
|
|
/>
|
|
|
|
|
<Seperator half />
|
|
|
|
|
</>
|
|
|
|
|
) : null}
|
|
|
|
|
|
2021-06-14 08:38:03 +05:00
|
|
|
<Button
|
2021-06-15 13:50:48 +05:00
|
|
|
onPress={publishNote}
|
2021-06-14 08:38:03 +05:00
|
|
|
fontSize={SIZE.md}
|
|
|
|
|
width="100%"
|
|
|
|
|
height={50}
|
2021-06-15 13:50:48 +05:00
|
|
|
type="accent"
|
|
|
|
|
title={isPublished ? 'Update published note' : 'Publish note'}
|
2021-06-14 08:38:03 +05:00
|
|
|
/>
|
2021-06-15 13:50:48 +05:00
|
|
|
|
|
|
|
|
{isPublished && (
|
|
|
|
|
<>
|
|
|
|
|
<Seperator half />
|
|
|
|
|
<Button
|
|
|
|
|
onPress={deletePublishedNote}
|
|
|
|
|
fontSize={SIZE.md}
|
|
|
|
|
width="100%"
|
|
|
|
|
height={50}
|
|
|
|
|
type="error"
|
|
|
|
|
title="Unpublish note"
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</View>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<Paragraph
|
|
|
|
|
color={colors.icon}
|
|
|
|
|
size={SIZE.xs + 1}
|
|
|
|
|
style={{
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
marginTop: 5,
|
|
|
|
|
textDecorationLine: 'underline',
|
|
|
|
|
}}
|
|
|
|
|
onPress={async () => {
|
|
|
|
|
try {
|
|
|
|
|
await openLinkInBrowser(
|
2021-06-16 11:45:42 +05:00
|
|
|
'https://docs.notesnook.com/monographs/',
|
2021-06-15 13:50:48 +05:00
|
|
|
colors.accent,
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
}}>
|
|
|
|
|
Learn more about Notesnook Monograph
|
|
|
|
|
</Paragraph>
|
2021-06-14 08:38:03 +05:00
|
|
|
</View>
|
|
|
|
|
</ActionSheetWrapper>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default PublishNoteDialog;
|