-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Like and Favorite Page using IDB
- Loading branch information
1 parent
3362b70
commit 67d1de2
Showing
10 changed files
with
165 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { openDB } from 'idb'; | ||
import CONFIG from '../globals/config'; | ||
|
||
const { DATABASE_NAME, DATABASE_VERSION, OBJECT_STORE_NAME } = CONFIG; | ||
|
||
const dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, { | ||
upgrade(database) { | ||
database.createObjectStore(OBJECT_STORE_NAME, { keyPath: 'id' }); | ||
}, | ||
}); | ||
|
||
const FavoriteRestaurantIdb = { | ||
async getRestaurant(id) { | ||
return (await dbPromise).get(OBJECT_STORE_NAME, id); | ||
}, | ||
async getAllRestaurant() { | ||
return (await dbPromise).getAll(OBJECT_STORE_NAME); | ||
}, | ||
async putRestaurant(restaurant) { | ||
return (await dbPromise).put(OBJECT_STORE_NAME, restaurant); | ||
}, | ||
async deleteRestaurant(id) { | ||
return (await dbPromise).delete(OBJECT_STORE_NAME, id); | ||
}, | ||
}; | ||
|
||
export default FavoriteRestaurantIdb; |
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
const CONFIG = { | ||
BASE_URL: 'https://restaurant-api.dicoding.dev/', | ||
BASE_IMAGE_URL: 'https://restaurant-api.dicoding.dev/images/medium/', | ||
CACHE_NAME: 'RestoMantap-v1', | ||
CACHE_NAME: 'RestoMantap-1', | ||
DATABASE_NAME: 'restaurant-catalogue-database', | ||
DATABASE_VERSION: 1, | ||
OBJECT_STORE_NAME: 'restaurants', | ||
}; | ||
|
||
export default CONFIG; |
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,48 @@ | ||
import FavoriteRestaurantIdb from '../data/database'; | ||
import { createLikeButtonTemplate, createLikedButtonTemplate } from '../views/templates/template-creator'; | ||
|
||
const LikeButtonInitiator = { | ||
async init({ likeButtonContainer, restaurant }) { | ||
this._likeButtonContainer = likeButtonContainer; | ||
this._restaurant = restaurant; | ||
|
||
await this._renderButton(); | ||
}, | ||
|
||
async _renderButton() { | ||
const { id } = this._restaurant; | ||
|
||
if (await this._isRestaurantExist(id)) { | ||
this._renderLiked(); | ||
} else { | ||
this._renderLike(); | ||
} | ||
}, | ||
|
||
async _isRestaurantExist(id) { | ||
const restaurant = await FavoriteRestaurantIdb.getRestaurant(id); | ||
return !!restaurant; | ||
}, | ||
|
||
_renderLike() { | ||
this._likeButtonContainer.innerHTML = createLikeButtonTemplate(); | ||
|
||
const likeButton = document.querySelector('#likeButton'); | ||
likeButton.addEventListener('click', async () => { | ||
await FavoriteRestaurantIdb.putRestaurant(this._restaurant); | ||
this._renderButton(); | ||
}); | ||
}, | ||
|
||
_renderLiked() { | ||
this._likeButtonContainer.innerHTML = createLikedButtonTemplate(); | ||
|
||
const likeButton = document.querySelector('#likeButton'); | ||
likeButton.addEventListener('click', async () => { | ||
await FavoriteRestaurantIdb.deleteRestaurant(this._restaurant.id); | ||
this._renderButton(); | ||
}); | ||
}, | ||
}; | ||
|
||
export default LikeButtonInitiator; |
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 |
---|---|---|
@@ -1,13 +1,26 @@ | ||
const Favorite = { | ||
import FavoriteRestaurantIdb from '../../data/database'; | ||
import { createRestaurantItemTemplate } from '../templates/template-creator'; | ||
|
||
const Like = { | ||
async render() { | ||
return ` | ||
<h2>Favorite Page</h2> | ||
`; | ||
<section class="headline" id="headline"> | ||
<h2>List Restoran Mantap Favorit Anda</h2> | ||
</section> | ||
<section class="content" id="list"> | ||
</section> | ||
`; | ||
}, | ||
|
||
async afterRender() { | ||
// Fungsi ini akan dipanggil setelah render() | ||
const restaurants = await FavoriteRestaurantIdb.getAllRestaurant(); | ||
const restaurantContainer = document.querySelector('#list'); | ||
restaurants.forEach((restaurant) => { | ||
restaurantContainer.innerHTML += createRestaurantItemTemplate(restaurant); | ||
}); | ||
}, | ||
}; | ||
|
||
export default Favorite; | ||
export default Like; |
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