Skip to content

Commit

Permalink
fix: research save by removing lock feature (#4008)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariojsnunes authored Nov 18, 2024
1 parent 142af4d commit a245b2f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ functions/firebase-debug.log
.pnp.*

*.tsbuildinfo
supabase/.branches
supabase/.temp
supabase/.env
23 changes: 5 additions & 18 deletions src/pages/Research/Content/Common/Research.form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,12 @@ const ResearchForm = observer((props: IProps) => {

const store = useResearchStore()
const [showSubmitModal, setShowSubmitModal] = useState(false)
const [submissionHandler, setSubmissionHandler] = useState({
const [submissionHandler, setSubmissionHandler] = useState(() => ({
draft: formValues.moderation === IModerationStatus.DRAFT,
shouldSubmit: false,
})
}))
const [slug, setSlug] = useState<string>('')

// Managing locked state
useEffect(() => {
if (store.activeUser && props.research) {
store.lockResearchItem(props.research, store.activeUser.userName)
}

return () => {
if (props.research) {
store.unlockResearchItem(props.research)
}
}
}, [store.activeUser])

useEffect(() => {
if (submissionHandler.shouldSubmit) {
const form = document.getElementById('researchForm')
Expand All @@ -105,11 +92,11 @@ const ResearchForm = observer((props: IProps) => {
}
}, [submissionHandler])

const onSubmit = async (formValues: IResearch.FormInput) => {
formValues.moderation = submissionHandler.draft
const onSubmit = async (values: IResearch.FormInput) => {
values.moderation = submissionHandler.draft
? IModerationStatus.DRAFT
: IModerationStatus.ACCEPTED // No moderation for researches for now
const updatedResearh = await store.uploadResearch(formValues)
const updatedResearh = await store.uploadResearch(values)

if (updatedResearh) {
setSlug(updatedResearh.slug)
Expand Down
20 changes: 0 additions & 20 deletions src/pages/Research/Content/Common/ResearchUpdate.form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,6 @@ export const ResearchUpdateForm = observer((props: IProps) => {
}
}, [store.updateUploadStatus?.Complete])

// Managing locked state
useEffect(() => {
if (store.activeUser)
store.toggleLockResearchUpdate(
props.research._id,
store.activeUser.userName,
formValues._id,
true,
)

return () => {
store.toggleLockResearchUpdate(
props.research._id,
'',
formValues._id,
false,
)
}
}, [store.activeUser])

const trySubmitForm = (isDraft: boolean) => {
const form = document.getElementById('updateForm')
setIsDraft(isDraft)
Expand Down
60 changes: 0 additions & 60 deletions src/stores/Research/research.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ export class ResearchStore extends ModuleStore {
updateUpdateUploadStatus: action,
resetResearchUploadStatus: action,
resetUpdateUploadStatus: action,
lockResearchItem: action,
unlockResearchItem: action,
})
}

Expand Down Expand Up @@ -380,64 +378,6 @@ export class ResearchStore extends ModuleStore {
}
}

public async lockResearchItem(item: IResearchDB, username: string) {
if (item) {
const dbRef = this.db
.collection<IResearch.Item>(COLLECTION_NAME)
.doc(item._id)
const newItem = {
...item,
locked: {
by: username,
at: new Date().toISOString(),
},
}
await this._updateResearchItem(dbRef, newItem)
}
}

public async unlockResearchItem(item: IResearchDB) {
if (item) {
const dbRef = this.db
.collection<IResearch.Item>(COLLECTION_NAME)
.doc(item._id)
const newItem = {
...item,
locked: null,
}
await this._updateResearchItem(dbRef, newItem)
}
}

public async toggleLockResearchUpdate(
researchId: string,
username: string,
updateId: string,
lock: boolean,
) {
const dbRef = this.db
.collection<IResearch.Item>(COLLECTION_NAME)
.doc(researchId)

const item = toJS(await dbRef.get('server')) as IResearchDB
const updateIndex = item.updates.findIndex((upd) => upd._id === updateId)
const updatedItem = {
...item,
updates: [...item.updates],
}

if (updatedItem.updates[updateIndex]) {
updatedItem.updates[updateIndex].locked = lock
? {
by: username,
at: new Date().toISOString(),
}
: null
}

await dbRef.set(updatedItem)
}

private async _setCollaborators(
collaborators: IResearch.Item['collaborators'] | string | undefined,
) {
Expand Down

0 comments on commit a245b2f

Please sign in to comment.