some change

This commit is contained in:
Romulus21
2022-01-14 14:48:31 +01:00
parent 7abf0bafa5
commit e9a4550070
10 changed files with 18 additions and 79 deletions

View File

@@ -1,22 +1,16 @@
import { createContext } from "preact";
import { useEffect, useState } from "preact/hooks";
import usePlants from "./hooks/PlantsHook";
import { useLocalStorage } from "./hooks/LocalStorageHook"
import useUser from "./hooks/UserHook";
export const UserContext = createContext()
export const PlantsContext = createContext()
export const UserContext = createContext(null)
export const PlantsContext = createContext(null)
export default function ContextsProviders({children}) {
const [data, setData] = useLocalStorage('data', {})
const [user, setUser] = useUser(data, setData)
const {plants, addPlant, editPlant, removePlant, addAction, doneTask, history} = usePlants(data, setData)
useEffect(() => {
console.log('first', user);
}, [user])
return <UserContext.Provider value={[user, setUser]}>
<PlantsContext.Provider value={{plants, addPlant, editPlant, removePlant, addAction, doneTask, history}}>

View File

@@ -12,13 +12,13 @@ import {useEffect} from "preact/hooks";
const App = () => {
useEffect(() => {
if (!Notification) {
alert('Le navigateur ne supporte pas les notifications.');
} else if (Notification.permission !== 'granted') {
Notification.requestPermission();
}
}, [])
// useEffect(() => {
// if (!Notification) {
// alert('Le navigateur ne supporte pas les notifications.');
// } else if (Notification.permission !== 'granted') {
// Notification.requestPermission();
// }
// }, [])
return (
<div id="app" class="h-screen overflow-auto flex flex-col">

View File

@@ -3,7 +3,6 @@ import {route} from "preact-router";
const usePlants = (data, setData) => {
const [plants, setPlants] = useState([])
const [tasks, setTasks] = useState([])
useEffect(() =>{
setPlants(data.plants ?? [])
@@ -32,9 +31,7 @@ const usePlants = (data, setData) => {
const addAction = (plant, action) => {
action.id = action.action_type + '-' + plant.id
let plantIndex = plants.findIndex(item => item.id === plant.id)
let actionIndex = plant.actions.findIndex(item => item.action_type === action.action_type)
console.log(actionIndex, plantIndex, plant, plants, action)
actionIndex >= 0 ? plant.actions[actionIndex] = action
: plant.actions.push(action)
editPlant(plant)

View File

@@ -6,7 +6,6 @@ const useUser = (data, setData) => {
useEffect(() =>{
setData({...data, user: user})
console.log('in', user);
document.querySelector('html').classList.toggle('dark', user.dark_mode)
}, [user])

View File

@@ -11,13 +11,11 @@ import {Tasks} from "../components/Tasks"
export const Home = () => {
const [addModal, setAddModal] = useState(false)
const [plantForm, setPlantForm] = useState({})
const app = document.getElementById("app")
const { plants, addPlant } = useContext(PlantsContext)
const handleSubmit = (e) => {
e.preventDefault()
e.stopPropagation()
console.log(plantForm)
addPlant(plantForm)
setAddModal(false)
}
@@ -47,7 +45,7 @@ export const Home = () => {
</div>
</div>
{createPortal(
{typeof window !== "undefined" && createPortal(
<Modal isOpen={addModal} onChange={handleCloseAddModal}>
<ModalTitle>
Add Plant
@@ -62,7 +60,7 @@ export const Home = () => {
</Button>
</form>
</Modal>,
app
document.getElementById('app')
)}
</PageLayout>
)

View File

@@ -37,7 +37,6 @@ const Plant = ({id}) => {
if (!actionForm.action_type) {
actionForm.action_type = action_types[0]
}
console.log("my event", e, actionForm)
addAction(plant, actionForm)
setAddModal(false)
}

View File

@@ -1,55 +1,3 @@
import { getFiles, setupPrecaching, setupRouting } from 'preact-cli/sw/';
import { setupRouting } from 'preact-cli/sw'
setupRouting();
setupPrecaching(getFiles());
function wait(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
const BASE = location.protocol + '//' + location.host
const PREFIX = "V1"
const CACHED_FILES = [
`${BASE}/sw.js`,
`${BASE}/js/app.js`,
`${BASE}/css/app.css`,
`${BASE}/offline.html`,
]
self.addEventListener('install', (event) => {
self.skipWaiting()
event.waitUntil(
(async () => {
const cache = await caches.open(PREFIX)
await cache.addAll(CACHED_FILES)
})()
)
console.log(`${PREFIX} Install`)
})
self.addEventListener('activate', (event) => {
clients.claim()
event.waitUntil((async() => {
const keys = await caches.keys()
await Promise.all(
keys.map(key => {
if (!key.includes(PREFIX)) {
return caches.delete(key)
}
})
)
})())
console.log(`${PREFIX} Activate`)
})
const delay = 1000 * 60 * 60 * 24
console.log(localStorage.getItem('data'))
wait(delay)
.then(() => {
// do thing
}).catch(err => console.log(err))
setupRouting()

View File

@@ -11,5 +11,6 @@
</head>
<body>
<% preact.bodyEnd %>
<script src="sw.js"></script>
</body>
</html>