init
This commit is contained in:
149
src/lib/app-state.ts
Normal file
149
src/lib/app-state.ts
Normal file
@@ -0,0 +1,149 @@
|
||||
import type { Place } from "./types";
|
||||
import { PLACES } from "./mock-data";
|
||||
|
||||
export type Screen = "places" | "collections" | "profile" | "place" | "collection";
|
||||
|
||||
export type Tab = "places" | "collections" | "profile";
|
||||
|
||||
export type StackFrame = {
|
||||
screen: Screen;
|
||||
placeId?: string;
|
||||
collectionId?: string;
|
||||
};
|
||||
|
||||
export type Modal =
|
||||
| { type: "add" }
|
||||
| { type: "invite"; collectionId: string }
|
||||
| { type: "members"; collectionId: string }
|
||||
| { type: "confirmDeletePlace"; placeId: string }
|
||||
| { type: "confirmDeleteCollection"; collectionId: string }
|
||||
| null;
|
||||
|
||||
export type AppState = {
|
||||
tab: Tab;
|
||||
stack: StackFrame[];
|
||||
filter: string;
|
||||
search: string;
|
||||
places: Place[];
|
||||
modal: Modal;
|
||||
toast: string | null;
|
||||
toastKey: number;
|
||||
offline: boolean;
|
||||
};
|
||||
|
||||
export type Action =
|
||||
| { type: "NAV"; screen: Screen; placeId?: string; collectionId?: string }
|
||||
| { type: "BACK" }
|
||||
| { type: "TAB"; tab: Tab }
|
||||
| { type: "SET_FILTER"; value: string }
|
||||
| { type: "SET_SEARCH"; value: string }
|
||||
| { type: "TOGGLE_VISITED"; placeId: string }
|
||||
| { type: "SET_RATING"; placeId: string; value: number }
|
||||
| { type: "SET_NOTES"; placeId: string; value: string }
|
||||
| { type: "ADD_PLACE"; place: Place }
|
||||
| { type: "DELETE_PLACE"; placeId: string }
|
||||
| { type: "TOAST"; value: string }
|
||||
| { type: "CLEAR_TOAST"; key: number }
|
||||
| { type: "OPEN_ADD" }
|
||||
| { type: "OPEN_INVITE"; collectionId: string }
|
||||
| { type: "OPEN_MEMBERS"; collectionId: string }
|
||||
| { type: "CONFIRM_DELETE_PLACE"; placeId: string }
|
||||
| { type: "CONFIRM_DELETE_COLLECTION"; collectionId: string }
|
||||
| { type: "CLOSE_MODAL" }
|
||||
| { type: "SET_OFFLINE"; value: boolean };
|
||||
|
||||
export const INITIAL_STATE: AppState = {
|
||||
tab: "places",
|
||||
stack: [{ screen: "places" }],
|
||||
filter: "all",
|
||||
search: "",
|
||||
places: PLACES,
|
||||
modal: null,
|
||||
toast: null,
|
||||
toastKey: 0,
|
||||
offline: false,
|
||||
};
|
||||
|
||||
export function reducer(state: AppState, action: Action): AppState {
|
||||
switch (action.type) {
|
||||
case "NAV": {
|
||||
const stack = [
|
||||
...state.stack,
|
||||
{ screen: action.screen, placeId: action.placeId, collectionId: action.collectionId },
|
||||
];
|
||||
return { ...state, stack };
|
||||
}
|
||||
case "BACK": {
|
||||
if (state.stack.length <= 1) return state;
|
||||
return { ...state, stack: state.stack.slice(0, -1) };
|
||||
}
|
||||
case "TAB": {
|
||||
return { ...state, tab: action.tab, stack: [{ screen: action.tab }] };
|
||||
}
|
||||
case "SET_FILTER":
|
||||
return { ...state, filter: action.value };
|
||||
case "SET_SEARCH":
|
||||
return { ...state, search: action.value };
|
||||
case "TOGGLE_VISITED": {
|
||||
const places = state.places.map((p) =>
|
||||
p.id === action.placeId
|
||||
? {
|
||||
...p,
|
||||
visited: !p.visited,
|
||||
visited_at: !p.visited
|
||||
? new Date().toISOString().slice(0, 10)
|
||||
: undefined,
|
||||
}
|
||||
: p,
|
||||
);
|
||||
return { ...state, places };
|
||||
}
|
||||
case "SET_RATING": {
|
||||
const places = state.places.map((p) =>
|
||||
p.id === action.placeId
|
||||
? { ...p, my_rating: action.value || undefined }
|
||||
: p,
|
||||
);
|
||||
return { ...state, places };
|
||||
}
|
||||
case "SET_NOTES": {
|
||||
const places = state.places.map((p) =>
|
||||
p.id === action.placeId ? { ...p, my_notes: action.value } : p,
|
||||
);
|
||||
return { ...state, places };
|
||||
}
|
||||
case "ADD_PLACE": {
|
||||
return { ...state, places: [action.place, ...state.places] };
|
||||
}
|
||||
case "DELETE_PLACE": {
|
||||
return {
|
||||
...state,
|
||||
places: state.places.filter((p) => p.id !== action.placeId),
|
||||
stack: state.stack.length > 1 ? state.stack.slice(0, -1) : state.stack,
|
||||
modal: null,
|
||||
};
|
||||
}
|
||||
case "TOAST":
|
||||
return { ...state, toast: action.value, toastKey: state.toastKey + 1 };
|
||||
case "CLEAR_TOAST":
|
||||
return state.toastKey === action.key ? { ...state, toast: null } : state;
|
||||
case "OPEN_ADD":
|
||||
return { ...state, modal: { type: "add" } };
|
||||
case "OPEN_INVITE":
|
||||
return { ...state, modal: { type: "invite", collectionId: action.collectionId } };
|
||||
case "OPEN_MEMBERS":
|
||||
return { ...state, modal: { type: "members", collectionId: action.collectionId } };
|
||||
case "CONFIRM_DELETE_PLACE":
|
||||
return { ...state, modal: { type: "confirmDeletePlace", placeId: action.placeId } };
|
||||
case "CONFIRM_DELETE_COLLECTION":
|
||||
return { ...state, modal: { type: "confirmDeleteCollection", collectionId: action.collectionId } };
|
||||
case "CLOSE_MODAL":
|
||||
return { ...state, modal: null };
|
||||
case "SET_OFFLINE":
|
||||
return { ...state, offline: action.value };
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
export type Dispatch = (action: Action) => void;
|
||||
Reference in New Issue
Block a user