📅  最后修改于: 2023-12-03 15:20:54.630000             🧑  作者: Mango
Upapet is a web application that helps pet owners track their pets' health and schedule appointments with veterinarians. It allows users to create multiple pet profiles, add their pets' medical records, and set reminders for appointments and medication.
Upapet is built using the following technologies:
Here is a code snippet demonstrating how Upapet uses Redux to manage state:
import { createStore } from 'redux';
// Define initial state
const initialState = {
pets: [],
appointments: [],
medications: [],
};
// Define reducer function
function reducer(state = initialState, action) {
switch (action.type) {
case 'ADD_PET':
return {
...state,
pets: [...state.pets, action.payload],
};
case 'ADD_APPOINTMENT':
return {
...state,
appointments: [...state.appointments, action.payload],
};
case 'ADD_MEDICATION':
return {
...state,
medications: [...state.medications, action.payload],
};
default:
return state;
}
}
// Create store
const store = createStore(reducer);
// Dispatch actions
store.dispatch({ type: 'ADD_PET', payload: { name: 'Fluffy', breed: 'Persian' } });
store.dispatch({ type: 'ADD_APPOINTMENT', payload: { date: '2022-01-01', pet: 'Fluffy' } });
store.dispatch({ type: 'ADD_MEDICATION', payload: { name: 'Vitamin C', pet: 'Fluffy' } });
// Get state
const state = store.getState();
console.log(state);
// Output: {
// pets: [{ name: 'Fluffy', breed: 'Persian' }],
// appointments: [{ date: '2022-01-01', pet: 'Fluffy' }],
// medications: [{ name: 'Vitamin C', pet: 'Fluffy' }]
// }
This code creates a Redux store with an initial state containing empty arrays for pets, appointments, and medications. It then defines a reducer function that updates the state based on dispatched actions. Finally, it dispatches three actions to add a pet, appointment, and medication, and logs the resulting state to the console.