big refacto change all flows
This commit is contained in:
@@ -11,21 +11,25 @@ import { goBack, navigate } from "../../navigation/NavigationService";
|
||||
import { Routes } from "../../navigation";
|
||||
import firebase from "../../config/firebase";
|
||||
import useMinuit from "react-native-minuit/src/hooks/useMinuit.js";
|
||||
import { useUser } from "../../providers/UserDataProvider";
|
||||
|
||||
const CreatingLyrics = ({ active, config, regenerateKey, selections }) => {
|
||||
const CreatingLyrics = ({ active, config, selections }) => {
|
||||
const [progress, setProgress] = useState(0);
|
||||
const [called, setCalled] = useState(false);
|
||||
const [result, setResult] = useState(null);
|
||||
const [saved, setSaved] = useState(false);
|
||||
const { setIsLoading } = useMinuit();
|
||||
const { updateProjectData } = useUser();
|
||||
|
||||
// When asked to regenerate, reset flags so effect runs again
|
||||
// Reset when becomes active
|
||||
useEffect(() => {
|
||||
if (active) {
|
||||
setCalled(false);
|
||||
setResult(null);
|
||||
setProgress(0);
|
||||
setSaved(false);
|
||||
}
|
||||
}, [regenerateKey, active]);
|
||||
}, [active]);
|
||||
|
||||
useEffect(() => {
|
||||
if (active) {
|
||||
@@ -74,7 +78,42 @@ const CreatingLyrics = ({ active, config, regenerateKey, selections }) => {
|
||||
}
|
||||
}, [active, called, config, setIsLoading]);
|
||||
|
||||
console.log(result);
|
||||
// Lorsque la génération est terminée, enregistrer et naviguer automatiquement vers Lyrics
|
||||
useEffect(() => {
|
||||
const autoSaveAndGo = async () => {
|
||||
try {
|
||||
if (saved) return;
|
||||
setSaved(true);
|
||||
await setIsLoading(true);
|
||||
await updateProjectData({
|
||||
title: result?.title || "",
|
||||
titleLower: (result?.title || "").toLowerCase(),
|
||||
lyrics: Array.isArray(result?.lyrics) ? result.lyrics : [],
|
||||
config: config || null,
|
||||
selections: selections || null,
|
||||
hasLyrics: false,
|
||||
});
|
||||
navigate(Routes.Lyrics);
|
||||
} catch (e) {
|
||||
console.log("Auto save generated lyrics error", e?.message);
|
||||
} finally {
|
||||
await setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (active && result && progress >= 100 && !saved) {
|
||||
autoSaveAndGo();
|
||||
}
|
||||
}, [
|
||||
active,
|
||||
result,
|
||||
progress,
|
||||
saved,
|
||||
setIsLoading,
|
||||
updateProjectData,
|
||||
config,
|
||||
selections,
|
||||
]);
|
||||
|
||||
return (
|
||||
<View
|
||||
@@ -121,19 +160,6 @@ const CreatingLyrics = ({ active, config, regenerateKey, selections }) => {
|
||||
Platform.OS !== "ios" ? "dimezisBlurView" : "none"
|
||||
}
|
||||
>
|
||||
<Pressable
|
||||
style={{
|
||||
...size({ size: 24 }),
|
||||
...Style.containerCenter,
|
||||
position: "absolute",
|
||||
top: 10,
|
||||
left: 10,
|
||||
zIndex: 3,
|
||||
}}
|
||||
onPress={goBack}
|
||||
>
|
||||
<Image source={icons.close} style={size({ size: 11 })} />
|
||||
</Pressable>
|
||||
<View style={{ flex: 1, justifyContent: "flex-end", gap: 20 }}>
|
||||
<Text
|
||||
style={{
|
||||
@@ -157,21 +183,37 @@ const CreatingLyrics = ({ active, config, regenerateKey, selections }) => {
|
||||
{progress}%
|
||||
</Text>
|
||||
</View>
|
||||
<GradientButton
|
||||
title="Découvrir mon texte"
|
||||
containerStyle={{
|
||||
width: "80%",
|
||||
alignSelf: "center",
|
||||
}}
|
||||
onPress={() =>
|
||||
navigate(Routes.Lyrics, {
|
||||
lyricsData: result,
|
||||
config,
|
||||
selections,
|
||||
hasLyrics: false,
|
||||
})
|
||||
}
|
||||
/>
|
||||
{!saved && (
|
||||
<GradientButton
|
||||
title="Découvrir mon texte"
|
||||
containerStyle={{
|
||||
width: "80%",
|
||||
alignSelf: "center",
|
||||
}}
|
||||
onPress={async () => {
|
||||
try {
|
||||
if (saved) return;
|
||||
setSaved(true);
|
||||
await setIsLoading(true);
|
||||
await updateProjectData({
|
||||
title: result?.title || "",
|
||||
titleLower: (result?.title || "").toLowerCase(),
|
||||
lyrics: Array.isArray(result?.lyrics)
|
||||
? result.lyrics
|
||||
: [],
|
||||
config: config || null,
|
||||
selections: selections || null,
|
||||
hasLyrics: false,
|
||||
});
|
||||
navigate(Routes.Lyrics);
|
||||
} catch (e) {
|
||||
console.log("Save generated lyrics error", e?.message);
|
||||
} finally {
|
||||
await setIsLoading(false);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</View>
|
||||
</BlurView>
|
||||
</View>
|
||||
|
||||
Reference in New Issue
Block a user