Adjusted helper functions to call from single index file

This commit is contained in:
Xinrui Chen 2022-07-14 08:47:59 -07:00
parent cafb35108a
commit 1cbe0758c1
13 changed files with 16 additions and 23 deletions

View File

@ -1,7 +1,5 @@
const getAvgRating = (ratings) => {
export const getAvgRating = (ratings) => {
if (!ratings) return 0;
if (ratings.length === 1) return ratings[0].rating;
else return ratings.reduce((prev, curr) => prev.rating + curr.rating) / ratings.length;
};
export default getAvgRating;

4
src/helpers/index.js Normal file
View File

@ -0,0 +1,4 @@
export * from './toProduct';
export * from './parse';
export * from './normalize';
export * from './getAvgRating';

View File

@ -1,3 +1,2 @@
const normalize = (str) => str.toLowerCase().trim()
export const normalize = (str) => str.toLowerCase().trim()
export default normalize;

View File

@ -1,4 +1,4 @@
import normalize from './normalize';
import {normalize }from './normalize';
export function parseSlug(slug) {
return normalize(slug).replaceAll("-", " ")

View File

@ -1,11 +1,8 @@
import normalize from './normalize';
import { parseName } from './parse';
import {normalize, parseName} from './index';
const toProduct = (product, currentProduct) => {
export const toProduct = (product, currentProduct) => {
const url = new URL(window.location);
url.searchParams.set('product', normalize(parseName(product.name)));
window.history.pushState({}, '', url);
currentProduct.set(product)
}
export default toProduct;

View File

@ -1,6 +1,6 @@
<script>
import { urlFor } from '$lib/sanityClient';
import { productsView, currentProduct } from '$lib/stores';
import { currentProduct } from '$lib/stores';
</script>
<div class="content">

View File

@ -1,7 +1,7 @@
<script>
import { urlFor } from './sanityClient';
import { currentProduct } from '$lib/stores.js';
import toProduct from '$helpers/toProduct';
import { toProduct } from '$helpers';
export let products;
const {container} = {

View File

@ -1,7 +1,6 @@
<script>
import { productsView, products, tags } from '$lib/stores';
import normalize from '$helpers/normalize';
import getAvgRating from '$helpers/getAvgRating';
import {normalize, getAvgRating } from '$helpers';
export let filters;
export let reset;
let { selectedCat, selectedRating } = filters;

View File

@ -1,7 +1,7 @@
<script>
export let productsView;
export let currentProduct;
import toProduct from '$helpers/toProduct';
import { toProduct } from '$helpers';
const { container, productStyle } = {
container: 'pt-4',

View File

@ -1,6 +1,6 @@
<script>
import { products, productsView } from '$lib/stores';
import normalize from '$helpers/normalize';
import { normalize } from '$helpers';
const searchProducts = (e) => {
productsView.set(

View File

@ -1,8 +1,7 @@
<script>
import SortOption from './SortOption.svelte';
import { productsView } from '$lib/stores';
import normalize from '$helpers/normalize';
import getAvgRating from '$helpers/getAvgRating';
import {normalize, getAvgRating } from '$helpers';
const sortOptions = ['Rating', 'Name', 'Created'];

View File

@ -1,6 +1,5 @@
<script>
import '../app.css';
import { page } from '$app/stores';
import { products, productsView, currentProduct } from '$lib/stores';
import Products from '$lib/Layout/Products.svelte';
import Header from '$lib/Layout/Header.svelte';

View File

@ -3,8 +3,7 @@
import Feature from '$lib/Feature.svelte';
import { products, productsView, tags, currentProduct } from '$lib/stores';
import { browser } from '$app/env';
import normalize from '$helpers/normalize';
import { parseSlug } from '$helpers/parse';
import {normalize, parseSlug } from '$helpers';
export let data;
products.set(data.products);
@ -31,7 +30,6 @@
if (browser) {
window.onpopstate = () => {
console.log('sttae poped')
goToProduct();
}
}