All files / utils parkUtils.js

100% Statements 56/56
100% Branches 14/14
100% Functions 11/11
100% Lines 53/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113        2x 2x       2x                       4x 11x 11x 1x 1x   10x 10x 1x 1x   9x     4x 3x 1x   2x 2x     5x 2x 1x   1x       4x 5x 5x       4x 1x 1x 1x 1x 1x 1x       4x 2x   2x     4x 2x 1x   1x 1x 1x       4x 3x 1x   2x 2x     5x 2x 1x   1x 1x 1x     4x                        
import { toast } from "react-toastify";
import { useNavigate } from 'react-router-dom'
 
export function onDeleteSuccess(message) {
    console.log(message);
    toast(message);
}
 
export function cellToAxiosParamsDelete(cell) {
    return {
        url: "/api/parks",
        method: "DELETE",
        params: {
            id: cell.row.values.id
        }
    }
}
 
 
 
// get parks from local storage
const get = () => {
    const parkValue = localStorage.getItem("parks");
    if (parkValue === undefined) {
        const parkCollection = { nextId: 1, parks: [] }
        return set(parkCollection);
    }
    const parkCollection = JSON.parse(parkValue);
    if (parkCollection === null) {
        const parkCollection = { nextId: 1, parks: [] }
        return set(parkCollection);
    }
    return parkCollection;
};
 
const getById = (id) => {
    if (id === undefined) {
        return { "error": "id is a required parameter" };
    }
    const parkCollection = get();
    const parks = parkCollection.parks;
 
    /* eslint-disable-next-line eqeqeq */ // we really do want == here, not ===
    const index = parks.findIndex((r) => r.id == id);
    if (index === -1) {
        return { "error": `park with id ${id} not found` };
    }
    return { park: parks[index] };
}
 
// set parks in local storage
const set = (parkCollection) => {
    localStorage.setItem("parks", JSON.stringify(parkCollection));
    return parkCollection;
};
 
// add a park to local storage
const add = (park) => {
    const parkCollection = get();
    park = { ...park, id: parkCollection.nextId };
    parkCollection.nextId++;
    parkCollection.parks.push(park);
    set(parkCollection);
    return park;
};
 
// update a park in local storage
const update = (park) => {
    const parkCollection = get();
 
    const parks = parkCollection.parks;
 
    /* eslint-disable-next-line eqeqeq */ // we really do want == here, not ===
    const index = parks.findIndex((r) => r.id == park.id);
    if (index === -1) {
        return { "error": `park with id ${park.id} not found` };
    }
    parks[index] = park;
    set(parkCollection);
    return { parkCollection: parkCollection };
};
 
// delete a park from local storage
const del = (id) => {
    if (id === undefined) {
        return { "error": "id is a required parameter" };
    }
    const parkCollection = get();
    const parks = parkCollection.parks;
 
    /* eslint-disable-next-line eqeqeq */ // we really do want == here, not ===
    const index = parks.findIndex((r) => r.id == id);
    if (index === -1) {
        return { "error": `park with id ${id} not found` };
    }
    parks.splice(index, 1);
    set(parkCollection);
    return { parkCollection: parkCollection };
};
 
const parkUtils = {
    get,
    getById,
    add,
    update,
    del
};
 
export { parkUtils };