-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
1,286 additions
and
450 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import styled from '@emotion/styled'; | ||
import { alpha, IconButton, InputBase, OutlinedInput } from '@mui/material'; | ||
import SearchIcon from '@mui/icons-material/Search'; | ||
import RestartAltIcon from '@mui/icons-material/RestartAlt'; | ||
import React, { useEffect, useState } from 'react' | ||
|
||
interface ISearchBox { | ||
dataSource?: any[]; | ||
setDataSource?: (data: any) => void; | ||
} | ||
|
||
const SearchBox: React.FC<ISearchBox> = ({ dataSource, setDataSource }) => { | ||
|
||
const [searchKey, setSearchKey] = useState<string>(""); | ||
const originalDataSource: any[] = dataSource ?? []; | ||
|
||
|
||
const onReset = () => { | ||
setSearchKey(""); | ||
setDataSource && setDataSource(originalDataSource); | ||
} | ||
|
||
const filterDataSourceBysearchKey = () => { | ||
// Escape special characters in the searchKey to build a valid regular expression | ||
const escapedSearchKey = searchKey.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
|
||
// Create a regular expression pattern that matches strings starting with the searchKey | ||
const regexPattern = new RegExp(`^${escapedSearchKey}`, 'i'); | ||
|
||
const filter: any[] = dataSource?.filter((item: any) => regexPattern.test(item.name)) ?? [] | ||
setDataSource && setDataSource(filter); | ||
} | ||
|
||
useEffect(() => { | ||
if (searchKey === "") { | ||
setDataSource && setDataSource(originalDataSource); | ||
} else { | ||
filterDataSourceBysearchKey() | ||
} | ||
|
||
}, [searchKey]) | ||
|
||
|
||
return ( | ||
<div> | ||
<OutlinedInput | ||
value={searchKey} | ||
sx={{ my: 1, flex: 1 }} | ||
placeholder="Search By Name" | ||
inputProps={{ 'aria-label': 'search by name' }} | ||
onChange={(e) => setSearchKey(e.target.value)} | ||
/> | ||
{searchKey !== "" && | ||
<IconButton | ||
aria-label="search" | ||
color='primary' | ||
sx={{ mx: 1 }} | ||
onClick={onReset} | ||
> | ||
<RestartAltIcon /> | ||
</IconButton> | ||
} | ||
|
||
<IconButton | ||
aria-label="search" | ||
color='primary' | ||
sx={{ mx: 1 }} | ||
disabled={searchKey === ""} | ||
onClick={filterDataSourceBysearchKey} | ||
> | ||
<SearchIcon /> | ||
</IconButton> | ||
</div> | ||
) | ||
} | ||
|
||
export default SearchBox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { collection, addDoc, doc, getDoc, getDocs, setDoc, query, where } from 'firebase/firestore'; | ||
import { db } from '..'; | ||
|
||
const collectionName: string = 'foods'; | ||
|
||
const useFoodServices = () => { | ||
/** | ||
* save | ||
* @param payload | ||
*/ | ||
const save = async (payload: IFood) => { | ||
try { | ||
await addDoc(collection(db, collectionName), payload) | ||
.then((docRef: any) => { | ||
console.log(`[SUCCESS][SAVE]| ${collectionName} written with ID: `, docRef.id); | ||
return docRef.id; | ||
}) | ||
.catch((error: any) => { | ||
console.error(`[ERROR][SAVE] | ${collection}: `, error); | ||
}); | ||
} catch (error) { | ||
console.error(`[ERROR][SAVE] | ${collectionName} : `, error); | ||
} | ||
}; | ||
|
||
/** | ||
* get List full | ||
*/ | ||
const getList = async () => { | ||
try { | ||
await getDocs(collection(db, collectionName)) | ||
.then((res: any) => { | ||
const documents = res.docs.map((doc: { data: () => any }) => doc.data()); | ||
console.log(`[SUCCESS][GET] | ${collectionName} : `, documents); | ||
return documents; | ||
}) | ||
.catch((error: any) => { | ||
console.error(`[ERROR][GET] | ${collectionName} : `, error); | ||
}); | ||
} catch (error) { | ||
console.error(`[ERROR][GET] | ${collectionName} : `, error); | ||
} | ||
}; | ||
|
||
/** | ||
* get signle data by id | ||
* @param id | ||
*/ | ||
const getById = async (id: string) => { | ||
try { | ||
const docRef = doc(db, collectionName, id); | ||
await getDoc(docRef) | ||
.then((resDoc: any) => { | ||
if (resDoc.exists()) { | ||
const response = resDoc.data(); | ||
console.log(`[SUCCESS][GETBYIT] | ${collectionName} : `, response); | ||
return response; | ||
} else { | ||
console.log(`[SUCCESS][GETBYIT] | ${collectionName} : Document does not exist`); | ||
} | ||
}) | ||
.catch((error: any) => { | ||
console.error(`[ERROR][GETBYID] | ${collectionName} : `, error); | ||
}); | ||
} catch (error) { | ||
console.error(`[ERROR][GETBYID] | ${collectionName} : `, error); | ||
} | ||
}; | ||
|
||
/** | ||
* update single data by id | ||
* @param id | ||
* @param pyaload | ||
*/ | ||
const updateById = async (id: string, pyaload: IFood) => { | ||
try { | ||
const docRef = doc(db, collectionName, id); | ||
await setDoc(docRef, pyaload, { merge: true }) | ||
.then((resDoc: any) => { | ||
console.log(`[SUCCESS][UPDATE] | ${collectionName} : Document updated successfully`); | ||
}) | ||
.catch((error: any) => { | ||
console.error(`[ERROR][UPDATE] | ${collectionName} : `, error); | ||
}); | ||
} catch (error) { | ||
console.error(`[ERROR][UPDATE] | ${collectionName} : `, error); | ||
} | ||
}; | ||
|
||
/** | ||
* get list data by user id | ||
* @param uid | ||
* @returns | ||
*/ | ||
const getListByUserId = async (uid: string) => { | ||
const qry = query(collection(db, collectionName), where('uid', '==', uid)); | ||
try { | ||
const querySnapshot = await getDocs(qry); | ||
if (!querySnapshot.empty) { | ||
const response = querySnapshot.docs.map((doc) => { | ||
return { id: doc.id, ...doc.data() }; | ||
}); | ||
console.log(`[SUCCESS][GETBYIT] | ${collectionName} : `, response); | ||
return response; | ||
} else { | ||
console.log(`[SUCCESS][GETBYIT] | ${collectionName} : Document does not exist`); | ||
return []; | ||
} | ||
} catch (error) { | ||
console.error(`[ERROR][GET] | ${collectionName} : `, error); | ||
// You might want to throw the error here or handle it as needed | ||
throw error; | ||
} | ||
}; | ||
|
||
return { save, getList, getById, updateById, getListByUserId }; | ||
}; | ||
|
||
export default useFoodServices; |
Oops, something went wrong.
6425b88
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
cheers-dashboard – ./
cheers-dashboard-git-main-nu5rim3.vercel.app
cheers-dashboard-tan.vercel.app
cheers-dashboard-nu5rim3.vercel.app