-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
state: add trainingStore,modelStore. update: userStore (#138)
* store set * feat: Models interface Signed-off-by: Nasfame <laciferin@gmail.com> * style: pretty code by ci * store updated * pr-fixes: trainRequestStore,modelStore Signed-off-by: Nasfame <laciferin@gmail.com> * doc: some doubts on zustand's memory management Signed-off-by: Nasfame <laciferin@gmail.com> * style: pretty code by ci * rename: stores Signed-off-by: Nasfame <laciferin@gmail.com> * modelStore: complete Signed-off-by: Nasfame <laciferin@gmail.com> * style: pretty code by ci --------- Signed-off-by: Nasfame <laciferin@gmail.com> Co-authored-by: Nasfame <laciferin@gmail.com> Co-authored-by: Nasfame <Nasfame@users.noreply.github.com>
- Loading branch information
1 parent
81e7be3
commit 3b7d85c
Showing
3 changed files
with
160 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { create } from 'zustand' | ||
import { devtools, persist } from 'zustand/middleware' | ||
import { upsert_user } from '@app/explore/upsert_user' | ||
import { Model, TrainingRequest } from '@prisma/client' | ||
|
||
type IModel = Omit<Omit<Model, 'createdAt'>, 'updatedAt'> | ||
|
||
interface Models { | ||
[modelId: string]: IModel | ||
} | ||
|
||
type Store = { | ||
// manages the state of current model, | ||
// TODO: restricts users to 1 tab | ||
model?: IModel | ||
models?: Models | ||
init: (userId: string) => void | ||
setModel: (model: Partial<IModel>) => void | ||
getModel: (id: string) => IModel | ||
} | ||
|
||
const useModelStore = create<Store>()( | ||
devtools( | ||
persist( | ||
(set, get) => ({ | ||
init(userId: string) { | ||
//make api call | ||
const returnedModels = [] | ||
const userModels: Models = {} | ||
//create object properties | ||
|
||
for (const model of returnedModels) { | ||
userModels[model.id] = model | ||
} | ||
|
||
set((state) => ({ | ||
...state, | ||
models: userModels, | ||
})) | ||
console.log('modelStore: init') | ||
}, | ||
|
||
//set user current model in focus | ||
async setModel(modelDto: Partial<IModel>, syncDB: boolean = false) { | ||
let { model } = get() | ||
|
||
set((state) => ({ | ||
...state, | ||
model: { | ||
...model, | ||
...modelDto, | ||
}, | ||
})) | ||
console.log('modelStore: setModel') | ||
|
||
if (syncDB) { | ||
// TODO: upsert the model to DB | ||
// TODO: set/update the models[id] = ... | ||
} | ||
}, | ||
getModel(id: string) { | ||
const { models } = get() | ||
|
||
let model = models[id] | ||
|
||
if (!model) { | ||
model = null | ||
// FIXME: retrieve from the DB with prisma api call | ||
} | ||
return model | ||
}, | ||
}), | ||
{ | ||
name: 'ModelStore', | ||
}, | ||
), | ||
), | ||
) | ||
|
||
export default useModelStore |
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,79 @@ | ||
import { create } from 'zustand' | ||
import { devtools, persist } from 'zustand/middleware' | ||
import { Model, TrainingRequest } from '@prisma/client' | ||
|
||
type ITrainRequest = Omit<Omit<TrainingRequest, 'createdAt'>, 'updatedAt'> | ||
|
||
interface ITrainRequsts { | ||
[requestId: string]: ITrainRequest | ||
} | ||
|
||
type Store = { | ||
// request: holds the current train request that is yet to be processed to the DB | ||
request: Partial<ITrainRequest> | ||
// requests: holds all all the train requests associated with the user | ||
requests: ITrainRequsts | ||
init: (request: Partial<ITrainRequest>) => void | ||
setTrainRequest: (request: Partial<ITrainRequest>) => void | ||
getTrainRequest: (id: string) => ITrainRequest | ||
} | ||
|
||
const useTrainRequestStore = create<Store>()( | ||
devtools( | ||
persist( | ||
(set, get) => ({ | ||
request: null, | ||
requests: null, | ||
|
||
async init() { | ||
//TODO: fill in the requests with all the training requests associated with the user | ||
|
||
set((state) => ({ | ||
...state, | ||
// TODO: api call result | ||
})) | ||
console.log('train request: init') | ||
}, | ||
|
||
setTrainRequest(requestDto: Partial<ITrainRequest>, syncDB: boolean = true) { | ||
//update request store | ||
const { request } = get() | ||
|
||
set((state) => ({ | ||
...state, | ||
request: { | ||
...request, | ||
...requestDto, | ||
}, | ||
})) | ||
console.log('train request: setTrainRequest') | ||
|
||
// TODO: see if request is updated, if not use get().request | ||
console.log(request) | ||
|
||
if (syncDB) { | ||
// TODO: upsert the request with DB | ||
// TODO: set/update the requests[id] = ... | ||
} | ||
}, | ||
|
||
getTrainRequest(id: string) { | ||
const { requests } = get() | ||
|
||
let request = requests[id] | ||
|
||
if (!request) { | ||
request = null | ||
// FIXME: retrieve from the DB with prisma api call | ||
} | ||
return request | ||
}, | ||
}), | ||
{ | ||
name: 'TrainingRequestStore', | ||
}, | ||
), | ||
), | ||
) | ||
|
||
export default useTrainRequestStore |
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 |
---|---|---|
|
@@ -67,7 +67,7 @@ const useUserStore = create<Store>()( | |
}*/ | ||
}), | ||
{ | ||
name: 'userData', | ||
name: 'UserStore', | ||
}, | ||
), | ||
), | ||
|