allowed category editing and deletion

This commit is contained in:
moist-webDev 2023-06-09 22:06:28 -04:00
parent d6bb4d7a16
commit 8e0a06183a
1 changed files with 61 additions and 12 deletions

View File

@ -6,17 +6,34 @@ import db from '../lib/client'
export default function OtherCat({theme, otherCategories, setOtherCategories, setSelCat, setShowBack, setTitle}:any){ export default function OtherCat({theme, otherCategories, setOtherCategories, setSelCat, setShowBack, setTitle}:any){
const [modalVisible, setModalVisible] = useState(false); const [modalVisible, setModalVisible] = useState(false);
const [newCat, setNewCat] = useState({title: '', showEpisodes: false}); const [newCat, setNewCat]:any = useState({id: 0, title: '', showEpisodes: false});
const [editPop, setEditPop] = useState(false);
const [catSorted, setCatSorted] = useState(otherCategories);
//sort aplhabetically using a.title and b.title in a useEffect
useEffect(()=>{
setCatSorted(otherCategories.sort((a:any, b:any) => a.title.localeCompare(b.title)))
},[otherCategories])
const st = style(theme); const st = style(theme);
const submit = async () => { const submit = async () => {
const res = await db.from('categories').insert(newCat); if (!editPop){
if (res.data) { const res = await db.from('categories').insert(newCat);
setOtherCategories([...otherCategories, res.data]) if (res.data) {
setModalVisible(false); setOtherCategories([...otherCategories, res.data])
setNewCat({title: '', showEpisodes: false}); setModalVisible(false);
setNewCat({title: '', showEpisodes: false});
}
}else {
const res = await db.from('categories').update(newCat).eq({id: newCat.id});
if (res.data) {
const updatedList = [...otherCategories.filter((cat:any) => cat.id !== newCat.id), newCat]
setOtherCategories(updatedList)
setModalVisible(false);
setNewCat({title: '', showEpisodes: false});
}
} }
} }
@ -28,16 +45,43 @@ export default function OtherCat({theme, otherCategories, setOtherCategories, se
setNewCat((current:any)=>{return {...current, showEpisodes: !current.showEpisodes}}) setNewCat((current:any)=>{return {...current, showEpisodes: !current.showEpisodes}})
} }
const handleAddNew = () => {
setNewCat({title: '', showEpisodes: false});
setEditPop(false);
setModalVisible(true);
}
const handleEdit = (cat:any) => {
setNewCat(cat);
setEditPop(true);
setModalVisible(true);
}
const handleDelete = async () => {
const res = await db.from('categories').delete().eq({id: newCat.id});
if (res.data) {
setOtherCategories([...otherCategories.filter((c:any) => c.id !== newCat.id)])
}
setModalVisible(false);
setNewCat({title: '', showEpisodes: false});
setEditPop(false);
}
return ( return (
<View style={st.container}> <View style={st.container}>
<TouchableOpacity onPress={()=>setModalVisible(true)}> <TouchableOpacity onPress={handleAddNew}>
<Text style={st.add}>ADD NEW CATEGORY</Text> <Text style={st.add}>ADD NEW CATEGORY</Text>
</TouchableOpacity> </TouchableOpacity>
<ScrollView> <ScrollView>
{otherCategories.map((cat:any) => { {catSorted.map((cat:any) => {
return ( return (
<Link onPress={()=>{setShowBack('categories');setSelCat(cat); setTitle(cat.title);}} to='/other' style={st.category} key={cat.id}> <Link onPress={()=>{setShowBack('categories');setSelCat(cat); setTitle(cat.title);}} to='/other' style={st.category} key={cat.id}>
<Text style={st.text}>{cat.title}</Text> <View style={st.row}>
<Text style={{...st.text, flex:1, textAlign: 'center', fontSize: 20}}>{cat.title}</Text>
<TouchableOpacity style={{padding:10}} onPress={()=>handleEdit(cat)}>
<AntDesign style={st.text} name="edit" size={16} />
</TouchableOpacity>
</View>
</Link> </Link>
) )
})} })}
@ -51,7 +95,7 @@ export default function OtherCat({theme, otherCategories, setOtherCategories, se
<View style={st.modalView}> <View style={st.modalView}>
<View style={st.row}> <View style={st.row}>
<Text style={st.modalBack} onPress={()=>setModalVisible(false)}><Ionicons name="chevron-back" size={20} /></Text> <Text style={st.modalBack} onPress={()=>setModalVisible(false)}><Ionicons name="chevron-back" size={20} /></Text>
<Text style={st.modalTitle} >Add Movie</Text> <Text style={st.modalTitle} >{(editPop)?'Edit':'Add'} Movie</Text>
<Text style={st.modalSubmit} onPress={submit}>SUBMIT</Text> <Text style={st.modalSubmit} onPress={submit}>SUBMIT</Text>
</View> </View>
<View style={st.row}> <View style={st.row}>
@ -62,6 +106,7 @@ export default function OtherCat({theme, otherCategories, setOtherCategories, se
<Text style={st.switchText}>Show season and episode tracker: </Text> <Text style={st.switchText}>Show season and episode tracker: </Text>
<Switch style={st.switch} onValueChange={handleCheck} value={newCat.showEpisodes}/> <Switch style={st.switch} onValueChange={handleCheck} value={newCat.showEpisodes}/>
</View> </View>
{(editPop)?<Text style={st.delete} onLongPress={handleDelete}>DELETE</Text>:null}
</View> </View>
</View> </View>
</Modal> </Modal>
@ -79,6 +124,11 @@ const style = (theme:any) => {
text: { text: {
color: theme.text, color: theme.text,
}, },
delete: {
color: 'red',
textAlign: 'center',
marginVertical: 0,
},
switch: { switch: {
flex: 1, flex: 1,
marginHorizontal: 20 marginHorizontal: 20
@ -96,9 +146,8 @@ const style = (theme:any) => {
}, },
category: { category: {
color: theme.text, color: theme.text,
fontSize: 20,
textAlign: 'center', textAlign: 'center',
padding: 20, padding: 14,
borderColor: theme.accent, borderColor: theme.accent,
borderWidth: 1, borderWidth: 1,
justifyContent: 'center', justifyContent: 'center',