Skip to content

Commit

Permalink
state: add trainingStore,modelStore. update: userStore (#138)
Browse files Browse the repository at this point in the history
* 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
3 people authored Nov 13, 2023
1 parent 81e7be3 commit 3b7d85c
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 1 deletion.
80 changes: 80 additions & 0 deletions src/state/modelStore.ts
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
79 changes: 79 additions & 0 deletions src/state/trainRequestStore.ts
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
2 changes: 1 addition & 1 deletion src/state/userStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const useUserStore = create<Store>()(
}*/
}),
{
name: 'userData',
name: 'UserStore',
},
),
),
Expand Down

0 comments on commit 3b7d85c

Please sign in to comment.