-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setting up zustand (initially for users only)
- Loading branch information
Showing
10 changed files
with
414 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { | ||
AdminBannerData, | ||
EntityShape, | ||
MCRUser, | ||
ReportMetadataShape, | ||
ReportShape, | ||
} from "types"; | ||
|
||
// initial user state | ||
export interface McrUserState { | ||
// INITIAL STATE | ||
user?: MCRUser; | ||
showLocalLogins: boolean | undefined; | ||
// ACTIONS | ||
setUser: (newUser?: MCRUser) => void; | ||
setShowLocalLogins: (showLocalLogins: boolean) => void; | ||
} | ||
|
||
// initial admin banner state | ||
export interface AdminBannerState { | ||
// INITIAL STATE | ||
bannerData: AdminBannerData | undefined; | ||
bannerActive: boolean; | ||
bannerLoading: boolean; | ||
bannerErrorMessage: string; | ||
bannerDeleting: boolean; | ||
// ACTIONS | ||
setBannerData: (newBannerData: AdminBannerData | undefined) => void; | ||
clearAdminBanner: () => void; | ||
setBannerActive: (bannerStatus: boolean) => void; | ||
setBannerLoading: (bannerLoading: boolean) => void; | ||
setBannerErrorMessage: (bannerErrorMessage: string) => void; | ||
setBannerDeleting: (bannerDeleting: boolean) => void; | ||
} | ||
|
||
// initial report state | ||
export interface McrReportState { | ||
// INITIAL STATE | ||
report: ReportShape | undefined; | ||
reportsByState: ReportMetadataShape[] | undefined; | ||
submittedReportsByState: ReportMetadataShape[] | undefined; | ||
lastSavedTime: string | undefined; | ||
// ACTIONS | ||
setReport: (newReport: ReportShape | undefined) => void; | ||
setReportsByState: ( | ||
newReportsByState: ReportMetadataShape[] | undefined | ||
) => void; | ||
clearReportsByState: () => void; | ||
setSubmittedReportsByState: ( | ||
newSubmittedReportsByState: ReportMetadataShape[] | undefined | ||
) => void; | ||
setLastSavedTime: (lastSavedTime: string | undefined) => void; | ||
} | ||
|
||
// initial entity state | ||
export interface McrEntityState { | ||
// INITIAL STATE | ||
selectedEntity: EntityShape | undefined; | ||
// ACTIONS | ||
setSelectedEntity: (newSelectedEntity: EntityShape | undefined) => void; | ||
clearSelectedEntity: () => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { create } from "zustand"; | ||
import { devtools, persist } from "zustand/middleware"; | ||
// types | ||
import { | ||
McrUserState, | ||
MCRUser, | ||
AdminBannerData, | ||
AdminBannerState, | ||
ReportShape, | ||
McrReportState, | ||
ReportMetadataShape, | ||
EntityShape, | ||
McrEntityState, | ||
} from "types"; | ||
|
||
// USER STORE | ||
const userStore = (set: Function) => ({ | ||
// initial state | ||
user: undefined, | ||
// show local logins | ||
showLocalLogins: undefined, | ||
// actions | ||
setUser: (newUser?: MCRUser) => | ||
set(() => ({ user: newUser }), false, { type: "setUser" }), | ||
// toggle show local logins (dev only) | ||
setShowLocalLogins: () => | ||
set(() => ({ showLocalLogins: true }), false, { type: "showLocalLogins" }), | ||
}); | ||
|
||
// BANNER STORE | ||
const bannerStore = (set: Function) => ({ | ||
// initial state | ||
bannerData: undefined, | ||
bannerActive: false, | ||
bannerLoading: false, | ||
bannerErrorMessage: "", | ||
bannerDeleting: false, | ||
// actions | ||
setBannerData: (newBanner: AdminBannerData | undefined) => | ||
set(() => ({ bannerData: newBanner }), false, { type: "setBannerData" }), | ||
clearAdminBanner: () => | ||
set(() => ({ bannerData: undefined }), false, { type: "clearAdminBanner" }), | ||
setBannerActive: (bannerStatus: boolean) => | ||
set(() => ({ bannerActive: bannerStatus }), false, { | ||
type: "setBannerActive", | ||
}), | ||
setBannerLoading: (loading: boolean) => | ||
set(() => ({ bannerLoading: loading }), false, { | ||
type: "setBannerLoading", | ||
}), | ||
setBannerErrorMessage: (errorMessage: string) => | ||
set(() => ({ bannerErrorMessage: errorMessage }), false, { | ||
type: "setBannerErrorMessage", | ||
}), | ||
setBannerDeleting: (deleting: boolean) => | ||
set(() => ({ bannerDeleting: deleting }), false, { | ||
type: "setBannerDeleting", | ||
}), | ||
}); | ||
|
||
// REPORT STORE | ||
const reportStore = (set: Function) => ({ | ||
// initial state | ||
report: undefined, | ||
reportsByState: undefined, | ||
submittedReportsByState: undefined, | ||
lastSavedTime: undefined, | ||
// actions | ||
setReport: (newReport: ReportShape | undefined) => | ||
set(() => ({ report: newReport }), false, { type: "setReport" }), | ||
setReportsByState: (newReportsByState: ReportMetadataShape[] | undefined) => | ||
set(() => ({ reportsByState: newReportsByState }), false, { | ||
type: "setReportsByState", | ||
}), | ||
clearReportsByState: () => | ||
set(() => ({ reportsByState: undefined }), false, { | ||
type: "clearReportsByState", | ||
}), | ||
setSubmittedReportsByState: ( | ||
newSubmittedReportsByState: ReportMetadataShape[] | undefined | ||
) => | ||
set( | ||
() => ({ submittedReportsByState: newSubmittedReportsByState }), | ||
false, | ||
{ type: "setSubmittedReportsByState" } | ||
), | ||
setLastSavedTime: (savedTime: string | undefined) => | ||
set(() => ({ lastSavedTime: savedTime }), false, { | ||
type: "setLastSavedTime", | ||
}), | ||
}); | ||
|
||
// ENTITY STORE | ||
const entityStore = (set: Function) => ({ | ||
// initial state | ||
selectedEntity: undefined, | ||
// actions | ||
setSelectedEntity: (newSelectedEntity: EntityShape | undefined) => | ||
set( | ||
() => ({ | ||
selectedEntity: newSelectedEntity, | ||
}), | ||
false, | ||
{ | ||
type: "setSelectedEntity", | ||
} | ||
), | ||
clearSelectedEntity: () => | ||
set(() => ({ selectedEntity: undefined }), false, { | ||
type: "clearSelectedEntity", | ||
}), | ||
}); | ||
|
||
export const useStore = create( | ||
// persist and devtools are being used for debugging state | ||
persist( | ||
devtools<McrUserState & AdminBannerState & McrReportState & McrEntityState>( | ||
(set) => ({ | ||
...userStore(set), | ||
...bannerStore(set), | ||
...reportStore(set), | ||
...entityStore(set), | ||
}) | ||
), | ||
{ | ||
name: "mcr-store", | ||
} | ||
) | ||
); |
Oops, something went wrong.