some change
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,4 +1,6 @@
|
||||
node_modules
|
||||
/build
|
||||
/*.log
|
||||
/.idea
|
||||
/.idea
|
||||
|
||||
deploy.sh
|
||||
1
size-plugin.json
Normal file
1
size-plugin.json
Normal file
File diff suppressed because one or more lines are too long
@@ -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}}>
|
||||
|
||||
@@ -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">
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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])
|
||||
|
||||
|
||||
@@ -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>
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
56
src/sw.js
56
src/sw.js
@@ -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()
|
||||
|
||||
@@ -11,5 +11,6 @@
|
||||
</head>
|
||||
<body>
|
||||
<% preact.bodyEnd %>
|
||||
<script src="sw.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user